Skip to content

Commit 19e323c

Browse files
authored
Create program.cpp
1 parent 26692cb commit 19e323c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Reverse Polish Notation/program.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
int evalRPN(vector<string>& tokens)
4+
{
5+
stack<long>stk;
6+
for(int i=0;i<tokens.size();i++)
7+
{
8+
if(tokens[i]!="+" && tokens[i]!="/" && tokens[i]!="*" && tokens[i]!="-")
9+
{
10+
long t=stoi(tokens[i]);
11+
stk.push(t);
12+
}
13+
else
14+
{
15+
long a=stk.top();
16+
stk.pop();
17+
long b=stk.top();
18+
stk.pop();
19+
string ch=tokens[i];
20+
long c=0;
21+
if(ch=="+")
22+
c=b+a;
23+
if(ch=="-")
24+
c=b-a;
25+
if(ch=="*")
26+
c=b*a;
27+
if(ch=="/")
28+
c=b/a;
29+
stk.push(c);
30+
}
31+
}
32+
return int(stk.top());
33+
}
34+
};

0 commit comments

Comments
 (0)