If We Were a Child Again


Description

The Problem

 

The first project for the poor student was to make a calculator that can just perform the basic arithmetic operations.

 


But like many other university students he doesn’t like to do any project by himself. He just wants to collect programs from here and there. As you are a friend of him, he asks you to write the program. But, you are also intelligent enough to tackle this kind of people. You agreed to write only the (integer) division and mod (% in C/C++) operations for him.

Input

Input is a sequence of lines. Each line will contain an input number. One or more spaces. A sign (division or mod). Again spaces. And another input number. Both the input numbers are non-negative integer. The first one may be arbitrarily long. The second number n will be in the range (0 < n < 231).

Output

A line for each input, each containing an integer. See the sample input and output. Output should not contain any extra space.

Sample Input

110 / 100
99 % 10
2147483647 / 2147483647
2147483646 % 2147483647

Sample Output

1
9
1
2147483646

HINT

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	char str[1000],a[100],b[100],c;
	int t,i,j,x,y;
    int	 num,s,d[1000],e,m;
	while(gets(str))
	{
		t=0;
		for(i=0;str[i];i++)
		{
			if(str[i]=='/'||str[i]=='%')
			{
				c=str[i];
				a[t-1]='\0';
				break;
			}
			else
				a[t++]=str[i];
		}
		a[t]='\0';
		t=0;
		for(j=i+2;str[j];j++)
			b[t++]=str[j];
		b[t]='\0';
		x=strlen(a);
		y=strlen(b);
		s=0;
		if(strcmp(a,b)<0&&x<y)
		{
			if(c=='/')
				cout<<"0"<<endl;
			else if(c=='%')
			{
				cout<<a<<endl;
			}
		}
		else if(strcmp(a,b)==0)
		{
			if(c=='/')
				cout<<"1"<<endl;
			else if(c=='%')
			{
				cout<<"0"<<endl;
			}
		}	
		else
		{
			for(i=0;i<y;i++)
				s=s*10+(b[i]-'0');
			num=0;
			if(c=='/')
			{
				e=0;
				for(i=0;i<x;i++)
				{
					num=num*10+(a[i]-'0');//从高位向低位逐渐的除(例子123/5,1/5=0,* 1%5=1 * ,(1*10+2)/5=2,* 12%5=2 *  ,(2*10+3)/5=4)所以为024
					m=num/s;
					d[e++]=m;
					num%=s;
				}
				for(i=0;i<e;i++)
				{
					if(d[i]!=0)
						break;
				}
				for(j=i;j<e;j++)
					cout<<d[j];
					//printf("%I64d",d[j]);
				cout<<endl;
			}
			else if(c=='%')
			{
				for(i=0;i<x;i++)
				{
					num=num*10+(a[i]-'0');//从高位向低位逐渐的取余(例子123%5, 1%5=1 ,(1*10+2)%5=2,(2*10+3)%5=3)所以为余数3
					num%=s;
				}
				cout<<num<<endl;
				//printf("%I64d\n",num);
			}
			
		//	cout<<s<<endl;
		}
		//cout<<a<<endl<<x<<endl;
		//cout<<b<<endl<<y<<endl;		
	}
	return 0;
}


另一种方法更简洁

#include<stdio.h>
#include<string.h>
int main()
{
	int b,t,len,i,j,k;
	char a[1000],flag;
	while(scanf("%s %c %d",a,&flag,&b)!=EOF)
	{
		len=strlen(a);
		if(flag=='/')
			t=0; //标志作用
		else if(flag=='%')
			t=1;
		k=0;
		j=0;
		for(i=0;i<len;i++)
		{
			k=k*10+(a[i]-'0');
			if((k/b)!=0||(j!=0))
			{
				if(t==0)
					printf("%d",k/b);
				k%=b;
				j=1;//标志除了第一次整除的零不输出外,其余均输出,例如100/5输出的是20而不是020
			}
		}
		if(j==0&&t==0) printf("0");//整除的特殊情况例如2/5的值是零
		if(t!=0) printf("%d",k);
		printf("\n");
	}
	return 0;
}




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值