Question

Do not use pointer variables, pointer arithmetic or operater new ITCS-2530    Project                        Write an...

Do not use pointer variables, pointer arithmetic or operater new

ITCS-2530    Project                       

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

CFrac

- numerator : int

- denominator : int

- simplify () : void

+ CFrac (int = 0, int = 1)

+ ~CFrac()

+ add (const CFrac&) const : CFrac

+ subtract (const CFrac&) const : CFrac

+ multiply (const CFrac&) const : CFrac

+ divide (const CFrac&) const : CFrac

+ showFrac () const : void

+ setFrac () : void

Where the arithmetic functions perform the corresponding arithmetic calculations on the current object and the parameter object, and return the result. The subtract function should subtract the parameter object from the current object, and return the difference. Likewise, the divide function should divide the current object by the parameter object and return the quotient. The parameter is passed as constant reference so it is efficient and the parameters can’t be modified. All these four arithmetic functions are constant functions because they do not modify the current object.

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 showFrac 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 does 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. Define a client part function to display the menu and get user choice. The function should return a valid user choice of 1-5

About Spec:

When a program defines and uses a class, the spec separates the class part and user of the class part. The class part is specified in the UML diagram and we will assume it is working accordingly. To lessen your load on this project, I am providing the client part operation below. Please code accordingly.

Operations – level 0:

  1. Display menu options and get user choice – call function menuChoice()
  2. As long as user choice is not 5, do the following
    1. Get input for frac1 and frac2
    2. If choice is 1, add frac2 to frac1
    3. Else if choice is 2, subtract frac2 from frac1
    4. Else if choice is 3, multiply frac1 and frac2
    5. Else divide frac1 by frac2
    6. Display calculation result
    7. Display menu options and get user choice – call function menuChoice
  3. Main ends

Operation – level 1 - int menuChoice()

  1. Display menu options
  2. Get user choice
  3. While user choice is not 1-5
    1. Display message: valid menu choice 1-5 only
    2. Get user choice
  4. Return user choice

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

class CFrac {
private:
int numerator;
int denominator;

public:
void simplify();
CFrac(int numer = 0, int denom = 1);
~CFrac();
CFrac add(const CFrac&) const;
CFrac subtract(const CFrac&) const;
CFrac multiply(const CFrac&) const;
CFrac divide(const CFrac&) const;
void showFrac() const;
void setFrac();
};

void CFrac::simplify()
{

if (numerator < 0 && denominator < 0) { //If both Denominator and Numerator are negative, it equals positive
numerator *= -1;
denominator *= -1;
}
if (numerator < 0) { //If numerator is negative it temporarly makes it positive until end of if-statement
numerator *= -1;
for (int r = denominator * numerator; r > 1; r--) {
if ((denominator % r == 0) && (numerator % r == 0)) {
denominator /= r;
numerator /= r;
}
}
numerator *= -1;
}
if (denominator < 0) {
numerator *= -1; //If denominator is negative it formats it so negative comes before numerator at end
for (int r = denominator * numerator; r > 1; r--) {
if ((denominator % r == 0) && (numerator % r == 0)) {
denominator /= r;
numerator /= r;
}
}
denominator *= -1;
}
else {
// If both positive no changes are made
for (int r = denominator * numerator; r > 1; r--) {
if ((denominator % r == 0) && (numerator % r == 0)) {
denominator /= r;
numerator /= r;
}
}
}
}
CFrac::CFrac(int numer, int denom)
{
this->numerator = numer;
this->denominator = denom;
}
CFrac::~CFrac()
{
}
CFrac CFrac::add(const CFrac& f) const
{
int a = numerator;
int b = denominator;
int c = f.numerator;
int d = f.denominator;
int sumnumer = (a * d + b * c);
int denom = (b * d);
CFrac frac(sumnumer, denom);
frac.simplify();
return frac;
}
CFrac CFrac::subtract(const CFrac& f) const
{
int a = numerator;
int b = denominator;
int c = f.numerator;
int d = f.denominator;
int sumnumer = (a * d - b * c);
int denom = (b * d);
CFrac frac(sumnumer, denom);
frac.simplify();
return frac;
}
CFrac CFrac::multiply(const CFrac& f) const
{
int a = numerator;
int b = denominator;
int c = f.numerator;
int d = f.denominator;
int mulnumer = (a * c);
int muldenom = (b * d);
CFrac frac(mulnumer, muldenom);
frac.simplify();
return frac;
}
CFrac CFrac::divide(const CFrac& f) const
{
int a = numerator;
int b = denominator;
int c = f.numerator;
int d = f.denominator;
int divnumer = (a * d);
int denom = (c * b);
CFrac frac(divnumer, denom);
frac.simplify();
return frac;
}
void CFrac::showFrac() const
{
if (numerator > denominator) {
int a = 0, b = 0;

a = numerator / denominator;

b = numerator - (a * denominator);

cout << a << " " << b << " / " << denominator;
}
else if (numerator == 0) {
cout << denominator;
}
else {
cout << numerator << "/" << denominator;
}
}
void CFrac::setFrac()
{
cout << "Enter Numerator :";
cin >> numerator;
while (true) {
cout << "Denominator :";
cin >> denominator;
if (denominator == 0) {
cout << "** Invalid.Must not br zer **" << endl;
}
else
break;
}
}
int main()
{

int choice, numer, denom;

while (true) {
cout << "\n1 -- ADDITION" << endl;
cout << "2 -- SUBTRACTION" << endl;
cout << "3 -- MULTIPLICATION" << endl;
cout << "4 -- DIVISION" << endl;
cout << "5 -- EXIT" << endl;
cout << "Enter Choice :";
cin >> choice;
switch (choice) {
case 1: {
cout << "Fraction#1:" << endl;
cout << "Enter Numerator :";
cin >> numer;
cout << "Enter Denominator :";
cin >> denom;
CFrac F1(numer, denom);

cout << "Fraction#2:" << endl;
cout << "Enter Numerator :";
cin >> numer;
cout << "Enter Denominator :";
cin >> denom;
CFrac F2(numer, denom);

CFrac res = F1.add(F2);
F1.showFrac();
cout << " + ";
F2.showFrac();
cout << " = ";
res.showFrac();
cout << endl;
continue;
}
case 2: {
cout << "Fraction#1:" << endl;
cout << "Enter Numerator :";
cin >> numer;
cout << "Enter Denominator :";
cin >> denom;
CFrac F1(numer, denom);

cout << "Fraction#2:" << endl;
cout << "Enter Numerator :";
cin >> numer;
cout << "Enter Denominator :";
cin >> denom;
CFrac F2(numer, denom);

CFrac res = F1.subtract(F2);
F1.showFrac();
cout << " - ";
F2.showFrac();
cout << " = ";
res.showFrac();
cout << endl;
continue;
}
case 3: {
cout << "Fraction#1:" << endl;
cout << "Enter Numerator :";
cin >> numer;
cout << "Enter Denominator :";
cin >> denom;
CFrac F1(numer, denom);

cout << "Fraction#2:" << endl;
cout << "Enter Numerator :";
cin >> numer;
cout << "Enter Denominator :";
cin >> denom;
CFrac F2(numer, denom);

CFrac res = F1.multiply(F2);
F1.showFrac();
cout << " * ";
F2.showFrac();
cout << " = ";
res.showFrac();
cout << endl;
continue;
}
case 4: {
cout << "Fraction#1:" << endl;
cout << "Enter Numerator :";
cin >> numer;
cout << "Enter Denominator :";
cin >> denom;
CFrac F1(numer, denom);

cout << "Fraction#2:" << endl;
cout << "Enter Numerator :";
cin >> numer;
cout << "Enter Denominator :";
cin >> denom;
CFrac F2(numer, denom);

CFrac res = F1.divide(F2);
F1.showFrac();
cout << " / ";
F2.showFrac();
cout << " = ";
res.showFrac();
cout << endl;
continue;
}

case 5: {

break;
}

default: {
cout << "** Invalid Choice **" << endl;
continue;
}
}
break;
}

return 0;
}

________________________

Output:


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Do not use pointer variables, pointer arithmetic or operater new ITCS-2530    Project                        Write an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...

    C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...

  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • Create a procedure driven program for performing arithmetic with fractions. Using a switch statement in main...

    Create a procedure driven program for performing arithmetic with fractions. Using a switch statement in main to perform the various actions. Similar to this; Menu a) Load/Reload first fraction b) Load/Reload second fraction c) Add two fractions and display answer d) Subtract two fractions and display answer e) Multiply two fractions and display answer f) Divide two fractions and display answer g) Exit Provide functions that allow for The reloading of the fractions. The addition of two rational numbers. The...

  • In this project, you will use functions and dictionaries to track basketball players and their respective...

    In this project, you will use functions and dictionaries to track basketball players and their respective points, then display statistics of points made. You will need three functions as follows: def freeThrowMade(playerDictionary, playerName) - this function will add 1 point to the player's total score def twoPointMade(playerDictionary, playerName) - this function will add 2 points to the player's total score def threePointMade(playerDictionary, playerName) - this function will add 3 points to the player's total score Each of these functions has...

  • C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers...

    C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • Need help with Java for Fraction exercise

    Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...

  • DO NOT USE ARRAYS IF YOU KNOW ABOUT ARRAYS, I WANT YOU TO USE 3 VARIABLES,...

    DO NOT USE ARRAYS IF YOU KNOW ABOUT ARRAYS, I WANT YOU TO USE 3 VARIABLES, IT WILL FORCE YOU TO USE COMPOUND LOGICAL STATEMENTS FOR THE IF STATEMENTS. Create a program that will display a menu to the user. The choices should be as follows: Enter 3 grades Show average (with the 3 grades) and letter grade Show highest grade Show lowest grade Exit If you want to put the menu into a function you may. The program should...

  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT