You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
741 B

#include <bits/stdc++.h>
using namespace std;
bool isOp(string c){
return c == "+" || c == "-" || c == "*" || c == "/";
}
int eval(string s){
stack<int> stk;
stringstream s1(s);
string c;
while (s1>>c){
if(isOp(c)){
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
if (c == "+") stk.push(b + a);
if (c == "-") stk.push(b - a);
if (c == "*") stk.push(b * a);
if (c == "/") stk.push(b / a);
}else
stk.push(stoi(c));
}
return stk.top();
}
int main() {
string s = "24 9 6 3 / + 5 - * 4 +";
cout << "answer:" << eval(s) << endl;
return 0;
}