1088 Rational Arithmetic (20)(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/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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
long int a1, b1, a2, b2;
long int gcd(long int a, long int b)
{
if (a == 0)
return 1;
if (b == 0)
return a;
return gcd(b, a%b);
}
void print(long int a, long int b)
{
long int k = a / b;
if (k < 0)
a = -a + b * k;
else
a -= b * k;
if (a == 0)
{
if (k < 0)
cout << "(" << k << ")";
else
cout << k;
}
else if (k == 0)
{
if (a < 0)
cout << "(" << a << "/" << b << ")";
else
cout << a << "/" << b;
}
else
{
if (k < 0)
cout << "(" << k << " " << a << "/" << b << ")";
else
cout << k << " " << a << "/" << b;
}
}
void add()
{
long int a3, b3, t;
a3 = a1 * b2 + a2 * b1;
b3 = b1 * b2;
t = gcd(abs(a3), abs(b3));
a3 /= t;
b3 /= t;
print(a1, b1);
cout << " + ";
print(a2, b2);
cout << " = ";
print(a3, b3);
cout << endl;
}
void sub()
{
long int a3, b3, t;
a3 = a1 * b2 - a2 * b1;
b3 = b1 * b2;
t = gcd(abs(a3), abs(b3));
a3 /= t;
b3 /= t;
print(a1, b1);
cout << " - ";
print(a2, b2);
cout << " = ";
print(a3, b3);
cout << endl;
}
void mul()
{
long int a3, b3, t;
a3 = a1 * a2;
b3 = b1 * b2;
t = gcd(abs(a3), abs(b3));
a3 /= t;
b3 /= t;
print(a1, b1);
cout << " * ";
print(a2, b2);
cout << " = ";
print(a3, b3);
cout << endl;
}
void div()
{
if (a2 == 0)
{
print(a1, b1);
cout << " / 0 = Inf" << endl;
}
else
{
long int a3, b3, t;
if (a2 < 0)
{
a3 = -a1 * b2;
b3 = -b1 * a2;
}
else
{
a3 = a1 * b2;
b3 = b1 * a2;
}
t = gcd(abs(a3), abs(b3));
a3 /= t;
b3 /= t;
print(a1, b1);
cout << " / ";
print(a2, b2);
cout << " = ";
print(a3, b3);
cout << endl;
}
}
int main()
{
char ch;
cin >> a1 >> ch >> b1 >> a2 >> ch >> b2;
long int t;
t = gcd(abs(a1), abs(b1));
a1 /= t;
b1 /= t;
t = gcd(abs(a2), abs(b2));
a2 /= t;
b2 /= t;
add();
sub();
mul();
div();
return 0;
}