Arithmetic of fractions (C + + overload practice)

Source: Internet
Author: User
* * * fraction of the arithmetic * overloaded operator implementation * Minimal fractional print results  **/#include <iostream> usingnamespacestd; classfenshu//Fractional class {public:intfenzi;//numerator intfenmu;//denominator fenshuinput (INTFZ,INTFM);//Input function voidoutput ();//Output function fenshuoperator+ (fenshu&)//Overload arithmetic fenshuoperator-(fenshu&); fenshuoperator* (fenshu&); fenshuoperator/(fenshu&); }; fenshufenshu::operator+ (fenshu&a)/+ plus overload {Fenshug; g.fenmu=fenmu*a.fenmu; g.fenzi=fenzi*a.fenmu+a.fenzi*fenmu; RETURNG; } fenshufenshu::operator-(Fenshu&a)/minus sign overload {fenshug; g.fenmu=fenmu*a.fenmu; g.fenzi=fenzi*a.fenmu-a.fenzi*fenmu ; RETURNG; } fenshufenshu::operator* (Fenshu&a)//multiplication overload {Fenshug; g.fenmu=fenmu*a.fenmu; g.fenzi=fenzi*a.fenzi; returng;} fenshufenshu::operator/(FENSHU&AMP;A)//division overload {Fenshug; g.fenmu=fenmu*a.fenzi; g.fenzi=fenzi*a.fenmu; returng;} Fenshufenshu::input (INTFZ,INTFM)//input implementation {Fenshua; a.fenzi=fz;a.fenmu=fm; Returna;} voidfenshu::output ()//The most simple fraction output {for (inti=2;i<=fenzi| | i<=fenmu;i++) {if (fenzi%i==0&&fenmu%i==0)//Find molecular divideMother gcd I {fenzi=fenzi/i;//simplified numerator fenmu=fenmu/i;//simplified Denominator}} cout<<fenzi<< "/" <<fenMu<<endl; Print a simplified score} voidmain () {fenshus1,s2,s3; INTFENZI1,FENMU1,FENZI2,FENMU2 cout<< "Enter the numerator (space) denominator of the first score" << Endl cin>>fenzi1; cin>>fenmu1; cout<< "Please enter a second fraction of the numerator (space) denominator" <<endl; cin>>fenzi2; cin>>fenmu2; S1=s1.input (FENZI1,FENMU1); S2=s2.input (FENZI2,FENMU2); Print the fractional plus + method format and the result s3=s1+s2 after the operation; cout<<fenzi1<< "/" <<fenmu1<< "+" <<fenzi2<< "/" <<fenmu2<< "="; S3.output (); Print Fractional subtraction-method format and result S3=s1-s2 after operation; cout<<fenzi1<< "/" <<fenmu1<< "-" <<fenzi2<< "/" <<fenmu2<< "="; S3.output (); Print the fractional multiplication * method format and the result s3=s1*s2 after the operation; cout<<fenzi1<< "/" <<fenmu1<< "*" <<fenzi2<< "/" <<fenmu2<< "="; S3.output (); Print fraction/Fager type and the result of the Operation S3=s1/s2; cout<<fenzi1<< "/" <<fenmu1<< "/" <<fenzi2<< "/" <<fenmu2<< "="; S3.output (); }

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.