Having issues implementing the spefiction in the instruction
Having issues implementing the spefiction in the instructions
Problem Assignment: Rational tractions are of the tom a b, where a and b are integers and b f 0. In this exercise, by \"fractions we mean rational fractions. Suppose a b and c/d are fractions. Arithmetic operations on fractions are defined by the following rules: a b c/ d (ad b) bd a b c/ d (ad b)/bd ad bc, where c/ d #0 Fractions are compared as follows: a/b p If ad op bc, where op is any of the relational operations. For example, a bSolution
#include<iostream>
using namespace std;
//Class Fraction definition
class Fraction
{
//Data member for numerator and denominator
int numerator;
int denominator;
public:
//Input redirection operator overloaded
friend ostream& operator<<(ostream &os, const Fraction &f)
{
os<<f.numerator<<\" / \" <<f.denominator;
return os;
}
//Output redirection operator overloaded
friend istream& operator >> (istream &is, Fraction &f)
{
char ch;
is>>f.numerator;
//To discard fraction symbol
is>>ch;
is>>f.denominator;
return is;
}
//Returns true if both numerator and denominator cross multiplication is equal
bool operator ==(Fraction f)
{
return (numerator * f.denominator == denominator * f.numerator);
}
//Returns true if both numerator and denominator cross multiplication is not equal
bool operator != (Fraction f)
{
return (numerator * f.denominator != denominator * f.numerator);
}
//Returns true if both numerator and denominator cross multiplication is less than equal
bool operator <= (Fraction f)
{
return (numerator * f.denominator <= denominator * f.numerator);
}
//Returns true if both numerator and denominator cross multiplication is less
bool operator < (Fraction f)
{
return (numerator * f.denominator < denominator * f.numerator);
}
//Returns true if both numerator and denominator cross multiplication is greater than equal
bool operator >= (Fraction f)
{
return (numerator * f.denominator >= denominator * f.numerator);
}
//Returns true if both numerator and denominator cross multiplication is greater
bool operator > (Fraction f)
{
return (numerator * f.denominator > denominator * f.numerator);
}
//Addition operator overloaded
Fraction operator + (Fraction f)
{
Fraction t;
t.numerator = numerator * f.denominator + f.numerator * denominator;
t.denominator = denominator * f.denominator;
return t;
}
//Multiplication operator overloaded
Fraction operator * (Fraction f)
{
Fraction t;
t.numerator = numerator * f.numerator;
t.denominator = denominator * f.denominator;
return t;
}
//Subtraction operator overloaded
Fraction operator - (Fraction f)
{
Fraction t;
t.numerator = numerator * f.denominator - f.numerator * denominator;
t.denominator = denominator * f.denominator;
return t;
}
//Division operator overloaded
Fraction operator / (Fraction f)
{
Fraction t;
if(f.numerator == 0)
{
cout<<\"\ Cannot divide by zero\";
return *this;
}
else
{
t.numerator = numerator * f.denominator;
t.denominator = f.numerator * f.denominator;
return t;
}
}
//Displays the menu for Operator
void operatorSymbol()
{
cout<<\"\ + for Addition \\t - for Subtraction \\t * for Multiplication \\t / for Division \";
}
};//End of class
//Main method
int main()
{
char sym;
//Creates two Fraction class object
Fraction f1, f2;
//Displays the menu for operator symbol
f1.operatorSymbol();
cout<<\"\ Example First fraction 2/3 + second fraction 3/2\ \";
//Accepts first and second fraction
cout<<\"Enter an First Fraction: \";
cin>>f1;
cout<<\"\ Enter the expression symbol: \";
cin>>sym;
cout<<\"\ Enter an Second Fraction: \";
cin>>f2;
//Creates an object to store result
Fraction t;
if(sym == \'+\')
t = f1 + f2;
else if(sym == \'-\')
t = f1 - f2;
else if(sym == \'*\')
t = f1 * f2;
else if(sym == \'/\')
t = f1 / f2;
else
cout<<\"\ Invalid symbol\";
//Displays the result
cout<<f1<<\" \"<<sym<<f2<<\" = \"<<t;
}//End of main
Output 1:
+ for Addition - for Subtraction * for Multiplication / for Division
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 2/3
Enter the expression symbol: +
Enter an Second Fraction: 3/2
2 / 3 +3 / 2 = 13 / 6
Output 2:
+ for Addition - for Subtraction * for Multiplication / for Division
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 5/4
Enter the expression symbol: -
Enter an Second Fraction: 2/3
5 / 4 -2 / 3 = 7 / 12


