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
神坑:倒数第二个节点容易超long long int的范围,但可以先通分(同除以最大公因数),再进行后面的计算。
#include<iostream>
#include<cstring>
using namespace std;
string result_form(long long int a,long long int b){
string s = "";
long long int m = abs(a) / abs(b);
if(m == 0){
if(a == 0){
s = "0";
}
if(a > 0){
s = to_string(a) + "/" + to_string(b);
}
if(a < 0){
s = "(" + to_string(a) + "/" + to_string(b) + ")";
}
}else
{
if(a % b == 0){
if(a > 0){
s = to_string(m);
}
if(a < 0){
s = "(" + to_string(-m) + ")";
}
}
if(a % b > 0){
s = to_string(m) + " " + to_string(abs(a) - b * m) + "/" + to_string(b);
}
if(a % b < 0){
s = "(" + to_string(-m) + " " + to_string(abs(a) - b * m) + "/" + to_string(b) + ")";
}
}
return s;
}
//最大公约数
long long int gcd(long long int a,long long int b)
{
long long int temp_max = max(a,b);
long long int temp_min = min(a,b);
if(temp_max % temp_min == 0)
return temp_min;
else
return gcd(temp_min, temp_max % temp_min);
}
long long int a1, b1, a2, b2;
string result_output(long long int a,long long int b){
if(a == 0 && (a1 == 0 || a2 == 0)){
return "0";
}
long long int temp_gcd = gcd(abs(a), abs(b));
return result_form(a / temp_gcd, b / temp_gcd);
}
string add(){
return result_output(a1 * b2 + a2 * b1 , b1 * b2);
}
string sub(){
return result_output(a1 * b2 - a2 * b1 , b1 * b2);
}
string mul(){
return result_output(a1 * a2 , b1 * b2);
}
string div(){
if(a2 == 0){
return "Inf";
}else
{
if(a2 > 0){
return result_output(a1 * b2 , b1 * a2);
}
if(a2 < 0){
return result_output(- a1 * b2 , - b1 * a2);
}
}
return "";
}
int main(){
scanf("%lld/%lld %lld/%lld", &a1, &b1, &a2, &b2);
printf("%s + %s = %s\n", result_output(a1, b1).c_str(), result_output(a2, b2).c_str(), add().c_str());
printf("%s - %s = %s\n", result_output(a1, b1).c_str(), result_output(a2, b2).c_str(), sub().c_str());
printf("%s * %s = %s\n", result_output(a1, b1).c_str(), result_output(a2, b2).c_str(), mul().c_str());
printf("%s / %s = %s\n", result_output(a1, b1).c_str(), result_output(a2, b2).c_str(), div().c_str());
// cout << result_form(a1, b1) << " + " << result_form(a2, b2) << " = " << add() << endl;
// cout << result_form(a1, b1) << " - " << result_form(a2, b2) << " = " << sub() << endl;
// cout << result_form(a1, b1) << " * " << result_form(a2, b2) << " = " << mul() << endl;
// cout << result_form(a1, b1) << " / " << result_form(a2, b2) << " = " << div() << endl;
return 0;
}