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/2Sample 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/6Sample 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
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
struct rational{
ll a;
ll b;
}t[2];
ll GCD(ll a,ll b){
a=fabs(a);
b=fabs(b);
return b==0?a:GCD(b,a%b);
}
rational sum(rational aa,rational bb,ll gcd){
ll ta=0,tb=0;
ll k=aa.b*bb.b/gcd;//最小公倍数,也即是分母的值
ta=(aa.a*(k/aa.b))+(bb.a*(k/bb.b));//分子的值
ll m=(GCD(ta,k));
ta=ta/m;
k=k/m;
rational _sum;
_sum.a=ta;
_sum.b=k;
return _sum;
}
rational sumoppose(rational aa,rational bb,ll gcd){
ll ta=0,tb=0;
ll k=aa.b*bb.b/gcd;//最小公倍数,也即是分母的值
ta=(aa.a*(k/aa.b))-(bb.a*(k/bb.b));//分子的值
ll m=(GCD(ta,k));
ta=ta/m;
k=k/m;
rational _sum;
_sum.a=ta;
_sum.b=k;
return _sum;
}
rational product(rational aa,rational bb){
rational _product;
_product.a=aa.a*bb.a;
_product.b=aa.b*bb.b;
return _product;
}
rational productoppose(rational aa,rational bb){
rational _product;
_product.a=aa.a*bb.b;
_product.b=aa.b*bb.a;
return _product;
}
rational yuefen(rational aa){
ll k=GCD(aa.a,aa.b);
aa.a/=k;
aa.b/=k;
return aa;
}
void showresult(rational aa){
if(aa.a<0){
cout<<"(";
}
if(aa.a==0){
cout<<0;
}else if(aa.a%aa.b!=0){
if(aa.b==1){
cout<<aa.a;
}else if(abs(aa.a)>aa.b){
cout<<aa.a/aa.b<<' '<<(ll)abs(aa.a)%aa.b<<"/"<<aa.b;
}else{
cout<<aa.a<<"/"<<aa.b;
}
}else if(aa.a%aa.b==0){
cout<<aa.a/aa.b;
}
if(aa.a<0){
cout<<")";
}
}
int main()
{
rational tt[4];
for(ll i=0;i<2;i++){
ll temp1=0;
ll temp2=0;
char temp3;
cin>>temp1>>temp3>>temp2;
t[i].a=temp1;
t[i].b=temp2;
}
ll k=GCD(t[0].b,t[1].b);
tt[0]=sum(t[0],t[1],k);
tt[1]=sumoppose(t[0],t[1],k);
tt[2]=product(t[0],t[1]);
tt[3]=productoppose(t[0],t[1]);
for(ll i=0;i<4;i++){
tt[i]=yuefen(tt[i]);
if(tt[i].b<0){
tt[i].b*=-1;
tt[i].a*=-1;
}
}
for(ll i=0;i<4;i++){
//ll ttt=i;
switch (i){
case 0 :{
showresult(t[0]);
cout<<" + ";
showresult(t[1]);
cout<<" = ";
showresult(tt[i]);
cout<<endl;
break;
}
case 1 :{
showresult(t[0]);
cout<<" - ";
showresult(t[1]);
cout<<" = ";
showresult(tt[i]);
cout<<endl;
break;
}
case 2 :{
showresult(t[0]);
cout<<" * ";
showresult(t[1]);
cout<<" = ";
showresult(tt[i]);
cout<<endl;
break;
}
case 3 :{
if(t[1].a){
showresult(t[0]);
cout<<" / ";
showresult(t[1]);
cout<<" = ";
showresult(tt[i]);
cout<<endl;
}else{
showresult(t[0]);
cout<<" / ";
showresult(t[1]);
cout<<" = ";
cout<<"Inf";
cout<<endl;
}
break;
}
}
}
return 0;
}