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<cstdio>
#include<iostream>
using namespace std;
long long gcd(long long x, long long y){//计算最大公约数
if (x < y)swap(x, y);
if (x % y == 0)return y;
else return gcd(x - y, y);
}
struct Number{//带分数形式
long long flag;//符号
long long zhengshu;//整数部分
long long fenmu;
long long fenzi;
Number() :flag(0), zhengshu(0), fenmu(1), fenzi(0){}
void print(){//负责打印的成员函数
if (fenmu == 0){//如果分母为0
cout << "Inf";
return;
}
if (zhengshu == 0 && fenzi == 0){//按照分子和整数是否为0分为4种情况
cout << "0";
return;
}
if (flag == -1)cout << "(-";
if (zhengshu == 0 && fenzi != 0){
cout << fenzi << "/" << fenmu;
}
else if (zhengshu != 0 && fenzi == 0){
cout << zhengshu;
}
else if (zhengshu != 0 && fenzi != 0){
cout << zhengshu << " " << fenzi << "/" << fenmu;
}
if (flag == -1)cout << ")";
}
};
struct number{//假分数形式
long long fenzi;
long long fenmu;
number() :fenzi(0), fenmu(0){}
number(long long x, long long y){//假分数的构造函数
if (y < 0){ x = -x; y = -y; }//当y为0时,则把符号转移到放在分子x处
if (x == 0 || y == 0){ //如果其中一个为0,则不能进行gcd计算(因为其中有x%y)所以直接返回
fenzi = x;
fenmu = y;
return;
}
long long tmp = gcd(abs(x), abs(y));//约去最大公约数
x = x / tmp;
y = y / tmp;
fenzi = x;
fenmu = y;
}
};
Number* num2Num(number& num){//假分数到带分数的转换
Number *ret = new Number();
if (num.fenzi < 0){//假分数分子的符号变成带分数中的符号部分
num.fenzi = -num.fenzi;
ret->flag = -1;
}
if (num.fenzi == 0|| num.fenmu == 0){//如果其中一个为0,则直接赋值返回
ret->fenzi = num.fenzi;
ret->fenmu = num.fenmu;
ret->zhengshu = 0;
return ret;
}
if (num.fenzi % num.fenmu == 0){//计算整数部分
long long result = num.fenzi / num.fenmu;
ret->zhengshu = result;
ret->fenmu = 1;
ret->fenzi = 0;
}
else{//计算整数和余下分子部分
ret->zhengshu = num.fenzi / num.fenmu;
ret->fenmu = num.fenmu;
ret->fenzi = num.fenzi - ret->fenmu * ret->zhengshu;
}
return ret;
}
int main(){
freopen("F://Temp/input.txt", "r", stdin);
long long zi1, zi2, mu1, mu2;
scanf("%lld\/%lld %lld\/%lld", &zi1, &mu1, &zi2, &mu2);//格式化输入,比string转化来得方便多了
number* num1 = new number(zi1, mu1);//都大多用到了假分数的构造函数部分,很方便
number* num2 = new number(zi2, mu2);
Number* Num1 = new Number();
Number* Num2 = new Number();
Num1 = num2Num(*num1);
Num2 = num2Num(*num2);
number* num_jia = new number((zi1*mu2 + mu1*zi2), mu1*mu2);//按照公式计算
number* num_jian = new number((zi1*mu2 - mu1*zi2), mu1*mu2);
number* num_cheng = new number(zi1*zi2, mu1*mu2);
number* num_chu = new number(zi1*mu2, zi2*mu1);
Number* Num_jia = new Number();
Number* Num_jian = new Number();
Number* Num_cheng = new Number();
Number* Num_chu = new Number();
Num_jia = num2Num(*num_jia);
Num_jian = num2Num(*num_jian);
Num_cheng = num2Num(*num_cheng);
Num_chu = num2Num(*num_chu);
Num1->print(); cout << " + "; Num2->print(); cout << " = "; Num_jia->print(); cout << endl;
Num1->print(); cout << " - "; Num2->print(); cout << " = "; Num_jian->print(); cout << endl;
Num1->print(); cout << " * "; Num2->print(); cout << " = "; Num_cheng->print(); cout << endl;
Num1->print(); cout << " / "; Num2->print(); cout << " = "; Num_chu->print(); cout << endl;
return 0;
}