Question

I am working on creating a Prefix calculator, where the user enters a prefix expression, and the program evaluates it. It is working for some cases, e.g. "+43" outputs 7 as it should, but "/-421" outputs "2", when it should output "1", and "/+421" outputs "6" rather than "3", things like that. Any suggestions for fixing that? I'm going to add exceptions later, so they are commented out for now.

PrefixCalculator.cpp

#pragma once

#include <sstream>

using namespace std;

template<class T>
class PrefixCalculator {
public:
PrefixCalculator(void){
    numOperator = 0;
    numOperand = 0;
};
~PrefixCalculator(void){};

T eval(istringstream&);

int getNumOperator() {
    return numOperator;
};

int getNumOperand() {
    return numOperand;
};

private:
//if you feel you need private helper functions and/or helper data
int numOperator;
int numOperand;
};

template<class T>
T PrefixCalculator<T>::eval(istringstream& input) { 
 //this function needs to throw an exception if there's a problem with the expression or operators
char nextChar = input.peek();

//this while loop skips over the spaces in the expression, if there are any
while(nextChar == ' ') {
    input.get();    //move past this space
    nextChar = input.peek(); //check the next character
}

if(nextChar == '+') {
    input.get();    //moves past the +
    numOperator++;
    return eval(input) + eval(input);   //recursively calculates the first expression, and adds it to the second expression, returning the result
}

/***** more operators here ******/
if(nextChar == '-') {
    input.get();
    numOperator++;
    return eval(input) - eval(input);
}

if(nextChar == '*') {
    input.get();
    numOperator++;
    return eval(input) * eval(input);
}

if(nextChar == '/') {
    input.get();
    numOperator++;
    return eval(input) / eval(input);
} 

/******  BASE CASE HERE *******/
//it's not an operator, and it's not a space, so you must be reading an actual value (like '3' in "+ 3 6".  Use the >> operator of istringstream to pull in a T value!
input>>nextChar;
T digit = nextChar - '0';
numOperand++;
return digit;
//OR...there's bad input, in which case the reading would fail and you should throw an exception

}

driver.cpp

#include <sstream>
#include <string>
#include <iostream>
#include "PrefixCalculator.h"

using namespace std;

int main(int argc, char** argv) {
PrefixCalculator<int> calc;

string expression;
cout << "Give a prefix expression to evaluate, or q to quit." << endl;
getline(cin,expression);

while(expression[0] != 'q') {
    //try {
        int result = calc.eval(istringstream(expression));
        cout << result << endl;
    //}
    //catch { //will not compile, you have to finish this!
    //  
    //}

    cout << "Give a prefix expression to evaluate or q to quit." << endl;
    getline(cin,expression);
}

return 0;
 }
Was it helpful?

Solution

I'm confused. From the output you gave, your program is working correctly, but your expectations are not correct.

Let's evaluate the expression "/-421":
1. Operator detected, '/', push onto stack:

+----+
| /  |
+====+

Operator detected, '-', push onto stack:

+---+  
| - |  
+---+  
| / |  
+===+  

Number detected, push onto stack since previous operator requires 2 parameters.

+---+
| 4 |
+---+
| - |
+---+
| / |
+===+

Number dectected, the 2nd of of two operators.
Pop the remaining operand off the stack, 4.
Pop the operation off the stack evaluate with the parameters:

result = 4 - 2 --> 1

Push result on the stack.

+---+
| 2 |
+---+
| / |
+===+

Number detected, the 2nd parameter of 2 for the division operator.
Pop the number from the stack, this becomes the 1st parameter to the division operation.
Pop the operation off the stack and evaluate with the parameters:

result = 2 / 1 --> 2

Push the result on the stack.

+---+
| 2 |
+===+

End of expression reached, pop off result and print:

2

Edit 1:

You can confirm your program's operation by printing the stack (one item per line) before analyzing the next token.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top