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
Solution:
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int gcd(long long a, long long b){
if(a < b) swap(a, b);
if(a % b == 0) return b;
return gcd(b, a % b);
}
struct Num{
long long numerator;
long long denominator;
Num(long long n, long long d){
if(n == 0){
numerator = 0;
denominator = 1;
return;
}
long long tmp = gcd(abs(n), d);
numerator = n / tmp;
denominator = d / tmp;
}
~Num(){}
};
Num sum(Num &a, Num &b){
long long numerator = a.numerator * b.denominator + a.denominator * b.numerator;
long long denominator = a.denominator * b.denominator;
Num ans(numerator, denominator);
return ans;
}
Num difference(Num &a, Num &b){
long long numerator = a.numerator * b.denominator - a.denominator * b.numerator;
long long denominator = a.denominator * b.denominator;
Num ans(numerator, denominator);
return ans;
}
Num product(Num &a, Num &b){
long long numerator = a.numerator * b.numerator;
long long denominator = a.denominator * b.denominator;
Num ans(numerator, denominator);
return ans;
}
Num quotient(Num &a, Num &b){
long long numerator = a.numerator * b.denominator;
long long denominator = a.denominator * b.numerator;
if(denominator < 0){
numerator = -numerator;
denominator = -denominator;
}
Num ans(numerator, denominator);
return ans;
}
string numToString(Num &n){
string ans = "";
long long integer, fractional;
if(n.denominator == 1) ans = to_string(n.numerator);
else{
if(abs(n.numerator) < n.denominator) ans = to_string(n.numerator) + "/" + to_string(n.denominator);
else{
integer = n.numerator / n.denominator;
fractional = n.numerator % n.denominator;
ans = to_string(integer) + " " + to_string(abs(fractional)) + "/" + to_string(n.denominator);
}
}
if(ans[0] == '-') ans = "(" + ans + ")";
return ans;
}
int main(){
int an, ad, bn, bd;
int integer, fractional;
scanf("%d/%d %d/%d", &an, &ad, &bn, &bd);
Num n1((long long)an, (long long)ad), n2((long long)bn, (long long)bd);
string n3, n4, ans;
n3 = numToString(n1);
n4 = numToString(n2);
Num ans1 = sum(n1, n2);
ans = numToString(ans1);
printf("%s + %s = %s\n", n3.c_str(), n4.c_str(), ans.c_str());
Num ans2 = difference(n1, n2);
ans = numToString(ans2);
printf("%s - %s = %s\n", n3.c_str(), n4.c_str(), ans.c_str());
Num ans3 = product(n1, n2);
ans = numToString(ans3);
printf("%s * %s = %s\n", n3.c_str(), n4.c_str(), ans.c_str());
if(n2.numerator == 0) printf("%s / %s = Inf\n", n3.c_str(), n4.c_str());
else{
Num ans4 = quotient(n1, n2);
ans = numToString(ans4);
printf("%s / %s = %s\n", n3.c_str(), n4.c_str(), ans.c_str());
}
return 0;
}
|