The following is the incomplete header file for the class Fr

//The following is the (incomplete) header file for the class Fraction.
//You must complete the class header and implement all of its functions.

typedef unsigned int uint ;
class Fraction {
uint _numerator ;
uint _denominator ;
bool _isNegative ;
public :
//The sign of the first parameter determines the sign of the
//fraction
Fraction ( int pNum , uint pDen ) ;

//The denominator is assumed to be 1.
Fraction ( int pNum ) ;

//getters
uint numerator ( ) const ;
uint denominator ( ) const ;

//Returns the decimal result of performing the fraction\'s division
float value ( ) const ;

//Add prototypes here for:
//using the + operator to add two fractions.
//using the - operator to subtract two fractions.
//using < and > operators to compare fractions.







}

//Add the prototype here for the insertion operator.

Solution

#include<iostream>

using namespace std;

class fraction

{

private:

int* num;

int* deno;

public:

fraction();

fraction(int,int);

fraction(const fraction&);

void printfrac();

fraction operator+(fraction);

fraction operator-(fraction);

fraction operator*(fraction);

fraction operator/(fraction);

void operator=(fraction);

};

void fraction::printfrac()

{

cout<<*(fraction::num)<<\"/\"<<*(fraction::deno);

}

inline fraction::fraction()

{

this->num=new int(0);

this->deno=new int(0);

}

inline fraction::fraction(int num,int deno)

{

this->num=new int(num);

this->deno=new int(deno);

}

inline fraction::fraction(const fraction&f)

{

fraction::num=new int;

fraction::deno=new int;

*(this->num)=*(f.num);

*(this->deno)=*(f.deno);

}

void swap(int &x,int &y)

{

int tmp=x;

x=y;

y=tmp;

}

int lcm(int l,int s)

{

if(l<s)

swap(l,s);

int m=2,result=l;

while(result%s)

{

result=l*m;

m++;

}

return result;

}

int gcd(int a,int b)

{

if(a<b)

swap(a,b);

while(1)

{

int c=a%b;

if(c==0)

return b;

a=b;

b=c;

}

}

void fraction::operator=(fraction f)

{

*(this->num)=*(f.num);

*(this->deno)=*(f.deno);

}

fraction fraction::operator+(fraction f1)

{

int slcm=lcm(*(f1.deno),*(this->deno));

int num1=*(f1.num)*(slcm/ *(f1.deno));

int num2=*(this->num)*(slcm/ *(this->deno));

int num=num1+num2;

int t=num;

num=num/gcd(num,slcm);

slcm=slcm/gcd(t,slcm);

fraction f(num,slcm);

return f;

}

fraction fraction::operator-(fraction f2)

{

int slcm=lcm(*(this->deno),*(f2.deno));

int num2=*(f2.num)*(slcm / *(f2.deno));

int num1=*(this->num)*(slcm / *(this->deno));

int num=num1-num2;

int t=num;

num=num/gcd(num,slcm);

slcm=slcm/gcd(t,slcm);

fraction f(num,slcm);

return f;

}

fraction fraction::operator*(fraction f1)

{

int num1=*(f1.num)*(*(this->num));

int num2=*(f1.deno)*(*(this->deno));

int t=num1;

num1=num1/gcd(num1,num2);

num2=num2/gcd(t,num2);

fraction f(num1,num2);

return f;

}

fraction fraction::operator/(fraction f2)

{

int num1=*(this->num)*(*(f2.deno));

int num2=*(this->deno)*(*(f2.num));

int t=num1;

num1=num1/gcd(num1,num2);

num2=num2/gcd(t,num2);

fraction f(num1,num2);

return f;

}

int main()

{

int n1,n2,d1,d2,c;

char ch;

while(true)

{

cout<<\"\ Enter numerator and denominator of 1st fraction:\";

cin>>n1>>d1;

cout<<\"\ Enter numerator and denominator of 2nd fraction:\";

cin>>n2>>d2;

fraction f1(n1,d1);

fraction f2(n2,d2);

cout<<\"Fraction 1: \";

f1.printfrac();

cout<<\"\ Fraction 2: \";

f2.printfrac();

cout<<\"\ \";

do

{

cout<<\"\ Menu\ \"<<\"----------------\ \"<<\"1.Addition\ 2.Multiplication\ 3.Subtraction\ 4.Division\ 5.Copy using copy constructor\ 6.Copy using assgnment operator\ \";

cout<<\"\ Enter which operation you want to perform:\";

cin>>c;

switch(c)

{

case 1:

{

fraction res=f1+f2;

cout<<\"\ Result of addition:\";

res.printfrac();

break;

}

case 2:

{

fraction res=f1*f2;

cout<<\"\ Result of multiplication:\";

res.printfrac();

break;

}

case 3:

{

fraction res=f1-f2;

cout<<\"\ Result of subtraction:\";

res.printfrac();

break;

}

case 4:

{

fraction res=f1/f2;

cout<<\"\ Result of division:\";

res.printfrac();

break;

}

case 5:

{

cout<<\"\ New copy of 1st fraction:\";

fraction res1=f1;

res1.printfrac();

cout<<\"\ New copy of 2nd fraction:\";

fraction res2=f2;

res2.printfrac();

break;

}

case 6:

{

cout<<\"\ New copy of 1st fraction:\";

fraction res1;

res1=f1;

res1.printfrac();

cout<<\"\ New copy of 2nd fraction:\";

fraction res2;

res2=f2;

res2.printfrac();

break;

}

default:

cout<<\"\ Invalid choice!!!\";

}

cout<<\"\ Do you want to exit from program(Y/N):\";

cin>>ch;

if(ch==\'Y\'||ch==\'y\')

return 0;

cout<<\"Do you want to continue operation with these two fractions or want to continue with two new fractions(Y/N):\";

cin>>ch;

}while(ch==\'Y\'||ch==\'y\');

}

return 0;

}



//The following is the (incomplete) header file for the class Fraction. //You must complete the class header and implement all of its functions. typedef unsigne
//The following is the (incomplete) header file for the class Fraction. //You must complete the class header and implement all of its functions. typedef unsigne
//The following is the (incomplete) header file for the class Fraction. //You must complete the class header and implement all of its functions. typedef unsigne
//The following is the (incomplete) header file for the class Fraction. //You must complete the class header and implement all of its functions. typedef unsigne
//The following is the (incomplete) header file for the class Fraction. //You must complete the class header and implement all of its functions. typedef unsigne
//The following is the (incomplete) header file for the class Fraction. //You must complete the class header and implement all of its functions. typedef unsigne
//The following is the (incomplete) header file for the class Fraction. //You must complete the class header and implement all of its functions. typedef unsigne

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site