Blue Bridge Cup Practice system C + + CH08 01

Source: Internet
Author: User

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&LT;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&LT;SSH) cout<<"1"<<Endl; the     Elsecout<<"0"<<Endl; -      $}

Blue Bridge Cup Practice system C + + CH08 01

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.