1088. Rational Arithmetic (20)

题目:

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:

Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2". The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.

Sample Input 1:
2/3 -4/2
Sample Output 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
Sample Input 2:
5/3 0/6
Sample Output 2:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
注意:
1、这大概是20分的题目里面最纠结的一道了,没有之一。
2、用辗转相除法求最小公约数,不然后面几个case会超时。
3、求最简分数的时候要注意(在两个分数相除的时候会出现分母为负或者分子分母都为负的情况):
     1)如果分子分母都为负,则负负得正;
     2)如果分子为正,分母为负,则需要把符号转移到分子上。
4、输出时也要注意几点:
     1)如果分子为0则直接输出0,如果分母为0则直接输出Inf;
     2)分清楚只有整数项、只有分数项和两者皆有这三种情况。
5、我本来是用scanf("%d/%d %d/%d",&a1,&b1,&a2,&b2);来读取输入的,在自己的vs2010环境里面是可以准确读取的,可不知道为什么提交上去总是错误,1081题目里面也是这样做得啊,求大神们的指导。

代码:
//1088
#include<iostream>
using namespace std;

void reduct(long &a,long &b)
{//calculate the irreducible fraction of this fraction.
	if(a==0)
	{
		b=1;
		return;
	}
	else if(b==0)
		return;
	if(a<0&&b<0)
		a=-a,b=-b;
	long x=a,y=b;
	if(a<0&&b>0)
		x=-a;
	//make sure that only numerator can be negative
	if(b<0&&a>0)
		y=b=-b,a=-a;
	while(x%y!=0)
	{
		long z=x%y;
		x=y;
		y=z;
	}
	a/=y;
	b/=y;
}

int main()
{
	long a1,b1,a2,b2;
	cin>>a1;getchar();cin>>b1;
	cin>>a2;getchar();cin>>b2;
	reduct(a1,b1);
	reduct(a2,b2);
	char arith[4]={'+','-','*','/'};
	long a3[4],b3[4];
	//sum
	a3[0]=a1*b2+a2*b1;
	b3[0]=b1*b2;
	//difference
	a3[1]=a1*b2-a2*b1;
	b3[1]=b1*b2;
	//product
	a3[2]=a1*a2;
	b3[2]=b1*b2;
	//quotient
	a3[3]=a1*b2;
	b3[3]=a2*b1;
	for(int i=0;i<4;++i)
	{
		reduct(a3[i],b3[i]);
		long a[3]={a1,a2,a3[i]};
		long b[3]={b1,b2,b3[i]};
		for(int j=0;j<3;++j)
		{
			if(b[j]==0)
				cout<<"Inf";
			else if(a[j]==0)
				cout<<'0';
			else
			{
				int neg=0;//negative number flag
				if(a[j]<0)
				{//negative fraction
					neg=1;
					a[j]=-a[j];
					cout<<"(-";
				}
				if(a[j]%b[j]==0)//only integer part
					cout<<a[j]/b[j];
				else if(a[j]<b[j])//only fraction part
					cout<<a[j]<<'/'<<b[j];
				else//other situations
					cout<<a[j]/b[j]<<' '<<a[j]%b[j]<<'/'<<b[j];
				if(neg)
					cout<<')';
			}
			if(j==0)
				cout<<' '<<arith[i]<<' ';
			if(j==1)
				cout<<" = ";
		}
		cout<<endl;
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值