Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an interactive program that performs fractional calculations. You need def

ID: 3625799 • Letter: W

Question

Write an interactive program that performs fractional calculations. You need define a class fracType for a fractional number according to the UML diagram below:
fracType

- numerator : int
- denominator : int
- simplify () : void

+ fracType (int = 0, int = 1)
+ ~fracType()
+ add (const fracType&, const fracType&) : void
+ subtract (const fracType&, const fracType&) : void
+ multiply (const fracType&, const fracType&) : void
+ divide (const fracType&, const fracType&) : void
+ showFrac () const : void
+ setFrac () : void


Where the add, subtract, multiply and divide function perform the corresponding arithmetic calculations on the two parameters, and store the result on the calling object. The parameters are passed as constant reference so it is efficient and the parameters can’t be modified. The two argument constructor also serves as default constructor. The setFrac function prompts and gets user input of numerator and denominator of a fraction.

The simplify function reduces the fraction to least term; it does not care if the numerator is greater than the denominator. It is private so it’s only called by other member functions. The display function is responsible for displaying the fraction properly. If the numerator is greater than denominator, then it displays a whole number followed by a fraction. For example, a fractional number of 2/8 should be reduced to ¼, and 9/6 should be reduced to 3/2, and 3/2 should be displayed as 1 ½.

Here are some more examples:

5/2 should be displayed as 2 1/2
4/4 should be displayed as 1
0/8 should be displayed as 0
4/12 should be displayed as 1/3
________________________________________

Now that you have an ADT for a fractional number, you will use it for the fractional calculation program. The program should first display a menu to let user choose what kind of calculation to perform:

Fraction Calculation Menu:

1 -- ADDITION
2 -- SUBTRACTION
3 -- MULTIPLICATION
4 -- DIVISION
5 -- EXIT
-- >

As long as user do not choose 5, the program prompts for two fractional numbers, perform the selected calculation on these two numbers, and display the result.

When one calculation is finished, the menu is displayed again. Unless user chooses 5, the program shall keep on running. Display the menu and get user choice should be done in a function.

________________________________________

What listed below are basic rules of fractional calculation in mathematics.

N1/D1 and N2/D2 are two fractional numbers, where N1 and N2 are the numerators, D1 and D2 are denominators.

Addition:

N1 N2 N1*D2 + N2*D1 1 3 1 * 5 + 3 * 2 11
----- + ----- = ----------------------- example: --- + --- = ---------------- = ----
D1 D2 D1*D2 2 5 2 * 5 10

Subtraction:

N1 N2 N1*D2 - N2*D1 1 3 1 * 5 - 3 * 2 -1
----- - ----- = ----------------------- example: --- - --- = ---------------- = ----
D1 D2 D1*D2 2 5 2 * 5 10


multiplication:

N1 N2 N1*N2 1 3 1 * 3 3
----- * ----- = -------------- example: --- * --- = ------- = ---
D1 D2 D1*D2 2 5 2 * 5 10

division:

N1 N2 N1*D2 1 3 1 * 5 5
----- / ----- = -------------- example: --- / --- = -------- = ---
D1 D2 D1*N2 2 5 2 * 3 6


The following explains how to simplify a fraction to its least term, where CF stands for common factor:

Simplification:

N *CF N 8 4 * 2 4
------------ = ----- example: ---- = ------- = ----
D * CF D 6 3 * 2 3



Explanation / Answer

please rate - thanks

#include <iostream>
class fracType
{
public:
       fracType(); // default constructor
       void setFrac (int newNum,int newDen);
       fracType Add (fracType otherFraction) const;
       fracType Sub (fracType otherFraction) const;
       fracType Mul (fracType otherFraction) const;
       fracType Div (fracType otherFraction) const;
       void simplify();
       void showFrac() const;

private:
        int numerator;
        int denominator;
};
      


using namespace std;


int main()
{
    bool exit=false;
   
    do
    {
    
    int num;
    int den;
    fracType num1;
    fracType num2;
    fracType result;
    int choice;
    bool error;
    do{
    error=false;
    cout << " What is the numerator of the first fraction? ";
    if(!(cin >> num)) {
            cout << "Please enter numeric characters only. " ;
            cin.clear();
            cin.ignore(10000,' ');
            error=true;
        }
    }while(error);
    do{
    error=false;
    cout << "What is the denominator of the first fraction? ";
     if(!(cin >> den)) {
            cout << "Please enter numeric characters only. " ;
            cin.clear();
            cin.ignore(10000,' ');
            error=true;
        }
    }while(error);
    num1.setFrac(num, den);
    do{
    error=false;
    cout << " What is the numerator of the second fraction? ";
    if(!(cin >> num)) {
            cout << "Please enter numeric characters only. " ;
            cin.clear();
            cin.ignore(10000,' ');
            error=true;
        }
    }while(error);
    do{
    error=false;
    cout << "What is the denominator of the second fraction? ";
     if(!(cin >> den)) {
            cout << "Please enter numeric characters only. " ;
            cin.clear();
            cin.ignore(10000,' ');
            error=true;
        }
    }while(error);
    num2.setFrac(num, den);
    cout << endl;
    do{
    error=false;         
    cout << "Enter a number from 1 to 5 "
    << "1. Displays the sum of the two fractions "
    << "2. Displays the difference "
    << "3. Displays the product "
    << "4. Displays the quotient "
    << "5. Exit "
    << " Selection is ";
    if(!(cin >> choice)) {
            cout << "Please enter numeric characters only. " ;
            cin.clear();
            cin.ignore(10000,' ');
        error=true;
       }
    }while(error);
  
    cout << endl;
    exit=false;

    switch (choice)
           {
           case 1:
                cout << endl;
                num1.showFrac();
                cout << " + ";
                num2.showFrac();
                cout << " = ";
                result = num1.Add(num2);
                result.simplify();
                result.showFrac();
                   cout << endl;
                break;
           case 2:
                num1.showFrac();
                cout << " - ";
                num2.showFrac();
                cout << " = ";
                result = num1.Sub(num2);
                result.simplify();
                result.showFrac();
                cout << endl;
                break;
           case 3:
                num1.showFrac();
                cout << " * ";
                num2.showFrac();
                cout << " = ";
                result = num1.Mul(num2);
                result.simplify();
                result.showFrac();
                cout << endl;
                break;
           case 4:
                num1.showFrac();
                cout << " / ";
                num2.showFrac();
                cout << " = ";
                result = num1.Div(num2);
                result.simplify();
                result.showFrac();
                cout << endl;
                break;
           case 5:
                exit=true;
                break;     
        default: cout << "Not an input between 1 and 7. ";
                break;    
           }
           }while (!exit);
          
system("pause");

}
void fracType::simplify()     //Euclids algorithm
{ int a,b;
a=numerator;
b=denominator;
while(a!=b)
   {if(a>b)
      a -= b;
    else
      b -= a;
   }
numerator/=a;
denominator/=a;
}       
fracType::fracType()
{

numerator = 0;
denominator = 0;

}

void fracType::setFrac(int newNum, int newDen)
{
     numerator = newNum;
     denominator = newDen;
    
}

fracType fracType::Add(fracType otherFraction) const
{
         fracType sum;
         sum.numerator = numerator*otherFraction.denominator+otherFraction.numerator*denominator;
         sum.denominator = denominator*otherFraction.denominator;
         return sum;
}

fracType fracType::Sub(fracType otherFraction) const
{
    fracType difference;
    difference.numerator = numerator*otherFraction.denominator-otherFraction.numerator*denominator;
    difference.denominator = denominator*otherFraction.denominator;
    return difference;
}

fracType fracType::Mul(fracType otherFraction) const
{
    fracType product;
    product.numerator = numerator*otherFraction.numerator;
    product.denominator = denominator*otherFraction.denominator;   
    return product;
}

fracType fracType::Div(fracType otherFraction) const
{
    fracType quotient;
    quotient.numerator = numerator*otherFraction.denominator;
    quotient.denominator = denominator*otherFraction.numerator;
    return quotient;
}


void fracType::showFrac() const
{     if(denominator>numerator)
          cout << numerator << '/' << denominator;
       else
          if(numerator%denominator!=0)
             cout<<numerator/denominator<<" "<<numerator%denominator<<"/"<<denominator;
         else
             cout<<numerator/denominator;
   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote