Question

I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational...

I couldn't find where to comment. But it has to be written in C++!!!!!

Task-1: Rational fractions are of the form a / b, in which a and b are integers and ! ≠ 0. In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose a / b and c / d are fractions. Arithmetic operations and relational operations on fractions are defined by the following rules:

Arithmetic Operations:

a/b + c/d = (ad + bc)/bd

a/b – c/d = (ad – bc)/bd

a/b x c/d = ac/bd

(a/b)/(c/d) = ad/bc, in which c/d ≠0

Relational Operations:

(a/b<=c/d) = (ad<=bc)

(a/b>=c/d) = (ad>=bc)

(a/b==c/d) = (ad==bc)

(a/b!=c/d) = (ad!=bc)

Fractions are compared as follows: a / b op c / d if ad op bc, in which op is any of the relational operations. For example, a / b < c / d if ad < bc. In this assignment, you will need to design a class— say, fractionType which performs the arithmetic and relational operations on fractions. Overload the arithmetic and relational operators so that the appropriate symbols can be used to perform the operation. Also, overload the stream insertion and stream extraction operators for easy input and output. Write a C++ program that, using the class fractionType, performs operations on fractions. Among other things, test the following: Suppose x, y, and z are objects of type fractionType. If the input is 2/ 3, the statement: cin >> x; should store 2 / 3 in x. The statement: cout << x + y << endl; should output the value of x + y in fraction form. The statement: z = x + y; should store the sum of x and y in z in fraction form. You are given the main driver for this program “MainProgram.cpp”. A sample run of the program is provided below:

//Sample run of the prgram

Line 7: Num1=5/6

Line 8: Num2 = 0/1

Line 9: Enter the fraction in the form a/b:

Line 7: Num1=5/6

Line 8: Num2 = 0/1

Line 9: Enter the fraction in the form a/b: 23/27

Line 12: New value of num2 = 23/27

Line 14: Num3 = 273 / 162

Line 15: 5/6 + 23/27 =273/162

Line 16: 5/6 * 23/27= 115/162

Line 18: Num3 = -3/162

Line 19: 5/6 - 23/27=-3/162

Line 20:(5/6)/(23/27)=135/138

Process returned 0 (0x0) execution time : 88.345 s

Press any key to continue.

Comment:In the provided run, it was assumed that the user of the program will be asked to enter only one fraction which is the value for num2. The value of num1 was always assumed to be 5/6.

//“MainProgram.cpp”

#include
#include
#include "fractionType.h"

using namespace std;

int main()
{
fractionType num1(5, 6); //Line 1
fractionType num2; //Line 2
fractionType num3; //Line 3

cout << fixed; //Line 4
cout << showpoint; //Line 5
cout << setprecision(2); //Line 6

cout << "Line 7: Num1 = " << num1 << endl; //Line 7
cout << "Line 8: Num2 = " << num2 << endl; //Line 8

cout << "Line 9: Enter the fraction "
       << "in the form a / b: "; //Line 9
cin >> num2; //Line 10
cout << endl; //Line 11

cout << "Line 12: New value of num2 = "
<< num2 << endl;   //Line 12

num3 = num1 + num2; //Line 13

cout << "Line 14: Num3 = " << num3 << endl; //Line 14
cout << "Line 15: " << num1 << " + " << num2
<< " = " << num1 + num2 << endl; //Line 15
cout << "Line 16: " << num1 << " * " << num2
<< " = " << num1 * num2 << endl; //Line 16

num3 = num1 - num2; //Line 17

cout << "Line 18: Num3 = " << num3 << endl; //Line 18
cout << "Line 19: " << num1 << " - " << num2
<< " = " << num1 - num2 << endl; //Line 19
cout << "Line 20: (" << num1 << ") / (" << num2
<< ") = " << num1 / num2 << endl; //Line 20
cout << "Line 21: (" << num1 << ") <= (" << num2
<< ") = " << (num1 <= num2) << endl; //Line 21
cout << "Line 22: (" << num1 << ") == (" << num2
<< ") = " << (num1 == num2) << endl ; //Line 22
cout << "Line 23: (" << num1 << ") >= (" << num2
<< ") = " << (num1 >= num2) << endl ; //Line 23
cout << "Line 24: (" << num1 << ") != (" << num2
<< ") = " << (num1 != num2) << endl ; //Line 24
cout << "Line 25: (" << num1 << ") > (" << num2
<< ") = " << (num1 > num2) << endl; //Line 25
cout << "Line 26: (" << num1 << ") < (" << num2
<< ") = " << (num1 < num2) << endl; //Line 26
   return 0;
}

Task-2: (Application – Ingredient Management System Process)

Assume you were hired by the head chef at a big restaurant (like Freemason Abbey, USA family restaurant, Eleven Madison Park, Paramount, Shaya, etc.) to write a computer program to automate/expedite the ingredient management process. The chef needs to cook two similar dishes. These dishes use the same ingredients with slight differences to accommodate various dietary restrictions:

• Assume you have 3 tomatoes, a garlic, 2 onions, 1 butter bar, 2 lbs of ground beef and 2 lbs of beans.

• The first dish requires a half tomato, 3/4 of a garlic, 1/4 of an onion, 1/8 of a butter bar, 1/2 lbs of ground beef, 2/3 lbs of beans.

• The second dish requires 3/5 of a tomato, 1/5 of a garlic, 2/5 of an onion, 1/5 of butter bar, 7/10 lbs of ground beef, 9/10 lbs of beans.

The chef likes to keep the fridge stocked so you need to have a list of:

• The total amount of each ingredient that you will have left so the chef knows when to order more ingredients.

• Which of these two dishes is going to weigh more than the other (Disregard the weight of tomatoes, garlic, onions and butter) so the chef can change the prices accordingly.

In order to write this program, you could just type the formulas above to find the answers. Or you could create functions that will hold the formulas above and call those. However, the easiest way to do it would be to use the operators you overloaded for Task-1 in the fractionType module. For this assignment, you need to use the overloaded operators.

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

fractionType (int lnum- 0, int lden -1) //Define operator+() fractionType operatort (fractionType lrightFr); //Define operato

//Define post-decrement fractionTvpe operator-- int; //overload relational operators bool operator==(fractionType IrightFr) c

ostream& operator < (ostream& OS, const fractionType& fraction) os << fraction.numerator <</ << fraction.denominator; retur

bool fractionType: :operator(fractionType lrightFr) const //Return result return (numerator * lrightFr.denominator- denominat

return (numerator *lrightFr.denominator < denominator * lrightFr.numerator); //Define operator b001 fractionType: :operator>=

denominator = 1 ; else denominatorldeno; //Define setFraction () void fractionType: : setFraction (int lnum, int ldeno) numer

+ lrightFr.numerator denominator; temp.denominator - denominator * lrightFr.denominator return temp //Overload operator * fra

temp.denominator - denominator * lrightFr.denominator return temp; //Define operator/ () fractionType fractionType:: operator

this->numerator this->numerator +this >denominator this->denominator this->denominator return fractionType (numerator, denomi

fractionType this->numerator this-numerator -this- this->denominator- this->denominator temp-fractionType (numerator, denomin

cout << fixed; //Line 4 cout << showpoint; //Line 5 c ut << Line 7: Num! << num1 << endl; //Line 7 cout << Line 8: Num2 =

cout << Line 15 << num1 <<<< num2 <<<< numl num2 < endl //Line 15 //Display product c ut << Line 16: << num1 << *

<<- << (num1 < num2) << endl; //Line cout << Line 22: ( << num1 << ) == ( << << ) = << (num! == num2 ) << end! ;

Sample Output: Line 7: Num! = 5 Line 8: Num2 Line 9: Enter the fraction in the form a / b: 2/3 Line 12: New value of num2 2 L

Code:

//fractionType.h

//Include libraries

#ifndef H_fraction

//Define fraction

#define H_fraction

//Include library

#include <iostream>

using namespace std;

//Define class fractionType

class fractionType

{

   /*Function prototype*/

   friend ostream& operator<< (ostream&, const fractionType&);

   friend istream& operator>> (istream&, fractionType&);

   public:

//Define setFraction()

   void setFraction(int lnum, int lden);

  

   //Define constructor

   fractionType(int lnum = 0, int lden = 1);

  

   //Define operator+()

   fractionType operator+(fractionType lrightFr);

  

   //Define operator*()

   fractionType operator*(fractionType lrightFr);

  

   //Define operator-()

   fractionType operator-(fractionType lrightFr);

  

   //Define operator/()

   fractionType operator/(fractionType lrightFr);

  

   //overload /

   //increment and decrement operators ++, --

   //Define pre-increment

   fractionType operator++ ();

  

  

   //Define post-increment

   fractionType operator++( int );

   //Define pre-decrement

   fractionType operator-- ();

  

  

   //Define post-decrement

   fractionType operator--( int );

   //Overload relational operators

   bool operator==(fractionType lrightFr) const;

   bool operator!=(fractionType lrightFr) const;

   bool operator<=(fractionType lrightFr) const;

   bool operator<(fractionType lrightFr) const;

   bool operator>=(fractionType lrightFr) const;

   bool operator>(fractionType lrightFr) const;

     //Define variables

     private:

        int numerator;

        int denominator;

};

#endif

//f1.cpp

//Include libraries

#include <iostream>

#include "fractionType.h"

using namespace std;

//Define operator<<

ostream& operator << (ostream& os, const fractionType& fraction)

{

   os << fraction.numerator << " / " << fraction.denominator;

   return os;

}

//Define operator >>

istream& operator>> (istream& is, fractionType& fraction)

{

   char ch;

   //Get numerator

   is >> fraction.numerator;

   //Read and discard

   is >> ch;

   //Get denominator

   is >> fraction.denominator;

   return is;

}

//Define operator==()

bool fractionType::operator==(fractionType lrightFr) const

{

     //Return result

   return (numerator * lrightFr.denominator ==

       denominator * lrightFr.numerator);

}

//Define operator!=()

bool fractionType::operator!=(fractionType lrightFr) const

{

   return (numerator * lrightFr.denominator !=

       denominator * lrightFr.numerator);

}

//Define operator<=

bool fractionType::operator<=(fractionType lrightFr) const

{

   return (numerator * lrightFr.denominator <=

       denominator * lrightFr.numerator);

}

//Define operator<

bool fractionType::operator<(fractionType lrightFr) const

{

   return (numerator * lrightFr.denominator <

       denominator * lrightFr.numerator);

}

//Define operator>=

bool fractionType::operator>=(fractionType lrightFr) const

{

   return (numerator * lrightFr.denominator >=

       denominator * lrightFr.numerator);

}

//Define operator>

bool fractionType::operator>(fractionType lrightFr) const

{

   return (numerator * lrightFr.denominator >

       denominator * lrightFr.numerator);

}

//Define constructor

fractionType::fractionType(int lnum, int ldeno)

{

   numerator = lnum;

   if (ldeno == 0)

   {

       cout << "denominator cannot be zero" << endl;

       denominator = 1;

   }

   else

       denominator = ldeno;

}

//Define setFraction()

void fractionType::setFraction(int lnum, int ldeno)

{

   numerator = lnum;

   if (ldeno == 0)

   {

       cout << "denominator cannot be zero" << endl;

       denominator = 1;

   }

   else

       denominator = ldeno;

}

//Overload operator +

fractionType fractionType::operator+(fractionType lrightFr)

{

   fractionType temp;

   temp.numerator = numerator * lrightFr.denominator

       + lrightFr.numerator * denominator;

   temp.denominator = denominator * lrightFr.denominator;

   return temp;

}

//Overload operator *

fractionType fractionType::operator*(fractionType lrightFr)

{

   fractionType temp;

   temp.numerator = numerator * lrightFr.numerator;

   temp.denominator = denominator * lrightFr.denominator;

   return temp;

}

//Define operator-()

fractionType fractionType::operator-(fractionType lrightFr)

{

   fractionType temp;

   temp.numerator = numerator * lrightFr.denominator

       - lrightFr.numerator * denominator;

   temp.denominator = denominator * lrightFr.denominator;

   return temp;

}

//Define operator/()

fractionType fractionType::operator/(fractionType lrightFr)

{

   fractionType temp;

   if (lrightFr.numerator == 0)

   {

       cout << "Cannot divide by zero" << endl;

       return *this;

   }

   else

   {

       temp.numerator = numerator * lrightFr.denominator;

       temp.denominator = lrightFr.numerator * denominator;

       return temp;

   }

}

//Overload pre-increment ++ operator

fractionType fractionType::operator++ ()

{

   this->numerator = this->numerator +this->denominator;

   this->denominator = this->denominator ;

   return fractionType(numerator,denominator);

}

//post-increment ++ operator

fractionType fractionType::operator++( int )

{

   fractionType temp=fractionType(numerator,denominator);

   this->numerator = this->numerator +this->denominator;

   this->denominator = this->denominator ;

   return temp;

}

//pre-decrement -- operator

fractionType fractionType::operator-- ()

{

   this->numerator = this->numerator -this->denominator;

   this->denominator = this->denominator ;

   return fractionType(numerator,denominator);

}

//post-decrement -- operator

fractionType fractionType::operator--( int )

{

   fractionType temp=fractionType(numerator,denominator);

   this->numerator = this->numerator -this->denominator;

   this->denominator = this->denominator ;

   return temp;

}

//ftype.cpp

//Include library

#include "fractionType.h"

using namespace std;

//Define main

int main()

{

     //Define num1

     fractionType num1(5, 6); //Line 1

     //Define num2

     fractionType num2; //Line 2

     //Define num2

     fractionType num3; //Line 3

     //Display results

     cout << fixed; //Line 4

     cout << showpoint; //Line 5

     cout << "Line 7: Num1 = " << num1 << endl; //Line 7

     cout << "Line 8: Num2 = " << num2 << endl; //Line 8

     //Get user input

     cout << "Line 9: Enter the fraction "

             << "in the form a / b: "; //Line 9

     //Store value

     cin >> num2; //Line 10

     cout << endl; //Line 11

     //Display value

     cout << "Line 12: New value of num2 = "

     << num2 << endl;   //Line 12

     //Compute sum

     num3 = num1 + num2; //Line 13

     //Display num3

     cout << "Line 14: Num3 = " << num3 << endl; //Line 14

     //Display sum

     cout << "Line 15: " << num1 << " + " << num2

     << " = " << num1 + num2 << endl; //Line 15

     //Display product

     cout << "Line 16: " << num1 << " * " << num2

     << " = " << num1 * num2 << endl; //Line 16

     //Compute subtraction

     num3 = num1 - num2; //Line 17

     //Display num 3

     cout << "Line 18: Num3 = " << num3 << endl; //Line 18

     //Display result

     cout << "Line 19: " << num1 << " - " << num2

     << " = " << num1 - num2 << endl; //Line 19

     //Display quotient

     cout << "Line 20: (" << num1 << ") / (" << num2

     << ") = " << num1 / num2 << endl; //Line 20

     //Display results

     cout << "Line 21: (" << num1 << ") <= (" << num2

     << ") = " << (num1 <= num2) << endl; //Line 21

     cout << "Line 22: (" << num1 << ") == (" << num2

     << ") = " << (num1 == num2) << endl ; //Line 22

     cout << "Line 23: (" << num1 << ") >= (" << num2

     << ") = " << (num1 >= num2) << endl ; //Line 23

     cout << "Line 24: (" << num1 << ") != (" << num2

     << ") = " << (num1 != num2) << endl ; //Line 24

     cout << "Line 25: (" << num1 << ") > (" << num2

     << ") = " << (num1 > num2) << endl; //Line 25

     cout << "Line 26: (" << num1 << ") < (" << num2

     << ") = " << (num1 < num2) << endl; //Line 26

     //Pause console window

     system("pause");

     //Return 0

        return 0;

}

Add a comment
Know the answer?
Add Answer to:
I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational...
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
  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

  • 6. (Short answer) The C++ code below is very close to working, but, as written, won't...

    6. (Short answer) The C++ code below is very close to working, but, as written, won't compile. When trying to compile, the compiler generates a number of errors similar to "cout' was not declared in this scope." Respond to the prompts below about the code.(20 points, 5 points each) #include <iostream> void FindMax(int a, int b, int& max) { if (a > b) max = a; else { max = b; } int main() { int num1 = 0; int...

  • This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write...

    This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write a fraction class whose objects will represent fractions. For this assignment you aren't required to reduce your fractions. You should provide the following member functions: A set() operation that takes two integer arguments, a numerator and a denominator, and sets the calling object accordingly. Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a...

  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • C++ For this assignment you will be building on the Original Fraction class you began last...

    C++ For this assignment you will be building on the Original Fraction class you began last week. You'll be making four major changes to the class. [15 points] Delete your set() function. Add two constructors, a default constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Since Fractions cannot have...

  • C++ 10:19 AM Tue Oct 9 csive csicuny edu le. Original 3_1 Boolean and A&&B&&c and...

    C++ 10:19 AM Tue Oct 9 csive csicuny edu le. Original 3_1 Boolean and A&&B&&c and AllBIIC in a table with 3 columns Hand in HW #4 36 pts Read study SECs 3.1-3.3 1. Evaluate the following conditional expressions (T or F). 4 pts 2. FLOWCHART THE FOLLOWING (condensed) CODE in et int main) 8 PTS ( int number; cout <"Enter an integer:"; cin >>number if ( number>0) cout <You entered a positive integer:"< number < endl; else if (number...

  • Question 21 The loop condition can be a complex condition involving several operators. True OR False...

    Question 21 The loop condition can be a complex condition involving several operators. True OR False Question 22 final int MAX = 25, LIMIT = 100; int num1 = 12, num2 = 25, num3 = 87; if(num3-5 >= 2*LIMIT) { System.out.println ("apple"); } else { System.out.println ("orange"); } System.out.println("grape"); What prints? A. Apple B. Orange C. Grape D. apple grape E. orange grape F. Nothing. Question 23 When we use a while loop, we always know in advance how many...

  • C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object...

    C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object from another of the same type. II. Copy an object to pass it as argument to a function. III. Copy an object to return it from a function. Read the following program and answer questions (20 points) #include <iostream> using namespace std; class Line public: int getLength( void); Line( int len // simple constructor Line( const Line &obj) 1/ copy constructor Line (); //...

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