Martin Fric
Martin Fric

Reputation: 730

C++ postfix notation - evaluation

It is quite while since i was working with c++ and i am strugling to solve postfix notation. It looks like pop is not working properly and numbers are not rewritten.

Here is my code:

int main() {
    string input;
    string num = "";
    int i;
    int a,b,c;
    vector< int > numbers;
    getline(cin,input);
    for(i=0; i<input.length(); i++) {
        if(input[i] >= '0' && input[i] <= '9') { //creating multi digit number
            num.push_back(input[i]);
            if(i == input.length()) {
               istringstream(num) >> c;
                numbers.push(c);
            }
        } else if(input[i] == ' ') {
            istringstream(num) >> c;
            numbers.push(c);
            num = "";
        }
        if (input[i]=='+') {
            b = numbers[numbers.size() - 1];
            a = numbers[numbers.size() - 2];
            numbers.pop_back();numbers.pop_back();
            numbers.push_back(a+b));
            continue;
        }
        if (input[i]=='*') {
            b = numbers[numbers.size() - 1];
            a = numbers[numbers.size() - 2];
            numbers.pop_back();numbers.pop_back();
            numbers.push_back(a*b));
            continue;
        }
        if (input[i]=='/') {
            b = numbers[numbers.size() - 1];
            a = numbers[numbers.size() - 2];
            numbers.pop_back();numbers.pop_back();
            numbers.push_back(a/b));
            continue;
        }
        if (input[i]=='-') {
            b = numbers[numbers.size() - 1];
            a = numbers[numbers.size() - 2];
            numbers.pop_back();numbers.pop_back();
            numbers.push_back(a-b));
            continue;
        }
    }
    if(numbers.size() > 1 && i != input.length() -1) {
        return 0;
    }

    cout << numbers.top();
    return 0;
}

Basically it looks like the result not pushed as the top of the stack. for example 55 5 - 2 * will go into operation like:

  1. 55 - 5
  2. 5 * 2 (instead of 50 * 2)

So i am not sure if pop_back or push_back dont work;

Can anybody give me a hint please?

UPDATE:

I enhanced the code:

#include <iostream>
#include <stack>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

void print_numbers(vector <int> numbers, char op) {
    for(int i = 0; i < numbers.size(); i++){
        cout << op << ": " << numbers[i] << " ";
    }
    cout << endl;
}

int main() {
    string input;
    string num = "";
    int i;
    int a,b,c;
    vector< int > numbers;
    getline(cin,input);
    for(i=0; i<input.length(); i++) {
        if(input[i] >= '0' && input[i] <= '9') {
            num.push_back(input[i]);
            if(i == input.length() -1) {
               istringstream(num) >> c;
                numbers.push_back(c);
            }
        } else if(input[i] == ' ') {
            istringstream(num) >> c;
            numbers.push_back(c);
            num = "";
        }
        if (input[i]=='+') {
            b = numbers[numbers.size() - 1];
            a = numbers[numbers.size() - 2];
            numbers.pop_back();numbers.pop_back();
            numbers.push_back(a+b);
            continue;
        }
        if (input[i]=='*') {
            print_numbers(numbers,'*');
            b = numbers[numbers.size() - 1];
            a = numbers[numbers.size() - 2];
            numbers.pop_back();numbers.pop_back();
            numbers.push_back(a*b);
            print_numbers(numbers,'*');
            continue;
        }
        if (input[i]=='/') {
            b = numbers[numbers.size() - 1];
            a = numbers[numbers.size() - 2];
            numbers.pop_back();numbers.pop_back();
            numbers.push_back(a/b);
            continue;
        }
        if (input[i]=='-') {
            b = numbers[numbers.size() - 1];
            a = numbers[numbers.size() - 2];
            print_numbers(numbers,'-');
            numbers.pop_back();numbers.pop_back();
            numbers.push_back(a-b);
            print_numbers(numbers,'-');
            continue;
        }
    }

    print_numbers(numbers,' ');
    cout << numbers[numbers.size() - 1];
    return 0;
}

on input: 55 5 - 2 *

outputs:

1. -: 55 -: 5
2. -: 50
3. *: 50 *: 5 *: 2
4. *: 50 *: 10
5. : 50  : 10
6. 10

so it looks like proper number is placed in step nr2. but then it looks like buffer is again also with nr 5

Upvotes: 0

Views: 256

Answers (2)

john
john

Reputation: 87959

So the problem is what happens after the - in the input. The next character is a space so you enter this code

    } else if(input[i] == ' ') {
        istringstream(num) >> c;
        numbers.push_back(c);
        num = "";
    }

where you attempt to push another number onto the stack. However num is empty at this point which means that istringstream(num) >> c; fails and so the variable c is unchanged. Next line c gets pushed onto the stack anyway, but c still has it's old value of 5 from the previous number. This explains why you get the same number pushed back twice.

One solution would be this

    } else if(input[i] == ' ' && num != "") {
        istringstream(num) >> c;
        numbers.push_back(c);
        num = "";
    }

It took me literally two minutes to figure this out using a debugger. Maybe time you learned how to use one?

Upvotes: 1

john
john

Reputation: 87959

        if (i == input.length()) {
           istringstream(num) >> c;
            numbers.push(c);
        }

should be

        if (i + 1 == input.length()) {
           istringstream(num) >> c;
            numbers.push(c);
        }

i will never be equal to input.length(), look at the for loop condition.

Upvotes: 0

Related Questions