Question

In: Computer Science

I am writing a program that will work with two other files to add or subtract...

I am writing a program that will work with two other files to add or subtract fractions for as many fractions that user inputs. I need to overload the + and - and << and >> opperators for the assignment. The two files posted cannot be modified. Can someone correct the Fraction.ccp and Frction.h file that I am working on? I'm really close.

//
// 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;
  }
}

//MYFILES


//fraction.cpp
Fraction::Fraction()
{
num = 0;
dem = 1;
}
Fraction::Fraction(int a, int b){
num = a;
dem = b;
}
int Fraction::getNum(){
return num;
}
int Fraction::getDen(){
return den;
}
void Fraction::setNum(int a)
{
num = a;
}
void Fraction::setDen(int b)
{
den = b;
}
Fraction Fraction ::operator+( const Fraction & rightFraction )
{
Fraction temp;
temp.num = ( num * rightFraction.den ) + ( den * rightFraction.num );
temp.den = den * rightFraction.den;
return temp;
} // end function operator+
Fraction Fraction ::operator-( const Fraction & rightFraction )
{
Fraction temp;
temp.num = ( num * rightFraction.den ) - ( den * rightFraction.num );
temp.den = den * rightFraction.den;
return temp;
} // end function operator-
ostream& operator<<(ostream& os, const Fraction& f)
{
os << f.getNum() << "/" << f.getDen(); // output in fraction format
return os;
}
istream& operator>>(istream& is, Fraction& f)
{
int t, b;
do
{
// Read the numerator first
is >> t;
char c;
is >> c;
if (c == '/') // If there is a slash, then read the next number
is >> b;
else //otherwise just "unget()" it and set to b=1
{
is.unget();
b = 1;
}
if (b==0) // if b came out to 0, fraction is not possible, so we loop back to ask user input again
throw "Error: divide by zero ";
} while(b==0);
f = Fraction(t, b); // set the fraction
return is;
}
//Header file

#indef

#def

class Fraction{
public:
Fraction();
Fraction(int, int);
getNum();
getDen();
setNum(int);
setDen(int);
friend operator-( const Fraction & rightFraction );
friend operator+( const Fraction & rightFraction );
friend ostream& operator<<(ostream& os, const Fraction& f);
friend istream& operator>>(istream& is, Fraction& f);

private:
int num;
int den;
};

Solutions

Expert Solution

calculator.cpp

//
// calculator.cpp
//
#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;
}
}

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 getDem() const;
   void setNum(int num);
   void setDem(int den);
   void reduce();
   void signs();
   friend   ostream& operator<<(ostream &out,const Fraction &);
   friend   istream& operator>>(istream &,Fraction &);
   Fraction operator+(const Fraction );
   Fraction operator-(const 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);
   setDem(0);
}
Fraction::Fraction(int nem, int dem)
{
   if (dem == 0)
   {
       throw invalid_argument("division by zero");
   }
   else
   {
       setNum(nem);
       setDem(dem);
       reduce();
   }
  
}
void Fraction::setNum(int num)
{
   nemer = num;
}
void Fraction::setDem(int den)
{
   denom = den;
}
int Fraction::getNum() const
{
   return nemer;
}
int Fraction::getDem() const
{
  
   return denom;
}
void Fraction::reduce()
{
   signs();
   int Sign = 1;
   int nem = getNum();
   int den = getDem();
   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);
   setDem(den);
}
void Fraction::signs()
{
   int nem = getNum();
   int den = getDem();
   if (den < 0)
   {
       nem *= -1;
       den *= -1;
   }
   setNum(nem);
   setDem(den);
}
Fraction &Fraction::operator=(const Fraction frac)
{
   int num = frac.getNum();
   int den = frac.getDem();
   (*this).setNum(num);
   (*this).setDem(den);
   return *this;
}
istream &operator>>(istream &in, Fraction &frac)
{
   char Dum;
   int num;
   int den;
   in >> num;
   in >> Dum;
   in >> den;
   if (den == 0)
   {
       throw invalid_argument("Error: division by zero");
       exit(0);
   }
   frac.setNum(num);
   frac.setDem(den);
   frac.reduce();
   return in;
}
ostream &operator<<(ostream& out, const Fraction& frac)
{
;
   if (frac.getDem() == 1)
       out << frac.getNum();
   else
   {
       out << frac.getNum() << '/' << frac.getDem();
      
   }
   return out;
}
Fraction Fraction::operator+(const Fraction frac)
{
   int Nsum = ((*this).getNum() * frac.getDem()) + (getNum() * (*this).getDem());
   int Dsum = ((*this).getDem() * frac.getDem());
   Fraction frac2(Nsum, Dsum);
   frac2.reduce();
   return frac2;
}
Fraction Fraction::operator-(const Fraction frac)
{
   int Nsum= ((*this).getNum() * frac.getDem()) - (getNum() * (*this).getDem());
   int Dsum = ((*this).getDem() * frac.getDem());
   Fraction frac2(Nsum, Dsum );
   frac2.reduce();
   return frac2;
}

useFraction.cpp

//
// useFraction.cpp
//

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

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

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

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



Related Solutions

I am Writing a C-Program to read and write files. but none of my code is...
I am Writing a C-Program to read and write files. but none of my code is working like it should be. Please fix all code and supply output response. Please try to use existing code and code in comments. But if needed change any code that needs to be changed. Thank you in advance //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void rfile(void); void wfile(void); struct personnel {...
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide...
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide using functions. It needs to ask for the two numbers from the user and will ask the user for their choice of arithmetic operation 1- subtract 2- add 3- divide 4- multiply
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
I am writing a jave program. I have seen the topic before but I want to...
I am writing a jave program. I have seen the topic before but I want to do it this way. And I got an error says: BonusAndDayOffRew.java:14: error: variable monthlySales might not have been initialized getSales(monthlySales); ^ Here is the description:A retail company assigns a $5000 store bonus if monthly sales are $100,000 or more. Additionally, if their sales exceed 125% or more of their monthly goal of $90,000, then all employees will receive a message stating that they will...
I am trying to add two linked-list based integers, but the program keeps giving me the...
I am trying to add two linked-list based integers, but the program keeps giving me the incorrect result. I am trying to add (List 1: 43135) + (List 2: 172). I have pasted the code below #include <iostream> using namespace std; //Linked list node class Node {    public:    int num;    Node* next; }; //Function to create a new node with given numbers Node *new_Node(int num) {    Node *newNode = new Node();    newNode->num = num;   ...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need to make sure I did it correctly. I'm not sure if my formula is correct. I appreciate any help, thank you! TASK: Write a program that will calculate the final balance in an investment account. The user will supply the initial deposit, the annual interest rate, and the number of years invested. Solution Description: Write a program that calculates the final balance in an...
I am writing a matlab program where I created a figure that has a few different...
I am writing a matlab program where I created a figure that has a few different elements to it and now I need to move that figure into a specific excel file into a specific set of boxes in the excel file. With numbers and text I have always used the xlswrite function for this in order to put data into specific boxes. How do I do the same with this figure? The figure I have is called like this:...
This is a java program I am trying to figure out how to add a couple...
This is a java program I am trying to figure out how to add a couple methods to a program I am working on. I need to be able to: 1. Remove elements from a binary tree 2. Print them in breadth first order Any help would appreciated. //start BinaryTree class /** * * @author Reilly * @param <E> */ public class BinaryTree { TreeNode root = null; TreeNode current, parent; private int size = 0, counter = 0; public...
C++ For this assignment, you will write a C++ program to either add (A), subtract (S),...
C++ For this assignment, you will write a C++ program to either add (A), subtract (S), multiply (M), and (N), or (O) two matrices if possible. You will read in the dimensions (rows, then columns) of the first matrix, then read the first matrix, then the dimensions (rows then columns) of the second matrix, then the second matrix, then a character (A, S, M, N, O) to determine which operation they want to do. The program will then perform the...
i am looking to add more detail into my program the runs a game of rock...
i am looking to add more detail into my program the runs a game of rock paper scissors i want to include a function or way to ask the user to input their name so that it could be shown in the results at the end of a round/game. after a round something like "Mike wins!" i also want to output the round number when the result of for each round is outputed Round 1: Mike Wins! Round 2: Computer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT