"Runtime Error" when Tokens is ["3","-4", "+"],but it works well in my computer (win7 64 VS2010).


  • -1
    C
     class Solution {
     public:
    int evalRPN(vector<string> &tokens) 
    {
        int val = 0;
        int rop;
        int lop;
        stack<int> alg;
        for (int i=0; i<tokens.size();i++)
        {
            if("+"==tokens[i])
            {
                rop = alg.top();`enter code here`
                alg.pop();
                lop = alg.top();
                alg.pop();
                val = rop+lop;
                alg.push(val);
            }
            else if("-"==tokens[i])
            {
                rop = alg.top();
                alg.pop();
                lop = alg.top();
                alg.pop();
                val = rop-lop;
                alg.push(val);
            }
            else if("*"==tokens[i])
            {
                rop = alg.top();
                alg.pop();
                lop = alg.top();
                alg.pop();
                val = rop*lop;
                alg.push(val);
            }
            else if("/"==tokens[i])
            {
                rop = alg.top();
                alg.pop();
                lop = alg.top();
    			if(lop==0)
    			{
    				cout<<"Divisor cannot be zero!"<<endl;
                    exit(1);
    			}
                alg.pop();
                val = rop/lop;
                alg.push(val);
            }
            else
            {
                alg.push(atoi(tokens[i].c_str()));
            }
        }
        return alg.top();
    }
    

    };


  • 0
    S

    Look at your code again. It can't differentiate between "-" and "-4". Therefore, it treats "-4" as a minus sign, and thus try to subtract 3 from a non-existent number. This code would certainly throw an error given the input "3","-4", "+" on any platform. If it works 'fine' on your own computer, it means whatever you have there is different from what you posted.


  • 0
    C

    Thank you for your answer, but I have tested again on my computer. The result shows that: "-" and "-4" are type of string, C++ can differentiate between "-" and "-4". My test code is(Format is a bit messy,Please forgive me) :
    include "iostream"
    include "string"
    include "vector"
    using namespace std;
    int main()
    {
    string s1 = "-";
    string s2 = "-4";
    vector (a left Angle bracket)string(a right Angle bracket) vecstr;
    vecstr.push_back(s1);
    vecstr.push_back(s2);
    if ("-" == vecstr[0])
    {
    cout<<"true"<<endl;
    }
    else
    cout<<"false"<<endl;
    if ("-" == vecstr[1])
    {
    cout<<"true"<<endl;
    }
    else
    cout<<"false"<<endl;
    system("pause");
    return 0;
    }
    result is:
    "true
    false". If you think my code posted is wrong, can you tell me how should I correct my answer, thank you very much!


  • 0
    S

    Sorry my previous comment was indeed wrong. I usually use the c++11 style 'auto' for loop, so I thought token is a string rather than a vector of strings. Anyway, I tested your code, it does not produce the same error as you mentioned. It errs on ["0", "3", "/"], which is because the order of operands is wrong: you should always use "lop + operator + rop", not the other way around. After you change the order for at least '/' and '-', the code can be accepted.


  • 0
    C

    oh,I inversed the order of the operands,a very elementary mistake.Thanks very much for your help.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.