Created
October 1, 2014 15:59
-
-
Save zsh-89/4ecfd0d4767d398211c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define GET_TWO_OPERANDS { \ | |
y = operands.top(); operands.pop(); \ | |
x = operands.top(); operands.pop(); } | |
class Solution { | |
public: | |
int evalRPN(vector<string> &tokens) | |
{ | |
stack<int> operands; | |
for (int i = 0; i < tokens.size(); ++i) { | |
string &token = tokens[i]; | |
if (token == "+") { | |
int x; int y; | |
GET_TWO_OPERANDS; | |
operands.push(x + y); | |
} | |
else if (token == "-") { | |
int x; int y; | |
GET_TWO_OPERANDS; | |
operands.push(x - y); | |
} | |
else if (token == "*") { | |
int x; int y; | |
GET_TWO_OPERANDS; | |
operands.push(x * y); | |
} | |
else if (token == "/") { | |
int x; int y; | |
GET_TWO_OPERANDS; | |
operands.push(x / y); | |
} | |
else { | |
int x; | |
sscanf(token.c_str(), "%d", &x); | |
operands.push(x); | |
} | |
} | |
return operands.top(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, can you tell how the sscanf is working!