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
原题链接:
https://www.patest.cn/contests/pat-a-practise/1088
https://www.nowcoder.com/pat/5/problem/4036
主要是数字的提取
注意各种特殊情况
注意数据范围,稳妥起见全部使用longlong
CODE:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct S
{
long long fz;
long long fm;
};
long long gcd(long long a,long long b)
{
if ( max(a,b)%min(a,b)==0)
{
return min(a,b);
}
else
{
return gcd(min(a,b),max(a,b)-min(a,b));
}
}
long long lcm(long long a,long long b)
{
return a/gcd(abs(a),abs(b))*b;
}
S pre(string a)
{
S n;
long long fh;
for (long long i=0;i<a.length();i++)
if (a[i]=='/')
{
fh=i;
break;
}
if (a[0]=='-')
{
n.fz=-1*atoi(a.substr(1,fh-1).c_str());
}
else
n.fz=atoi(a.substr(0,fh).c_str());
n.fm=atoi(a.substr(fh+1,a.length()-fh-1).c_str());
if (n.fz!=0)
{
long long gc=gcd(abs(n.fz),n.fm);
n.fz/=gc;
n.fm/=gc;
}
return n;
}
void prt(S a)
{
if (a.fz==0)
{
cout<<"0";
}
else
{
if (a.fz<0)
{
cout<<"(-";
a.fz*=-1;
if (a.fz>=a.fm)
{
cout<<a.fz/a.fm;
a.fz%=a.fm;
if (a.fz!=0)
{
cout<<" "<<a.fz<<"/"<<a.fm;
}
}
else
cout<<a.fz<<"/"<<a.fm;
cout<<")";
}
else
{
if (a.fz>=a.fm)
{
cout<<a.fz/a.fm;
a.fz%=a.fm;
if (a.fz!=0)
{
cout<<" "<<a.fz<<"/"<<a.fm;
}
}
else
cout<<a.fz<<"/"<<a.fm;
}
}
}
void opr(S x,S y,char fh)
{
prt(x);
cout<<" "<<fh<<" ";
prt(y);
cout<<" = ";
long long nfm=lcm(x.fm,y.fm);
long long fz1=x.fz*(nfm/x.fm);
long long fz2=y.fz*(nfm/y.fm);
//cout<<nfm<<" "<<fz1<<" "<<fz2<<endl;
S ad;
if (fh=='+')
{
ad.fz=fz1+fz2;
ad.fm=nfm;
}
if (fh=='-')
{
ad.fz=fz1-fz2;
ad.fm=nfm;
}
if (fh=='*')
{
ad.fz=x.fz*y.fz;
ad.fm=x.fm*y.fm;
}
if (fh=='/')
{
if (y.fz==0)
{
cout<<"Inf";
return;
}
ad.fz=x.fz*y.fm;
ad.fm=x.fm*y.fz;
if (ad.fm<0)
{
ad.fm*=-1;
ad.fz*=-1;
}
}
if (ad.fz!=0)
{
long long sgc=gcd(abs(ad.fz),ad.fm);
ad.fz/=sgc;
ad.fm/=sgc;
prt(ad);
cout<<endl;
}
else
cout<<"0"<<endl;
}
int main()
{
S x,y;
string a,b;
cin>>a>>b;
x=pre(a);
y=pre(b);
//cout<<x.fz<<" "<<x.fm<<endl;
//cout<<y.fz<<" "<<y.fm<<endl;
opr(x,y,'+');
opr(x,y,'-');
opr(x,y,'*');
opr(x,y,'/');
return 0;
}