Define a class called fraction that will hold a numerator, denominator, and the
ID: 3752510 • Letter: D
Question
Define a class called fraction that will hold a numerator, denominator, and the decimal representation of the fraction. The class should define a default constructor that will set the numerator and the denominator to 1 and the correct decimal representation. There should be another constructor that will accept the numerator and denominator as parameters. To set the values for the constructors, use an initialization section. rite overloaded fraction operators to add, subtract, multiple, and divide fractions. Write an overloaded cout operator that will print out the fraction as xly. Write an overloaded cin function that will ask for a numerator and denominator of a fraction. You will also write a function that will calculate the decimal equivalent of the fraction. You should also have a function that will reduce the fraction and return the result. The original fraction should still be saved. For example, if the fraction is 6/10, this function should return 3/5, but if the original fraction should still be stored as 6/10. In the main function, you are going to write code that will display the following menu 1. Add fractions 2, Subtract fractions 3. Multiple fractions 4. Divide fractions 5, Reduce a fraction 6. Show the decimal equivalent of a fraction 7. Exit the program For the first 4 cases, the program should ask for the 2 fractions. For 4-6, the program should ask for 1 fraction. Sample output: Enter 1 numerator denominator (space in between) 1 3 Enter 2 numerator denominator: 3 6 Result: S/6 Enter numerator denominator (space in between) 4 6 Result: 2/3 Class member variables: numerator . decimal equivalent Class member functions . Default constructor · Constructor that will accept numerator and denominator Overloaded +,, Overloaded Function that will calculate the decimal equivalent Function that will give the reduced fraction Fraction must be implemented in a class with the above requirements. .No global variables are to be used.Explanation / Answer
Program:
#include <cstdio>
#include <iostream>
using namespace std;
int getGCD(int a,int b){
if(a==0 || b==0){
return 0;
}
if(a==b){
return a;
}
if(a>b){
return getGCD(a-b,b);
}else{
return getGCD(a,b-a);
}
}
int getLCM(int a,int b){
return (a*b)/getGCD(a,b);
}
class Fraction{
public:
int numerator,denominator;
float decimal;
Fraction(int n,int d){
numerator = n;
denominator = d;
calculateDecimal();
}
Fraction(){
numerator = 1;
denominator = 1;
calculateDecimal();
}
Fraction operator +(Fraction &number){
int lcm = getLCM(denominator,number.denominator);
int new_n = (lcm / denominator) + (lcm / number.denominator);
int new_d = lcm;
return Fraction(new_n,new_d);
}
Fraction operator -(Fraction &number){
int lcm = getLCM(denominator,number.denominator);
int new_n = (lcm / denominator) - (lcm / number.denominator);
int new_d = lcm;
return Fraction(new_n,new_d);
}
Fraction operator *(Fraction &number){
int new_n = numerator * number.numerator;
int new_d = denominator * number.denominator;
return Fraction(new_n,new_d);
}
Fraction operator /(Fraction &number){
int new_n = numerator * number.denominator;
int new_d = denominator * number.numerator;
return Fraction(new_n,new_d);
}
friend istream &operator >> (istream &input,Fraction &number){
input >> number.numerator >> number.denominator;
number.calculateDecimal();
return input;
}
friend ostream &operator << (ostream &output,Fraction &number){
output << number.numerator << "/" << number.denominator;
return output;
}
void calculateDecimal(){
decimal = numerator/(float)denominator;
}
float getDecimalValue(){
return decimal;
}
Fraction reducedFraction(){
int gcd = getGCD(numerator,denominator);
return Fraction(numerator/gcd,denominator/gcd);
}
};
int main() {
int choice = 0;
Fraction a,b,c;
while(true){
cout << "1. Add fraction 2. Substarct fraction 3. Multiply fraction 4. Divide fraction 5. Reduce a fraction 6. Show the decimal equivalent of a fraction 7. Exit the program ";
cout << "Enter your choice: ";
cin >> choice;
switch(choice){
case 1:
cout << " Enter 1st numerator denominator (space in between): ";
cin >> a;
cout << " Enter 2nd numerator denominator: ";
cin >> b;
c = a + b;
cout << " Result: ";
cout << c;
break;
case 2:
cout << " Enter 1st numerator denominator (space in between): ";
cin >> a;
cout << " Enter 2nd numerator denominator: ";
cin >> b;
c = a - b;
cout << " Result: ";
cout << c;
break;
case 3:
cout << " Enter 1st numerator denominator (space in between): ";
cin >> a;
cout << " Enter 2nd numerator denominator: ";
cin >> b;
c = a * b;
cout << " Result: ";
cout << c;
break;
case 4:
cout << " Enter 1st numerator denominator (space in between): ";
cin >> a;
cout << " Enter 2nd numerator denominator: ";
cin >> b;
c = a / b;
cout << " Result: ";
cout << c;
break;
case 5:
cout << " Enter numerator denominator (space in between): ";
cin >> a;
c = a.reducedFraction();
cout << " Result: ";
cout << c;
break;
case 6:
cout << " Enter numerator denominator (space in between): ";
cin >> a;
cout << " Result: ";
cout << a.getDecimalValue();
break;
case 7:
return 0;
break;
}
cout << " ";
}
return 0;
}
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.