您的位置:首页 > 其它

1088. Rational Arithmetic (20)

2016-02-18 02:34 405 查看
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

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long lcd(long long  a,long long b)
{
if(a< b)
{
long long t=a;
a=b;
b=t;
}
long long r = a%b;
while(r)
{
a=b;
b=r;
r=a%b;
}
return b;
}
inline long long lcm(long long a,long long b){return a*b/lcd(a,b);}
void print(long long n,long long d)
{
bool sign=false;
if(n < 0)
{
n=-n;
sign=true;
}
long long i=n/d;		//integer
n%=d;					//fraction
if( n != 0)
{
long long divisor=lcd(n,d);
n/=divisor;
d/=divisor;
}
if(sign)
{
if( i == 0)n=-n;
else i=-i;
}
if( i == 0 && n == 0)
{
cout<<0;
}
else if( i != 0 && n == 0)
{
if(i<0)cout<<"("<<i<<")";
else cout<<i;
}
else if( i != 0 && n != 0)
{
if(i<0)cout<<"("<<i<<" "<<n<<"/"<<d<<")";
else cout<<i<<" "<<n<<"/"<<d;
}
else
{
if(n < 0)cout<<"("<<n<<"/"<<d<<")";
else cout<<n<<"/"<<d;
}
}
int main()
{
long long numerator1,denominator1,numerator2,denominator2,res_numerator,res_denominator;
cin>>numerator1;getchar();cin>>denominator1;
cin>>numerator2;getchar();cin>>denominator2;
// +
res_denominator=lcm(denominator1,denominator2);
res_numerator=res_denominator/denominator1*numerator1+res_denominator/denominator2*numerator2;
print(numerator1,denominator1);cout<<" + ";print(numerator2,denominator2);cout<<" = ";print(res_numerator,res_denominator);cout<<endl;
// -
res_numerator=res_denominator/denominator1*numerator1-res_denominator/denominator2*numerator2;
print(numerator1,denominator1);cout<<" - ";print(numerator2,denominator2);cout<<" = ";print(res_numerator,res_denominator);cout<<endl;
// *
res_numerator=numerator1 * numerator2;res_denominator=denominator1*denominator2;
print(numerator1,denominator1);cout<<" * ";print(numerator2,denominator2);cout<<" = ";print(res_numerator,res_denominator);cout<<endl;
// /
if(numerator2 != 0)
{
if(numerator1 * numerator2 >= 0)
{
res_numerator=numerator1*denominator2>0?numerator1*denominator2:-numerator1*denominator2;
res_denominator=denominator1*numerator2>0?denominator1*numerator2:-denominator1*numerator2;
}
else
{
res_numerator=numerator1*denominator2>0?-numerator1*denominator2:numerator1*denominator2;
res_denominator=denominator1*numerator2>0?denominator1*numerator2:-denominator1*numerator2;
}
print(numerator1,denominator1);cout<<" / ";print(numerator2,denominator2);cout<<" = ";print(res_numerator,res_denominator);cout<<endl;
}
else
{print(numerator1,denominator1);cout<<" / ";print(numerator2,denominator2);cout<<" = ";cout<<"Inf";cout<<endl;}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pat