Question

C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I...

C++ PROGRAMMING

Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you!

I have attatched the homework instructions and the two files given.

Implementation

The main program, called calculator.cpp , is provided. Your project must include a class Fraction that encapsulates all functions related to the processing of fractions. The >> and <<

operators must be overloaded and used to read and write fractions. The presence of a zero denominator in input should be handled by throwing an exception of type invalid_argument defined in <stdexcept>. The class Fraction must be defined in a source file Fraction.cpp and declared in a header file Fraction.h. The implementation of the member functions should be defined in the file Fraction.cpp. The class Fraction must include a constructor Fraction::Fraction(int a, int b). The internal representation of the fraction should be in reduced form, i.e. using a pair of numbers that have no common factors. Constructors and other member functions must ensure that the fraction is always reduced. Use Euclid's algorithm to simplify fractions to reduced form. An example of a C implementation of this algorithm is provided on the web site web.cs.ucdavis.edu/~fgygi/ecs40 in the example programs of Lecture 1. The operators '+', '-' and '=' must be overloaded and defined. Member functions getNum() and getDen() should be implemented, returning the (reduced) numerator and denominator of the fraction. It should be possible to use the class Fraction in the program useFraction.cpp which #includes the header Fraction.h. The program useFraction.cpp is provided on the web site web.cs.ucdavis.edu/~fgygi/ecs40. You should not modify that program.

//
// useFraction.cpp
//
// DO NOT MODIFY THIS FILE
//

#include "Fraction.h"
#include<iostream>
using namespace std;

void print_fraction(const Fraction& f)
{
cout << "print_fraction: " << f.getNum() << "/" << f.getDen() << endl;
}

int main()
{
Fraction x(2,3);
Fraction y(6,-2);

cout << x << endl;
cout << y << endl;

cin >> y;
cout << y << endl;
print_fraction(y);
Fraction z = x + y;
cout << x << " + " << y << " = " << z << endl;
}

//
// calculator.cpp
//
// DO NOT MODIFY THIS FILE
//

#include "Fraction.h"
#include<iostream>
#include<stdexcept>
using namespace std;

int main()
{
Fraction x,y;
char op
try
{
cin >> x;
cin >> op;
while ( cin && ( op == '+' || op == '-' ) )
{
cin >> y;
if ( op == '+' )
x = x + y;
else
x = x - y;
cin >> op;
}
cout << x << endl;
}
catch ( invalid_argument& e )
{
cout << "Error: " << e.what() << endl;
}
}

Homework Answers

Answer #1

The two files given by your professor are main programs to test your code.

Your code should consist of two files, Fraction.h and Fraction.cpp.

Here is my implementation of the code. You can have this as a referrence to produce your own.

Fraction.h

#ifndef Fraction_h

#define Fraction_h

#include <iostream>

using namespace std;

class Fraction

{

public:

//Functions

Fraction();

Fraction(int,int);

   int getNum() const;

   int getDen() const;

   void setNum(int num);

   void setDen(int den);

   void reduce();

   void signs();

   friend ostream& operator<<(ostream &out,const Fraction &);

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

   Fraction operator+(const Fraction );

private:

int nemer;

int denom;

};

#endif

Fraction.cpp

#include "fraction.h"

#include <stdexcept>

#include <iostream>

#include <cstdlib>

using namespace std;

Fraction::Fraction()

{

   setNum(0);

   setDen(1);

}

Fraction::Fraction(int nem, int dem)

{

   if (dem == 0)

   {

   throw invalid_argument("Error: denominator is zero");

   }

   else

   {

   setNum(nem);

   setDen(dem);

   reduce();

   }

}

void Fraction::setNum(int num)

{

   nemer = num;

}

void Fraction::setDen(int den)

{

   denom = den;

}

int Fraction::getNum() const

{

   return nemer;

}

int Fraction::getDen() const

{

   return denom;

}

void Fraction::reduce()

{

   signs();

   int Sign = 1;

   int nem = getNum();

   int den = getDen();

   if (nem < 0)

   {

   nem *= -1;

   Sign = -1;

   }

   for (int i = (nem * den); i > 1; i--)

   {

   if ((nem % i == 0) && (den % i == 0))

   {

   nem /= i;

   den /= i;

   }

   }

   nem *= Sign;

   setNum(nem);

   setDen(den);

}

void Fraction::signs()

{

   int nem = getNum();

   int den = getDen();

   if (den < 0)

   {

   nem *= -1;

   den *= -1;

   }

   setNum(nem);

   setDen(den);

}

istream &operator>>(istream &in, Fraction &frac)

{

   int num;

   int den;

   in >> num;

   in >> den;

   if (den == 0)

   {

   throw invalid_argument("Error: denominator is zero");

   exit(0);

   }

   frac.setNum(num);

   frac.setDen(den);

   frac.reduce();

   return in;

}

ostream &operator<<(ostream& out, const Fraction& frac)

{

   if (frac.getDen() == 1)

   out << frac.getNum();

   else

   {

   out << frac.getNum() << "/" << frac.getDen() ;

   }

   return out;

}

Fraction Fraction::operator+(const Fraction frac)

{

   int Nsum = ((*this).getNum() * frac.getDen()) + (frac.getNum() * (*this).getDen());

   int Dsum = ((*this).getDen() * frac.getDen());

   Fraction frac2(Nsum, Dsum);

   frac2.reduce();

   return frac2;

}

//The below methods have been copy-pasted from useFraction.cpp - This is for testing your code
void print_fraction(const Fraction& f)

{

cout << "print_fraction: " << f.getNum() << "/" << f.getDen() << endl;

}

int main()

{

Fraction x(2,3);

Fraction y(6,-2);

cout << x << endl;

cout << y << endl;

cout<<"Enter fraction: ";

cin >> y;

cout << y << endl;

print_fraction(y);

Fraction z = x + y;

cout << x << " + " << y << " = " << z << endl;

}

Know the answer?
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
Similar Questions
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
1) Create a flowchart for the program below and Trace the program below with input of...
1) Create a flowchart for the program below and Trace the program below with input of 3 and then again with input of 5. #include <iostream> using namespace std; int Fall(int x, int m) { int Xm = 1, i=x; while(i>=x-m+1) { Xm = Xm*i; i=i-1; } return Xm; } int Delta(int x, int m) { int ans = 0; ans = Fall(x+1,m); ans = ans - Fall(x,m); return ans; } void main() { int x = 0, m =...
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
C++ PROGRAM When I input 3 S P R, it was suppoesed to pop up L...
C++ PROGRAM When I input 3 S P R, it was suppoesed to pop up L W T. But it showed L L L.IDK why the moveNo is not working. I am asking for help, plz dont put some random things on it. main.cpp #include <iostream> #include "computer.h" #include "human.h" #include "referee.h" using namespace std; int main() {     human h;     computer c;     referee r;     r.compare(h,c);     return 0; } computer.cpp #include<iostream> #include "computer.h" using namespace std;...
C++ program that Create a struct called car that has the following data members (variables): -...
C++ program that Create a struct called car that has the following data members (variables): - Color //color of the car - Model //model name of the car - Year //year the car was made - isElectric //whether the car is electric (true) or not (false) - topSpeed //top speed of the car, can be a decimal. code i have done struct not working properly. #include <iostream> using namespace std; struct Car { string color; string model_number; int year_model; bool...
Please write variables and program plan (pseudocode) of the C++ programming code below: #include <iostream> #include...
Please write variables and program plan (pseudocode) of the C++ programming code below: #include <iostream> #include <cmath> using namespace std; int getWeight(); float deliveryCharge(int weight); void displayCharge(int weight); int main () {    displayCharge(getWeight());    return 0; }   int getWeight() {    int weight;    cout << "Enter package weight (oz): ";    cin >> weight;    return weight; } float deliveryCharge(int weight) {    if (weight > 16)    {        return (((weight - 16) / 4) *...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
How do I make this code not include negative numbers in the total or average if...
How do I make this code not include negative numbers in the total or average if they are entered? (C++) For example, if you enter 4, 2, and -2, the average should not factor in the -2 and would be equal to 3. I want the code to factor in positive numbers only. ----- #include using namespace std; int main() {    int x, total = 0, count = 0;       cout << "Type in first value ";   ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT