In: Computer Science
Declare and define a class for a fraction number. A fraction in mathematics is defined as a/b, where a and b are integers and called numerator and denominator.
Requirements
Task1
Task2
Name the modified files as newfraction1.h and newfraction1.cpp.
A run of this test program might look like this:
>a.out
Enter the first fraction: numerator denominator
3 5
Enter the second fraction: numerator denominator
2 3
The two fractions entered are
f1 = 3/5
f2 = 2/3
The arithmetic operations on these two fractions:
f1 + f2 = 19/15
f1 - f2 = -1/15
f1 * f2 = 2/5
f1 / f2 = 9/10
Task 3
Redo the Task 2 using operators.
Declare and define a class for a fraction number. A fraction in mathematics is defined as a/b, where a and b are integers and called numerator and denominator.
Requirements
Task1
Task2
Name the modified files as newfraction1.h and newfraction1.cpp.
A run of this test program might look like this:
>a.out
Enter the first fraction: numerator denominator
3 5
Enter the second fraction: numerator denominator
2 3
The two fractions entered are
f1 = 3/5
f2 = 2/3
The arithmetic operations on these two fractions:
f1 + f2 = 19/15
f1 - f2 = -1/15
f1 * f2 = 2/5
f1 / f2 = 9/10
Task 3
Redo the Task 2 using operators.
Code
newfraction2.h
#ifndef NEWFRACTION2_H
#define NEWFRACTION2_H
#include <iostream>
using namespace std;
class fraction
{
friend istream &operator>>(istream&,
fraction&);
friend ostream &operator<<(ostream&,
fraction&);
public:
fraction(int,int);
fraction();
void printfraction();
fraction &operator=(fraction&);
fraction operator+(const fraction& );
fraction operator-(const fraction& );
fraction operator *(const fraction& );
fraction operator /(const fraction& );
void reduction();
private:
int numerator;
int denominator;
};
#endif
newfraction2.cpp
#include"newfraction2.h";
fraction::fraction(int numerator, int denominator)
{
this->numerator = numerator;
//this->denominator = denominator;
if(denominator ==0)
{
this->denominator = 1;
}// end if
reduction();
}
fraction::fraction()
{
this->numerator=0;
this->denominator=1;
}
fraction &fraction::operator=(fraction &number)
{
numerator = number.numerator;
denominator = number.denominator;
return *this;
}
istream &operator>>(istream &in, fraction
&number)
{
char slash;
in >> number.numerator
>> number.denominator;
if(number.denominator == 0)
{
cout<<"invalid denominator/n";
number.denominator = 1;
}
return in;
}
ostream &operator<<(ostream &out, fraction
&number)
{
out << number.numerator
<< '/' << number.denominator;
return out;
}
void fraction::reduction()
{
int largest;
largest = numerator > denominator ? numerator :
denominator;
int gcd = 0; // greatest common divisor
for ( int loop = 2; loop <= largest; loop++ )
if ( numerator % loop == 0 && denominator % loop == 0
)
gcd = loop;
if (gcd != 0)
{
numerator /= gcd;
denominator /= gcd;
} // end if
} // end function reduction
fraction fraction::operator+(const fraction& c1)
{
fraction temp;
temp.numerator=(numerator*c1.denominator)+(c1.numerator*denominator);
temp.denominator=denominator*c1.denominator;
temp.reduction();
return temp;
}
fraction fraction::operator-(const fraction& c1)
{
fraction temp;
temp.numerator=(numerator*c1.denominator)-(c1.numerator*denominator);
temp.denominator=denominator*c1.denominator;
temp.reduction();
return temp;
}
fraction fraction::operator*(const fraction& c1)
{
fraction temp;
temp.numerator=(numerator*c1.numerator);
temp.denominator=denominator*c1.denominator;
temp.reduction();
return temp;
}
fraction fraction::operator/(const fraction& c1)
{
fraction temp;
temp.numerator=(numerator*c1.denominator);
temp.denominator=denominator*c1.numerator;
temp.reduction();
return temp;
}
project1_task3.cpp
#include"newfraction2.h"
int main()
{
fraction f1,f2,f3;
cout<<"Enter firtst fraction: numerator
denominator"<<endl;
cin>>f1;
cout<<"Enter second fraction: numerator
denominator"<<endl;
cin>>f2;
cout<<"Ther two fraction enterd are
"<<endl;
cout<<"f1 = "<<f1<<endl;
cout<<"f2 = "<<f2<<endl;
cout<<"The arithmetic operations on these two
fractions:"<<endl;
f3=f1+f2;
cout<<"f1 + f2 = "<<f3<<endl;
f3=f1-f2;
cout<<"f1 - f2 = "<<f3<<endl;
f3=f1*f2;
cout<<"f1 * f2 = "<<f3<<endl;
f3=f1/f2;
cout<<"f1 / f2 = "<<f3<<endl;
return 1;
}
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.