本题要求编写程序,计算 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
思路
- 用结构体来模拟分数运算
- 两个
int
型变量相乘,结果可能超过int
型承受的范围。所有我的分子分母均采用long long int
- 必须在每一步加法后都进行约分。如果等全部加完后才约分,则会溢出
有些例子容易出错:
2
-1/2 2/4
// Output:
0
2
1/3 -1/2
// Output:
-1/6
代码
#include<cstdio>
#include<algorithm>
using namespace std;
struct Fraction {
long long int up;
long long int down;
};
long long int gcd(long long int, long long int);
Fraction reduction(Fraction result); /* 对分数进行约分 */
Fraction add(Fraction f1, Fraction f2);
Fraction minus(Fraction f1, Fraction f2);
Fraction multiply(Fraction f1, Fraction f2);
Fraction divide(Fraction f1, Fraction f2);
void display(Fraction result); /* 展示Fraction对象 */
int main() {
Fraction number1, number2, result;
scanf("%lld/%lld %lld/%lld", &number1.up, &number1.down, &number2.up, &number2.down);
/** 展示加法 */
display(number1);
printf(" + ");
display(number2);
printf(" = ");
display(add(number1, number2));
printf("\n");
/** 展示减法 */
display(number1);
printf(" - ");
display(number2);
printf(" = ");
display(minus(number1, number2));
printf("\n");
/** 展示乘法 */
display(number1);
printf(" * ");
display(number2);
printf(" = ");
display(multiply(number1, number2));
printf("\n");
/** 展示除法 */
display(number1);
printf(" / ");
display(number2);
printf(" = ");
if(number2.up == 0) {
printf("Inf\n");
}
else {
display(divide(number1, number2));
printf("\n");
}
return 0;
}
/***********************函数实现**********************************/
long long int gcd(long long int m, long long int n) {
if(n == 0)
return m;
else {
int remainder;
while(n) { /* 辗转相除法求最大公因数 */
remainder = m % n;
m = n;
n = remainder;
}
return m;
}
}
Fraction reduction(Fraction result) {
if(result.down < 0) { /* 如果分数是负的,我们统一把负号放到分子 */
result.up = -result.up;
result.down = -result.down;
}
if(result.up == 0) { /* 如果分子为0 */
result.down = 1; /* 令分母为1 */
}
else { /* 如果分子不为0 */
/* 找分子分母的最大公因数 */
int factor = gcd(abs(result.up), abs(result.down));
result.up /= factor; /* 约掉最大公因数 */
result.down /= factor;
}
return result;
}
Fraction add(Fraction f1, Fraction f2) {
Fraction result;
result.up = (f1.up * f2.down) + (f2.up * f1.down);
result.down = f1.down * f2.down;
return reduction(result);
}
Fraction minus(Fraction f1, Fraction f2) {
Fraction result;
result.up = (f1.up * f2.down) - (f2.up * f1.down);
result.down = f1.down * f2.down;
return reduction(result);
}
Fraction multiply(Fraction f1, Fraction f2) {
Fraction result;
result.up = f1.up * f2.up;
result.down = f1.down * f2.down;
return reduction(result);
}
Fraction divide(Fraction f1, Fraction f2) {
Fraction result;
result.up = f1.up * f2.down;
result.down = f1.down * f2.up;
return reduction(result);
}
void display(Fraction result) {
result = reduction(result);
if(result.up < 0)
printf("(");
if(result.up == 0) {
printf("0");
}
else if(result.down == 1) { /* 分母为1,是一个整数 */
printf("%lld", result.up);
}
else if(abs(result.up) > abs(result.down)) { /* 假分数 */
printf("%d %d/%d", result.up / result.down, abs(result.up) % result.down, result.down);
}
else { /* 真分数 */
printf("%d/%d", result.up, result.down);
}
if(result.up < 0)
printf(")");
}