The operation of the experiment two custom types
"Experimental Purpose"
- Understanding operator functions and operator overloading methods;
- 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"
- After the completion of the operation, the output of the desired result is numerator, so that it can output the simplest results;
- A single function can be used to write the simplification part;
- 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