Skip to content

Instantly share code, notes, and snippets.

@yuji314159
Created June 23, 2014 09:26
Show Gist options
  • Save yuji314159/54360992425a99049a88 to your computer and use it in GitHub Desktop.
Save yuji314159/54360992425a99049a88 to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> s;
char token[256];
int a, b;
while (cin >> token) {
switch (token[0]) {
case '+':
b = s.top(); s.pop();
a = s.top(); s.pop();
s.push(a + b);
break;
case '-':
b = s.top(); s.pop();
a = s.top(); s.pop();
s.push(a - b);
break;
case '*':
b = s.top(); s.pop();
a = s.top(); s.pop();
s.push(a * b);
break;
case '/':
b = s.top(); s.pop();
a = s.top(); s.pop();
s.push(a / b);
break;
default:
s.push(atoi(token));
break;
}
}
cout << s.top() << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment