Simple question.
Note: The scores that are read in may not be the simplest. The output also needs to be converted to the simplest.
#include <cstdio>#include<cstring>#include<cmath>#include<vector>#include<map>#include<stack>#include<queue>#include<string>#include<iostream>#include<algorithm>using namespacestd;structfs{Long LongFZ,FM; FS (Long LongALong Longb) {FZ=A; FM=b; }};Long LonggcdLong LongALong Longb) { if(b==0)returnA; returnGCD (b,a%b);} FS Change (fs res) {if(res.fz!=0) { Long LongGcd=gcd (ABS (RES.FZ), ABS (Res.fm)); RES.FZ=res.fz/GCD; Res.fm=res.fm/GCD; } Else{RES.FZ=0; Res.fm=1; } returnRes;} FS ADD (fs A,fs b) {FS res (0,1); RES.FZ=a.fz*b.fm+b.fz*a.fm; Res.fm=a.fm*b.fm; Res=Change (RES); returnRes;} FS SUB (fs A,fs b) {FS res (0,1); RES.FZ=a.fz*b.fm-b.fz*a.fm; Res.fm=a.fm*b.fm; Res=Change (RES); returnRes;} FS MUL (fs A,fs b) {FS res (0,1); RES.FZ=a.fz*B.FZ; Res.fm=a.fm*b.fm; Res=Change (RES); returnRes;} FS DIV (fs A,fs b) {FS res (0,1); if(b.fz==0) {RES.FZ=0; Res.fm=0; returnRes; } if(a.fz==0)returnRes; RES.FZ=a.fz*b.fm; Res.fm=a.fm*B.FZ; if(res.fm<0) {res.fm=-res.fm; RES.FZ=-RES.FZ; } Res=Change (RES); returnRes;}voidoutput (FS a) {if(a.fm==0) {printf ("INF"); return; } if(a.fz==0) {printf ("0"); return; } A=Change (a); if(ABS (A.FZ) <a.fm) {if(a.fz<0) printf ("("); printf ("%lld/%lld", a.fz,a.fm); if(a.fz<0) printf (")"); return; } if(a.fz>0) { if(a.fz%a.fm==0) {printf ("%lld", a.fz/a.fm); return; } Else{printf ("%lld%lld/%lld", a.fz/a.fm,a.fz%a.fm,a.fm); return; } } Else{A.FZ=-A.FZ; printf ("("); if(a.fz%a.fm==0) {printf ("-%lld", a.fz/a.fm); printf (")"); return; } Else{printf ("-%lld%lld/%lld", a.fz/a.fm,a.fz%a.fm,a.fm); printf (")"); return; } }}intMain () {Long LongS1,s2,s3,s4; scanf ("%lld/%lld%lld/%lld",&s1,&s2,&s3,&S4); FS A (S1,S2); FS b (S3,S4); Output (a); cout<<" + "; Output (b); cout<<" = "; output (ADD (b)); cout<<Endl; Output (a); cout<<" - "; Output (b); cout<<" = "; output (SUB (b)); cout<<Endl; Output (a); cout<<" * "; Output (b); cout<<" = "; output (MUL (A, b)); cout<<Endl; Output (a); cout<<" / "; Output (b); cout<<" = "; output (DIV (A, b)); cout<<Endl; return 0;}
PAT (Advanced level) 1088. Rational Arithmetic (20)