For both rational numbers, your task is to implement the basic arithmetics, that's, to calculate their sum, difference, PR Oduct and quotient.
Input Specification:
Each of the input file contains one test case, which gives on one line, and the rational numbers in the format "A1/b1 a2/b2". The numerators and the denominators is all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators is guaranteed to be non-zero numbers.
Output Specification:
For each test case, print in 4 lines the sum, difference, product and quotient of the rational numbers, respectively. The format of each line is "number1 operator number2 = result". Notice that all the rational numbers must being 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 are included in a pair of parentheses. If the denominator in the division is zero, output "INF" as the result. It is guaranteed, the the output integers is 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 = 2/3/0 = INF
#include <stdio.h>#include<stdlib.h>#include<algorithm>using namespacestd;//Beg Greatest common divisorLong LongGCD (Long LongALong Longb) { if(b==0)returnA; Else { returnGCD (b,a%b); } } structfraction{Long LongUp,down; }a,b; Fraction reduction (fraction res) {if(res.down<0) {Res.up=-1*Res.up; Res.down=-1*Res.down; } if(res.up==0) {Res.down=1; }Else { intD=gcd (ABS (RES.UP), ABS (Res.down)); Res.up/=D; Res.down/=D; } returnRes; } fraction Add (fraction a,fraction b) {fraction res; Res.down=a.down*B.down; Res.up=a.up*b.down+a.down*B.up; returnreduction (RES); }fraction MINUSF (fraction a,fraction b) {fraction res; Res.down=a.down*B.down; Res.up=a.up*b.down-a.down*B.up; returnreduction (RES); }fraction multi (fraction a,fraction b) {fraction res; Res.down=a.down*B.down; Res.up=a.up*B.up; returnreduction (RES); }fraction Divide (fraction a,fraction b) {fraction res; Res.down=a.down*B.up; Res.up=a.up*B.down; returnreduction (RES); }voidShow (fraction r) {R=Reduction (R); if(r.up<0) {printf ("("); } if(r.down==1) printf ("%lld", R.up); Else if(ABS (R.UP) >r.down) {printf ("%lld%lld/%lld", R.up/r.down,abs (r.up)%R.down,r.down); }Else{printf ("%lld/%lld", R.up,r.down); } if(r.up<0) {printf (")"); } }intMainintargcChar*argv[]) {scanf ("%lld/%lld%lld/%lld",&a.up,&a.down,&b.up,&B.down); Show (a);p rintf (" + ") Show (b);p rintf (" = "), Show (add (b));p rintf ("\ n"); Show (a);p rintf (" - ") Show (b);p rintf (" = "), Show (MINUSF (b));p rintf ("\ n"); Show (a);p rintf (" * ") Show (b);p rintf (" = "), Show (multi (b));p rintf ("\ n"); Show (a);p rintf (" / ") Show (b);p rintf (" = "); if(b.up==0) printf ("INF"); ElseShow (divide (b));p rintf ("\ n"); System ("Pause"); return 0;}
A1088. Rational Arithmetic (20)