# 题目

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

# 题目大意

2/3 -4/2

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 *(2)= 1 1/3
2/3/(-2)= 1/3

5/3 0/6

1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf

# 思路分析

## 思路2

gcd函数和化简函数，只要写好了这几个函数，这道题也就解决了。对于其中的四则运算，可以直接在化简函数中进行运算即可。

# 测试点分析

（1） 化简，采用辗转相除法，也就是gcd函数。包括加减乘除的结果也要化简。
（2） 格式化输出，负数要加括号。此处我输出采用的字符串，我觉得比数字输出简单一些。
（3）防止数据溢出。

4/8 4/8
1/-6 1/5
0/3 0/2
1/3 2/10

# 代码

## 代码1（代码量较大）

#include<iostream>
using namespace std;
struct node
{
long long up;
long long down;
bool symbol;
}a, b;
long long gcd(long long a, long long b)
{
return b == 0 ? abs(a) : gcd(b, a%b);
}
node reduction(node a)
{
long long gcdvalue = gcd(abs(a.up), abs(a.down));
if (gcdvalue)
{
a.up = a.up / gcdvalue;
a.down = a.down / gcdvalue;
}
return a;
}
{
a.up = a.up*b.down + b.up*a.down;
a.down = a.down*b.down;
if (a.up*a.down<0) a.symbol = false;
else a.symbol = true;
return reduction(a);
}
node sub(node a, node b)
{
a.up = a.up*b.down - b.up*a.down;
a.down = a.down*b.down;
if (a.up*a.down < 0) a.symbol = false;
else a.symbol = true;
return reduction(a);
}
node multi(node a, node b)
{
a.up = a.up*b.up;
a.down = a.down*b.down;
if (a.up*a.down < 0) a.symbol = false;
else a.symbol = true;
return reduction(a);
}
node div(node a, node b)
{
a.up = a.up*b.down;
a.down = a.down*b.up;
if (a.up*a.down < 0) a.symbol = false;
else a.symbol = true;
return reduction(a);
}
void show(node a)
{
if (a.symbol == false)
{
printf("(");
a.up = -1 * abs(a.up);
a.down = abs(a.down);
}
else
{
a.up = abs(a.up);
a.down = abs(a.down);
}
if (a.up%a.down == 0) printf("%lld", a.up / a.down);
else if (abs(a.up) > abs(a.down)) printf("%lld %lld/%lld", a.up / a.down, abs(a.up%a.down), a.down);
else
{
printf("%lld/%lld", a.up, abs(a.down));
}
if (a.symbol == false) printf(")");
}
int main()
{
scanf("%lld/%lld %lld/%lld", &a.up, &a.down, &b.up, &b.down);
if (a.up*a.down < 0) a.symbol = false;
else a.symbol = true;
if (b.up*b.down < 0) b.symbol = false;
else b.symbol = true;
a=reduction(a);
b=reduction(b);
show(a);
printf(" + ");
show(b);
printf(" = ");
printf("\n");
show(a);
printf(" - ");
show(b);
printf(" = ");
show(reduction(sub(a, b)));
printf("\n");
show(a);
printf(" * ");
show(b);
printf(" = ");
show(reduction(multi(a, b)));
printf("\n");
show(a);
printf(" / ");
show(b);
printf(" = ");
if (div(a, b).down == 0) printf("Inf\n");
else
{
show(reduction(div(a, b)));
printf("\n");
}
return 0;
}


## 代码2

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
long long gcd(long a, long b)
{
return b == 0 ? abs(a) : gcd(b, a%b);
}
string deduction(long long up1, long long down1)
{
string s;
int flag = 0;
long long gcdvalue = gcd(abs(up1), abs(down1));
if (up1 == 0)
{
s = "0";
return s;
}
if (gcdvalue)
{
up1 /= gcdvalue;
down1 /= gcdvalue;
}
if ((up1>0&&down1 < 0)||(up1<0&&down1>0))
{
up1 = (-1)*(abs(up1));
down1 = abs(down1);
s += '(';
s += '-';
flag = 1;
}
if (abs(up1) % abs(down1) == 0)
{
string t;
up1 = abs(up1);
down1 = abs(down1);
s += to_string(abs(up1) / abs(down1));
}
else if (abs(up1) < abs(down1))
{
s += to_string(abs(up1));
s += '/';
s += to_string(abs(down1));
}
else
{
s+=to_string(abs(up1 / down1));
s += ' ';
s += to_string(abs(up1) % down1);
s += '/';
s += to_string(down1);
}
if(flag==1) s += ')';
return s;
}
int main()
{
long long up1, down1, up2, down2;
scanf("%lld/%lld %lld/%lld", &up1, &down1, &up2, &down2);
cout << deduction(up1, down1) << " + " << deduction(up2, down2) << " = " << deduction(up1*down2 + up2 * down1, down1*down2) << endl;
cout << deduction(up1, down1) << " - " << deduction(up2, down2) << " = " << deduction(up1*down2 - up2 * down1, down1*down2) << endl;
cout << deduction(up1, down1) << " * " << deduction(up2, down2) << " = " << deduction(up1*up2, down1*down2) << endl;
cout << deduction(up1, down1) << " / " << deduction(up2, down2) << " = ";
if(up2!=0&&down2!=0) cout<<deduction(up1*down2, down1*up2) << endl;
else cout << "Inf" << endl;
return 0;
}


# 总结

06-07 104

03-14 58

04-30 100

08-21 215

08-18 1065

04-07 38

08-25 133

08-02 52

07-17 75

09-05 76

#### PAT(A) - 1088 Rational Arithmetic(20) -- 分数经典例题

©️2020 CSDN 皮肤主题: 游动-白 设计师: 上身试试

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。