Calculation of fractions

Source: Internet
Author: User
Tags gcd greatest common divisor

1. Operator overloading

2, Class of packaging

#include <stdio.h>#include<algorithm>#include<iostream>using namespacestd;intgcdintMintN///Beg Greatest common divisor{    if(n==0)        returnm; Else returnGCD (n,m%n);}classfraction{ Public: fraction (); Fraction (intAintb): molecule (a), denominator (b) {}voidSetData (); voiddisplay (); Fractionoperator* (Fraction &a) {fraction C; intTemp1;///molecule        intTemp2;///Denominator        intL///Greatest Common Divisortemp1= This->molecule*A.molecule; Temp2= This->denominator*A.denominator; L=gcd (TEMP1,TEMP2); C.molecule=temp1/m; C.denominator=temp2/m; returnC; } Fractionoperator+ (Fraction &a) {fraction C; intTemp1;///molecule        intTemp2;///Denominator        intL///Greatest Common Divisortemp1= This->molecule*a.denominator+ This->denominator*A.molecule; Temp2= This->denominator*A.denominator; L=gcd (TEMP1,TEMP2); C.molecule=temp1/L; C.denominator=temp2/m; returnC; } Fractionoperator-(Fraction &a) {fraction C; intTemp1;///molecule        intTemp2;///Denominator        intL///Greatest Common Divisortemp1= This->molecule*a.denominator- This->denominator*A.molecule; Temp2= This->denominator*A.denominator; L=gcd (TEMP1,TEMP2); C.molecule=temp1/m; C.denominator=temp2/m; returnC; } Fractionoperator/(Fraction &a) {fraction C; intTemp1;///molecule        intTemp2;///Denominator        intL///Greatest Common Divisortemp1= This->molecule*A.denominator; Temp2= This->denominator*A.molecule; L=gcd (TEMP1,TEMP2); C.molecule=temp1/m; C.denominator=temp2/m; returnC; }Private:    intmolecule;///molecule    intdenominator;///Denominator}; Fraction::fraction () {molecule=0; Denominator=1;}voidFraction::setdata () {cin>>molecule>>denominator;}voidfraction::d isplay () {cout<<molecule<<"/"<<denominator<<Endl;}intMain () {fraction a,b,c; printf ("This is fraction ' s operation\n"); printf ("Please input your wants fraction ' s operation\n"); Charo; scanf ("%c",&o); System ("CLS"); Switch(o) { Case '+': printf ("Please input fraction ' s molecule and imag\n");        A.setdata ();        B.setdata (); C=a+b;        C.display ();  Break;  Case '-': printf ("Please input fraction ' s molecule and imag\n");        A.setdata ();        B.setdata (); C=a-b;        C.display ();  Break;  Case '*': printf ("Please input fraction ' s molecule and imag\n");        A.setdata ();        B.setdata (); C=a*b;        C.display ();  Break;  Case '/': printf ("Please input fraction ' s molecule and imag\n");        A.setdata ();        B.setdata (); C=a/b;        C.display ();  Break; } System ("Pause"); return 0;}

Calculation of fractions

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.