Implementing istream in a C++ Fraction Calculator -


i trying learn object oriented programming , make simple fraction calculator can add or subtract number of functions , write answer reduced fraction.

example: input= 3/2 + 4/ 8 , output = 2

i trying overload operators in order accomplish this.

so in program, trying develop input consists of expression made of fractions separated operators '+'or '-'.

the number of fractions in expression arbitrary.

each of following 6 lines example of valid input expression:

1/2 + 3/4 1/2 -5/7+3/5 355/113 3    /9-21/    -7 4/7-5/-8 -2/-3+7/5 

the numerator and/or denominator of fraction given input may negative. trying make input consist of single expression on single line , end of input should detected detecting end of file.

my incomplete fraction class defined in source file below:

#include <iostream> using namespace std; #include "fraction.h" #include <stdexcept>  class fraction { public:      fraction::fraction(int a, int b);     int find_gcd(int n1, int n2);       void reduce_fraction(int *nump,  int *denomp)      {       int gcd;          gcd = find_gcd(*nump, *denomp);       *nump = *nump / gcd;       *denomp = *denomp / gcd;        if ((*denomp<0 && *nump < 0 ))     {         *denomp*=-1;         *nump*=-1;     }     else if (*denomp < 0 &&  *nump >0){         *denomp*=-1;      } if ( *denomp ==0) {         throw invalid_argument( "error: 0 denominator" );     }     }    fraction& fraction::operator+(const fraction& n) {     int denom = *denomp * n.denom;     int numera = (*nump * n.numera) + (n.denom * n.nump);     return fraction(numera,denom); }  fraction& fraction::operator-(const fraction& n) {     int denom = *denomp * n.denom;     int numera = (*nump * n.numera) - (n.denom* n.nump);     return fraction(numera, denom); }  friend ostream& operator<<(ostream &os, fraction& n) {     if (n.numera == 0)     {         cout << 0 << endl;         return os;     }     else if (n.numera == n.denom)     {         cout << 1 << endl;         return os     }     else     {         cout << n.numera << '/' << n.denom << endl;         return os;     } }  friend istream& operator>>(istream &os, fraction& n) {     while(!cin.eof)     {       }     } }    }; 

essentially having trouble traversing through files , obtaining characters assign data members of functions passed parameter.

i trying implement in istream member function, having trouble implementing in case

also header file below context:

#ifndef fraction_h #define fraction_h #include <iostream> using namespace std;   class fraction{      public:      fraction(int , int );     int fraction(int,int);     void reduce_fraction(int *,  int *);     fraction& operator+(const fraction&);     fraction& operator-(const fraction&);     friend ostream& operator<<(ostream &os, const  fraction& n);     friend istream& operator>>(istream &is, const fraction& n);   };  #endif 

the extraction operator, >>, should read one fraction.
use other code read complete expression , compute value.

it (without error checking, assuming input in correct form):

istream& operator >> (istream& is, fraction& f) {    char slash = 0;    return >> f.numerator >> slash >> f.denominator; } 

hint 1: don't need use single pointer solve - you're making things harder yourself.

hint 2: you're returning references operators, assigning result of addition, e.g. (1/2 + 3/4) = 7/3, doesn't make sense.

hint 3: never type characters "eof". not mean think does.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -