Rational Arithmetic

来源:互联网 发布:nginx获取header信息 编辑:程序博客网 时间:2024/05/18 15:27

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/32/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/31 2/3 - 0 = 1 2/31 2/3 * 0 = 01 2/3 / 0 = Inf

提交代码

#include<stdio.h>#include<algorithm>using namespace std;typedef long long ll;ll gcd(ll a,ll b){if(b==0) return a;else return gcd(b,a%b);}struct Fraction{ll up,down;};Fraction reduction(Fraction result){if(result.down<0){result.down =-result.down;result.up=-result.up;}if(result.up==0){result.down=1;}else{ll d=gcd(abs(result.up),abs(result.down));result.up=result.up/d;result.down=result.down/d;}return result;}Fraction add(Fraction a,Fraction b){Fraction result;result.up=a.up*b.down+a.down*b.up;result.down=a.down*b.down;return  reduction(result);}Fraction minu(Fraction a,Fraction b){Fraction result;result.up=a.up*b.down-a.down*b.up;result.down=a.down*b.down;return  reduction(result);}Fraction multi(Fraction a,Fraction b){Fraction result;result.up=a.up*b.up;result.down=a.down*b.down;return  reduction(result);}Fraction divide(Fraction a,Fraction b){Fraction result;result.up=a.up*b.down;result.down=a.down*b.up;return  reduction(result);}void prin(Fraction a){a=reduction(a);if(a.up<0) printf("(");if(a.down==1){printf("%lld",a.up);}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,a.down);}if(a.up<0) printf(")");}Fraction choose(Fraction a,Fraction b,char c){if(c=='+'){    return add(a,b);}else if(c=='-'){return minu(a,b);}else if(c=='*'){return multi(a,b); }  } int main(){Fraction temp1,temp2;scanf("%lld/%lld %lld/%lld",&temp1.up,&temp1.down,&temp2.up,&temp2.down);temp1=reduction(temp1);temp2=reduction(temp2);char str[4]={'+','-','*','/'};for(int i=0;i<3;i++){prin(temp1);printf(" %c ",str[i]);prin(temp2);printf(" = ");prin(choose(temp1,temp2,str[i]));printf("\n");}prin(temp1);printf(" %c ",str[3]);prin(temp2);printf(" = ");if(temp2.up==0) printf("Inf");else prin(divide(temp1,temp2));printf("\n");return 0;}


原创粉丝点击