1088 Rational Arithmetic

题目来源:PAT (Advanced Level) Practice

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

words:

Rational 传统的        Arithmetic 算术,四则运算                implement 实施,施行        basic 基本的

calculate 计算,估算        sum 和        difference 差        product 积        quotient 商

题意:

输入两个假分数形式的分数,计算这两个分数的和、差、积、商,并输出,输出时需要按照带分数的形式输出,负数需要加括号,除数为0时输出Inf;

思路:

1. 定义一个toString()函数用于把假分数形式的分数转化为带分数形式的最简分数,并以字符串形式返回

2. 对给定的两个分数进行加减乘除时对每一个运算分别进行分子和分母的运算,然后使用toString()函数得到计算结果的最终形式,其中除法需要判断除数是否为0,若为0则结果为Inf;

//PAT ad 1088 Rational Arithmetic
#include <iostream>
using namespace std;

typedef long long  ll;

ll gcd(ll a,ll b)	//求a和b的最大公因数
{
	while(b)
	{
		ll c=a%b;
		a=b;
		b=c;
	}
	return a;
}

string toString(ll a,ll b)	//将a/b的分数形式转化为字符串 
{
	ll x=gcd(abs(a),b); 	//求a和b的最大公因数 
	string s;
	if(b/x!=1)
	{
		if(abs(a)<b) 	s=to_string(a/x)+"/"+to_string(b/x);
		else	s=to_string(a/b)+" "+to_string((abs(a)%b)/x)+"/"+to_string(b/x);
	}	
	else
		s=to_string(a/x);
	if(s[0]=='-')	s="("+s+")";	//负数加括号 
	return s;
}

string add(ll a1,ll b1,ll a2,ll b2)	//加法 
{
	ll a=a1*b2+a2*b1;	//分子运算 
	ll b=b1*b2; 		//分母运算 
	return toString(a,b);		//以字符串的形式返回结果 
}

string subtraction(ll a1,ll b1,ll a2,ll b2)	//减法 
{
	ll a=a1*b2-a2*b1;
	ll b=b1*b2; 
	return toString(a,b);		//以字符串的形式返回结果 
}

string multiplication(ll a1,ll b1,ll a2,ll b2)	//乘法 
{
	ll a=a1*a2;
	ll b=b1*b2; 
	return toString(a,b);		//以字符串的形式返回结果 
}

string division(ll a1,ll b1,ll a2,ll b2)	//除法 
{
	ll a=a1*b2;
	ll b=b1*a2; 
	if(b==0)	return "Inf";	//除数为0 
	if(b<0)
	{
		a=-a;b=-b;	//移动负号 
	}
	return toString(a,b);		//以字符串的形式返回结果 
}

int main()
{
	ll a1,b1,a2,b2;
	scanf("%lld/%lld",&a1,&b1);		//输入 
	scanf("%lld/%lld",&a2,&b2);
 
	cout<<toString(a1,b1)<<" + "<<toString(a2,b2)<<" = "<<add(a1,b1,a2,b2)<<endl;				//加 
	cout<<toString(a1,b1)<<" - "<<toString(a2,b2)<<" = "<<subtraction(a1,b1,a2,b2)<<endl;		//减 
	cout<<toString(a1,b1)<<" * "<<toString(a2,b2)<<" = "<<multiplication(a1,b1,a2,b2)<<endl;	//乘 
	cout<<toString(a1,b1)<<" / "<<toString(a2,b2)<<" = "<<division(a1,b1,a2,b2)<<endl;			//除 
			
	return 0;
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值