PAT A 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、负号全都出现在分子项上(除法会改变负号位置)

2、有负号都需要加括号

3、是负数,且有整数部分,分数部分不能再加负号

4、数本身是零

其他只需要约简,计算,再约简,获取整数和分数部分输出即可。

 

代码:

#include <iostream>
#include <cmath>
using namespace std;

int factor(long a,long b);	//求公因数
void put(long a,long b,long c);		//按格式要求输出

int main()
{
	long a1,b1,a2,b2;	//输入数据
	cin>>a1;cin.get();
	cin>>b1;
	cin>>a2;cin.get();
	cin>>b2;

	long fact=factor(a1,b1);	//求最大公因数,化简
	a1/=fact;
	b1/=fact;
	fact=factor(a2,b2);
	a2/=fact;
	b2/=fact;

	long c11=a1/b1,a11=a1%b1,c22=a2/b2,a22=a2%b2;	//求带整数的格式

	long aa,bb,cc;	//运算结果
	put(a11,b1,c11);	//加法
	cout<<" + ";
	put(a22,b2,c22);
	cout<<" = ";
	bb=b1*b2;
	aa=a1*b2+a2*b1;
	fact=factor(aa,bb);	//约简输出
	aa/=fact;
	bb/=fact;
	cc=aa/bb;
	aa%=bb;
	put(aa,bb,cc);
	cout<<endl;

	put(a11,b1,c11);	//减法
	cout<<" - ";
	put(a22,b2,c22);
	cout<<" = ";
	bb=b1*b2;
	aa=a1*b2-a2*b1;
	fact=factor(aa,bb);	//约简输出
	aa/=fact;
	bb/=fact;
	cc=aa/bb;
	aa%=bb;
	put(aa,bb,cc);
	cout<<endl;

	put(a11,b1,c11);	//乘法
	cout<<" * ";
	put(a22,b2,c22);
	cout<<" = ";
	bb=b1*b2;
	aa=a1*a2;
	fact=factor(aa,bb);	//约简输出
	aa/=fact;
	bb/=fact;
	cc=aa/bb;
	aa%=bb;
	put(aa,bb,cc);
	cout<<endl;

	put(a11,b1,c11);	//除法
	cout<<" / ";
	put(a22,b2,c22);
	cout<<" = ";
	if(a2==0)	//非法
		cout<<"Inf\n";
	else	//合法
	{
		bb=b1*a2;
		aa=a1*b2;
		if(aa<0&&bb<0||aa>0&&bb<0)	//调整符号
		{
			aa*=-1;
			bb*=-1;
		}
		fact=factor(aa,bb);	//约简输出
		aa/=fact;
		bb/=fact;
		cc=aa/bb;
		aa%=bb;
		put(aa,bb,cc);
		cout<<endl;
	}

	return 0;
}

int factor(long a,long b)
{
	if(a%b==0)
		return b;
	if(b%a==0)
		return abs(a);
	long fact=1;	//因数
	long limit=(long)sqrt((double)min(abs(a),abs(b))+2);	//限制
	for(long i=2;i<=limit;i++)
	{
		if(a%i==0&&b%i==0)
			while(a%i==0&&b%i==0)
			{
				a/=i;
				b/=i;
				fact*=i;
			}
	}
	return fact;
}

void put(long a,long b,long c)
{
	int flag=0;
	if(c<0||a<0)
		flag=1;
	if(flag==1)
		cout<<"(";
	if(c==0&&a==0)
	{
		cout<<0;
		return;
	}
	if(c!=0)
	{
		cout<<c;
		if(a!=0)
			cout<<" ";
	}
	if(a!=0)
	{
		if(c<0)
			cout<<abs(a)<<"/"<<b;
		else
			cout<<a<<"/"<<b;
	}
	if(flag==1)
		cout<<")";
}


 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值