Fraction Calculator

Hi, so I'm trying to make a fraction calculator where the user can input anything in expressions with the '+' and '-' operations with any amount of whitespace. For example:
>> 3 + 5 / 7 + 8
>> 3+2+1/4/ 4 + 2/ 2

I've started on a fraction class but I'm a bit hazy with operator overloading. I've been playing with overloading operators below and I know that I need to overload the '+' and '-' operators so that if the user inputs something like " 3 + 4 / 5, it will read in the numerator integers and then the denominator, but I am a bit unclear how to proceed and what to do after. Any help would be greatly appreciated. Thanks!



#include <iostream>
using namespace std;

class Fraction
{
private:
int numerator;
int denominator;

public:

Fraction() {
numerator = 0;
denominator = 0;

}
Fraction(int n, int d, int z){
numerator = n;
denominator = d;

}
friend ostream &operator<<( ostream &output, const Fraction &F )
{
output << "<<" << F.numerator << "/" << F.denominator;
return output;
}

friend istream &operator>>( istream &input, Fraction &F )
{
input >> F.numerator >> F.denominator;
return input;
}
};
int main()
{
Fraction F3;

cout << ">>";
cin >> F3;

cout << F3 << endl;

return 0;
If you know the concept of stack use it here.You could pop each element out of the array and perform calculations according to certain rules ie by determining whether it is an operator or an operand . http://en.wikipedia.org/wiki/Stack_(abstract_data_type)
How exactly do you plan on parsing the input string? You're straying near a rather common (and interesting) programming problem that is rather more complex than many assume when starting to tackle expression parsing.
Topic archived. No new replies allowed.