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
题目大意:输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为0,分别在4行中按照“有理数1 运算符 有理数2 = 结果”的格式顺序输出2个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式“k a/b”,其中k是整数部分,a/b是最简分数部分;若为负数,则须加括号;若除法分母为0,则输出“Inf”。题目保证正确的输出中没有超过长整型范围的整数。
分析:计算时全部按照分数进行计算。进行加、减法时,先把分母化为最小公倍数,再将对应分子扩大相应倍数进行加减计算;进行除法时,改为进行倒数乘法。
注意在判断正负时,两个long int整数相乘有可能超过long long的范围。
#include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
using namespace std;long long gcd(long long a,long long b)
{return b==0?a:gcd(b,a%b);
}long long lcm(long long a,long long b)
{return a*b/gcd(a,b);
}void print(long long a,long long b)
{int f=0;if((a<0&&b>0)||(a>0&&b<0))f=1;a=fabs(a),b=fabs(b);if(f)printf("(-");if(a%b==0)printf("%lld",a/b);else{if(a/b!=0)printf("%lld ",a/b),a-=a/b*b;int g=gcd(a,b);printf("%lld/%lld",a/g,b/g);}if(f)printf(")");
}int main(void)
{#ifdef testfreopen("in.txt","r",stdin);//freopen("in.txt","w",stdout);clock_t start=clock();#endif //testlong long a,b,c,d,e,f;scanf("%lld/%lld%lld/%lld",&a,&b,&c,&d);//+print(a,b);printf(" + ");print(c,d);printf(" = ");f=lcm(b,d),e=f/b*a+c*f/d;print(e,f);printf("\n");//-print(a,b);printf(" - ");print(c,d);printf(" = ");f=lcm(b,d),e=f/b*a-c*f/d;print(e,f);printf("\n");//*print(a,b);printf(" * ");print(c,d);printf(" = ");f=b*d,e=a*c;print(e,f);printf("\n");// /print(a,b);printf(" / ");print(c,d);printf(" = ");f=b*c,e=a*d;if(f==0)printf("Inf");else print(e,f);printf("\n");#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位#endif //testreturn 0;
}