本题要求编写程序,计算 2 个有理数的和、差、积、商。
输入格式:
输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为 0。
输出格式:
分别在 4 行中按照 有理数1 运算符 有理数2 = 结果 的格式顺序输出 2 个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式 k a/b,其中 k 是整数部分,a/b 是最简分数部分;若为负数,则须加括号;若除法分母为 0,则输出 Inf。题目保证正确的输出中没有超过整型范围的整数。
输入样例 1:
2/3 -4/2
输出样例 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
输入样例 2:
5/3 0/6
输出样例 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<cmath>
using namespace std;
struct Num {
long up;
long down;
};
long greatest_common_factor(long x,long y);
long lowest_common_multiple(long x, long y);
void simplify(Num &x);
void display(Num x, Num y, Num z, char t);
void display_detail(Num x, bool end);
Num add(Num x, Num y);
Num minus_(Num x, Num y);
Num multiply(Num x, Num y);
Num divide(Num x, Num y);
long greatest_common_factor(long x,long y) { //最大公因数
x = abs(x);
y = abs(y);
long max = x >= y ? x : y;
long min = x <= y ? x : y;
long j;
while(max % min!=0){
j = max % min;
max = min;
min = j;
}
return min;
}
long lowest_common_multiple(long x, long y) { //最小公倍数
long i = greatest_common_factor(x, y);
return x * y / i;
}
void simplify(Num &x) { //化简
if (x.up == 0||x.down==0) {
x.down = 1;
}
else {
long i = greatest_common_factor(x.up,x.down);
x.up /= i;
x.down /= i;
}
}
void display(Num x, Num y, char t) { //输出
display_detail(x, false);
cout << ' ' << t << ' ';
display_detail(y, false);
Num z;
bool end = false;
if (t == '+') {
z = add(x, y);
}
else if (t == '-') {
z = minus_(x, y);
}
else if (t == '*') {
z = multiply(x, y);
}
else if (t == '/') {
z = divide(x, y);
if (y.up == 0) {
end = true;
}
}
simplify(z);
cout << " = ";
display_detail(z, end);
cout << endl;
}
void display_detail(Num x, bool end) { //单个数
simplify(x);
if (end == true) {
cout << "Inf";
return;
}
if (x.up < 0) {
cout << '(';
}
if (x.up%x.down==0) {
cout << x.up / x.down;
}
else if (abs(x.up) > x.down) {
long i = x.up / x.down;
x.up = x.up % x.down;
cout << i << ' ' << abs(x.up) << '/' << x.down;
}
else {
cout << x.up << '/' << x.down;
}
if (x.up < 0) {
cout << ')';
}
}
Num add(Num x, Num y) { //加法
long i = lowest_common_multiple(x.down, y.down);
x.up *= i / x.down;
y.up *= i / y.down;
x.down = y.down = i;
Num z;
z.up = x.up + y.up;
z.down = i;
simplify(z);
return z;
}
Num minus_(Num x, Num y) { //减法
long i = lowest_common_multiple(x.down, y.down);
x.up *= i / x.down;
y.up *= i / y.down;
x.down = y.down = i;
Num z;
z.up = x.up - y.up;
z.down = i;
simplify(z);
return z;
}
Num multiply(Num x, Num y) { //乘法
Num z;
z.up = x.up * y.up;
z.down = x.down*y.down;
simplify(z);
return z;
}
Num divide(Num x, Num y) { //除法
Num z;
z.up = x.up * y.down;
z.down = x.down*y.up;
if (z.down < 0) {
z.up = -z.up;
z.down = -z.down;
}
simplify(z);
return z;
}
int main() {
Num A, B;
scanf_s("%d/%d %d/%d", &A.up, &A.down, &B.up, &B.down);
simplify(A);
simplify(B);
display(A, B, '+');
display(A, B, '-');
display(A, B, '*');
display(A, B, '/');
system("pause");
return 0;
}