Skip to content

Instantly share code, notes, and snippets.

@zsh-89
Created October 1, 2014 15:59
Show Gist options
  • Save zsh-89/4ecfd0d4767d398211c0 to your computer and use it in GitHub Desktop.
Save zsh-89/4ecfd0d4767d398211c0 to your computer and use it in GitHub Desktop.
#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();
}
};
@sakshisingh0598
Copy link

hi, can you tell how the sscanf is working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment