C + + overloaded operator experiment defining fractional class implementation fractional arithmetic

Source: Internet
Author: User
Tags gcd greatest common divisor

The operation of the experiment two custom types

"Experimental Purpose"

    1. Understanding operator functions and operator overloading methods;
    2. Mastering operator overloading is a friend function;

"Experimental Content"

Topic:

In C + +, fractions are not predefined, creating a fractional class that has the following functions: it prevents the denominator from 0, and the numerator and denominator are negative when the fraction is not the simplest form. The addition, subtraction, multiplication, division and other arithmetic are accomplished with overloaded operators.

SOURCE program code:

#include <iostream>

#include <cstdlib>

using namespace Std;

int gcd (int m,int n)//ask for greatest common divisor function

{

if (M < n)

{

int tmp = m;

m = n;

n = tmp;

}

if (n = = 0)

return m;

Else

Return gcd (n,m% n);

}

Class fraction//Build fractions

{

int A, B;

Public

fraction (int x=0,int y=1)//constructor

{

if (y==0)//Determine if the denominator is 0

{

cout<< "Denominator is zero" <<endl;

Exit (0);

}

if (y<0)//denominator is less than 0 o'clock to transfer the minus sign to the molecule

{

A= ( -1) *x;

b= ( -1) *y;

}

A=x;

B=y;

}

Fraction (const FRACTION&F)//Copy Construction

{

A=F.A;

b=f.b;

}

~fraction () {}//destructor

void setfraction (int x,int y)//Reset the fractal value outside the class

{

if (y==0)

{

cout<< "Denominator is zero" <<endl;

Exit (0);

}

if (y<0)

{

A= ( -1) *x;

b= ( -1) *y;

}

A=x;

B=y;

}

void Show ()//output minimal results

{

int flag=1,m;

if (a<0)

{

A=-a;

Flag=-flag;

}

if (b<0)

{

B=-b;

Flag=-flag;

}

M=GCD (A, b);

a=a/m;

b=b/m;

A=a*flag;

if (a==0| | B==1)

cout<<a<<endl;

Else

cout<<a<< "/" <<b<<endl;

}

Friend fraction operator + (fraction & f1,fraction & F2)//with friend function overloading operator

{

return fraction (f1.a * f2.b + f1.b * f2.a, f1.b * f2.b);

}

Friend fraction operator-(fraction & f1,fraction & F2)

{

return fraction (f1.a*f2.b-f1.b*f2.a,f1.b*f2.b);

}

Friend fraction operator * (fraction & f1,fraction & F2)

{

return fraction (f1.a*f2.a,f1.b*f2.b);

}

Friend fraction operator/(fraction & f1,fraction & F2)

{

return fraction (f1.a*f2.b,f1.b*f2.a);

}

};

int main ()

{

int A, B;

cout<< "Please enter the numerator denominator of the first fraction" <<endl;

cin>>a>>b;

int c,d;

cout<< "Please enter the numerator denominator for the second fraction" <<endl;

cin>>c>>d;

Fraction a1,a2,a3,a4,a5,a6;

A1.setfraction (A, b);

A2.setfraction (C,D);

A3=A1+A2;

cout<< "Additive Results" <<endl;

A3.show ();

A4=A1-A2;

cout<< "Subtraction Results" <<endl;

A4.show ();

A5=A1*A2;

cout<< "Multiplication Method Results" <<endl;

A5.show ();

A6=A1/A2;

cout<< "Division method Results" <<endl;

A6.show ();

return 0;

}

Operation Result:

"Summary of the experiment"

    1. After the completion of the operation, the output of the desired result is numerator, so that it can output the simplest results;
    2. A single function can be used to write the simplification part;
    3. When overloaded with operators, the operator before and after the grid makes the format look good.

C + + overloaded operator experiment defining fractional class implementation fractional arithmetic

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.