pat1088. Rational Arithmetic (20)

时间:2021-05-15 17:46:34

1088. Rational Arithmetic (20)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
long long gcd(long long a,long long b){//不能保证返回的符号,但能保证返回的绝对值大小
if(b==){
if(a<){
a=-a;
}
return a;
}
return gcd(b,a%b);
}
long long com;
void output(long long fz1,long long fm1){
if(fz1==){
printf("");
return;
}
com=gcd(fz1,fm1);
fz1/=com;
fm1/=com; //cout<<fz1<<" "<<fm1<<" "<<com<<endl; if(fz1%fm1==){
printf("%lld",fz1/fm1);
}
else{
if(fz1/fm1){
printf("%lld ",fz1/fm1);
if(fz1<){
fz1=-fz1;
}
}
printf("%lld/%lld",fz1-fz1/fm1*fm1,fm1);
}
}
void add(long long fz1,long long fm1,long long fz2,long long fm2){
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
printf(" + ");
if(fz2<){
printf("(");
output(fz2,fm2);
printf(")");
}
else{
output(fz2,fm2);
}
com=gcd(fm1,fm2);
fz2*=fm1/com;
fz1*=fm2/com;
fm1*=fm2/com; //cout<<fz1<<" "<<fm1<<" "<<fz2<<" "<<fm2<<endl; printf(" = ");
fz1+=fz2;
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
}
void sub(long long fz1,long long fm1,long long fz2,long long fm2){
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
printf(" - ");
if(fz2<){
printf("(");
output(fz2,fm2);
printf(")");
}
else{
output(fz2,fm2);
}
com=gcd(fm1,fm2);
fz2*=fm1/com;
fz1*=fm2/com;
fm1*=fm2/com;
printf(" = ");
fz1-=fz2;
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
}
void mul(long long fz1,long long fm1,long long fz2,long long fm2){
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
printf(" * ");
if(fz2<){
printf("(");
output(fz2,fm2);
printf(")");
}
else{
output(fz2,fm2);
}
printf(" = ");
fz1*=fz2;
fm1*=fm2;
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
}
void quo(long long fz1,long long fm1,long long fz2,long long fm2){
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
printf(" / ");
if(fz2<){
printf("(");
output(fz2,fm2);
printf(")");
}
else{
output(fz2,fm2);
}
printf(" = ");
fz1*=fm2;
fm1*=fz2;
if(fm1==){
printf("Inf");
return;
}
if(fm1<){
fm1=-fm1;
fz1=-fz1;
}
if(fz1<){
printf("(");
output(fz1,fm1);
printf(")");
}
else{
output(fz1,fm1);
}
}
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
long long fz1,fm1,inter1,fz2,fm2,inter2;
scanf("%lld/%lld %lld/%lld",&fz1,&fm1,&fz2,&fm2); //cout<<fm2<<endl; com=gcd(fz1,fm1);
fz1/=com;
fm1/=com;
com=gcd(fz2,fm2); //cout<<com<<endl;
//cout<<fm2<<endl;
fz2/=com;
fm2/=com;
//cout<<fm2<<endl;
//计算
//cout<<fz1<<" "<<fm1<<" "<<fz2<<" "<<fm2<<endl;
add(fz1,fm1,fz2,fm2);
printf("\n");
sub(fz1,fm1,fz2,fm2);
printf("\n");
mul(fz1,fm1,fz2,fm2);
printf("\n");
quo(fz1,fm1,fz2,fm2);
printf("\n");
return ;
}