2

I'm currently trying to test for if there are too many operands but cannot figure out the condition for when a postfix expression has too many operands.

Could someone give me any pointers on what to test for?

Here is my function so far:

void evaluatePostFix(string str){
    Stack stack;
    // Strip whitespaces
    str.erase(str.find(' '), 1);
    if (str.length() == 1 || str.length() == 0){
        string singleOperand;
        singleOperand.push_back(str[0]);
        stack.push(createExpression("", singleOperand, ""));
    }
    int count = 0;

    for (const char & c : str){
        count++;
        if (isOperand(c)){
            string singleOperand;
            singleOperand.push_back(c);
            stack.push(singleOperand);
        } else {
            if (stack.isEmpty()){
                cout << "To many operators" << endl;
                return;
            }
            string operand1 = stack.top();
            stack.pop();
            if (stack.isEmpty()){
                cout << "To many operators" << endl;
                return;
            }
            string operand2 = stack.top();
            stack.pop();
            string operator1, expression;
            operator1.push_back(c);
            expression = createExpression(operand1, operand2, operator1);
            stack.push(expression);
        }
    }
    stack.print();
}
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Reece Humphreys
  • 147
  • 1
  • 8

1 Answers1

3

I think you're overthinking this. To evaluate postfix notation you do the following:

  1. Set up a stack

  2. Iterate over your input

  3. If you find an operand push it onto the stack

  4. If you find an operation pop the number of operands needed to perform it off the stack. Apply the operation and then push the result back onto the stack. If you're not able to pop off the correct number of operands than there are too few operands.

  5. At the end of this process you should have one item left in your stack - the result. If there is more than one item then at some point you had too many operands.

Here is a readable python implementation to illustrate:

def evaluate_postfix(inputstr):

    # split into a list of parts consisting of operands and operators
    ops = inputstr.split()
    stack = []

    for i in ops:

        # if it's an operand push to the stack
        if i.isdigit():
            stack.append(int(i))
        else:
            # if there's not enough operands exit
            if len(stack) < 2:
                print("TOO FEW OPERANDS")
                exit()
            else:
                # pop the operands, apply the operation, and push the result
                a, b = stack.pop(), stack.pop()

                if i == '+': stack.append(a + b)
                elif i == '-': stack.append(a - b)
                elif i == '/': stack.append(a / b)
                else: stack.append(a * b)

    # if there are multiple values left in the stack then at some point
    # there were too many operands for the number of operations
    if len(stack) != 1:
        print("TOO MANY OPERANDS")
        exit()
    return stack[0]

And some test cases:

print(evaluate_postfix("1 2 + 3 *"))
# 9
print(evaluate_postfix("1 2 + 3 * *"))
# TOO FEW OPERANDS
print(evaluate_postfix("1 2 3 4 + +"))
# TOO MANY OPERANDS
Primusa
  • 13,136
  • 3
  • 33
  • 53