Topic:
The problem description is known as a rational number class Zrf_ratio, which implements the following operator overload form:
Friend std::ostream& operator<< (std::ostream&, const zrf_ratio&);//output minimal fraction
Friend std::istream& operator>> (std::istream&, zrf_ratio&);
friend bool operator== (const zrf_ratio&, const zrf_ratio&);
friend bool operator< (const zrf_ratio&, const zrf_ratio&); When testing the test, the main program enters four integers a, B, C, D, representing two fractions of A/b and C/D. Requires the output of the simplest fraction and a comparison of two fractions equal and size. Sample Input 1 7 26 25 sample output ZRF IS:1/7; SSH IS:26/25
(ZRF==SSH) is:0; (ZRF<SSH) Is:1 Blue Bridge Cup is really very broken I submit the code always compile the error point in the evaluation actually in my code appended to the previous submitted code together without words AH!!!!!!!!!!!!!! Always compile error I want to get angry
1#include <iostream>2 using namespacestd;3 classzrf_ratio{4 Public:5 intSon,mom;6Friend std::ostream&operator<< (std::ostream& zout,Constzrf_ratio& z);//Output Minimal fraction7Friend IStream &operator>> (IStream &zin,zrf_ratio &z);8FriendBOOL operator==(ConstZrf_ratio &a,ConstZrf_ratio &b);9FriendBOOL operator< (ConstZrf_ratio &a,ConstZrf_ratio &b);Ten }; Oneostream&operator<< (Ostream &zout,ConstZrf_ratio &z) {//Output Minimal fraction A intx, y;//find the greatest common divisor of x, y -x=Z.son; -y=Z.mom; the inttmp; - if(x<y) {//Guaranteed X>y -tmp=x; -x=y; +y=tmp; - } + //To divide and seek greatest common divisor A Do{ attmp=x%y; -x=y; -y=tmp; -} while(tmp!=0); - //get TMP is greatest common divisor -zout<<z.son/x<<"/"<<z.mom/x; in returnzout; - } toistream&operator>> (IStream &zin,zrf_Ratio&z) { + intx, y; -Zin>>x>>y; thez.son=x; *Z.mom=y; $ returnZin;Panax Notoginseng } - BOOL operator==(Constzrf_ratio& A,Constzrf_ratio&b) { the intAx,ay,bx,by; +ax=A.son; Aay=A.mom; thebx=B.son; +by=B.mom; - returnax*by==bx*ay; $ } $ BOOL operator< (Constzrf_ratio& A,Constzrf_ratio&b) { - intAx,ay,bx,by; -ax=A.son; theay=A.mom; -bx=B.son;Wuyiby=B.mom; the return(Ax*by-bx*ay) * (Ay*by) <0; - } Wu intMain () { - Zrf_ratio SSH,ZRF; AboutCin>>zrf>>ssh; $cout<<"ZRF is:"<<zrf<<"; SSH if:"<<ssh<<Endl; -cout<<"(ZRF==SSH) is:"; - if(ZRF==SSH) cout<<"1"; - Elsecout<<"0"; Acout<<"(ZRF<SSH) is:"; + if(ZRF<SSH) cout<<"1"<<Endl; the Elsecout<<"0"<<Endl; - $}
Blue Bridge Cup Practice system C + + CH08 01