Implementing istream in a C++ Fraction Calculator

痴心易碎 提交于 2019-12-12 05:34:57

问题


I am trying to learn the object oriented programming and make the simple fraction calculator than can add or subtract any number of functions and write the answer as a reduced fraction.

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

I am trying overload operators in order to accomplish this.

So in the program, I am trying to develop the input consists of an expression made of fractions separated by the operators '+'or '-'.

The number of fractions in the expression is arbitrary.

Each of the following 6 lines is an 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 a fraction given as input may be negative. I am trying to make the input consist of a single expression on a single line and the end of input should be detected by detecting the end of file.

My incomplete fraction class is defined in my 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: zero 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 what I am having trouble with is traversing through the files and obtaining the characters to assign to data members of the functions which are passed as a parameter.

I am trying to implement this in my istream member function, but I am having trouble implementing it in this case

Also my header file is below for 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

回答1:


The extraction operator, >>, should read one fraction.
Use other code to read a complete expression and compute its value.

It would look something like this (without error checking, just assuming that input is in the correct form):

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

Hint 1: You don't need to use a single pointer to solve this - you're only making things harder for yourself.

Hint 2: You're returning references from the operators, but assigning to the result of an addition, e.g. (1/2 + 3/4) = 7/3, doesn't make much sense.

Hint 3: Never type the characters "eof". It does not mean what you think it does.



来源:https://stackoverflow.com/questions/23260190/implementing-istream-in-a-c-fraction-calculator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!