Skip to content

Commit a3b54d3

Browse files
authored
Create 150-Evaluate-Reverse-Polish-Notation.c
Create the C solution as the same solution in the YouTube video "https://www.youtube.com/watch?v=iu0082c4HDE"
1 parent 82e9776 commit a3b54d3

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
int evalRPN(char ** tokens, int tokensSize){
2+
long int stk[tokensSize];
3+
4+
int stkIndex = -1;
5+
6+
for(int i = 0; i < tokensSize; i++)
7+
{
8+
if(strcmp(tokens[i], "+") == 0)
9+
{
10+
int first = stk[stkIndex];
11+
stkIndex--;
12+
int second = stk[stkIndex];
13+
14+
stk[stkIndex] = first + second;
15+
}
16+
else if(strcmp(tokens[i], "-") == 0)
17+
{
18+
int first = stk[stkIndex];
19+
stkIndex--;
20+
int second = stk[stkIndex];
21+
22+
stk[stkIndex] = second - first;
23+
}
24+
else if(strcmp(tokens[i], "*") == 0)
25+
{
26+
long first = stk[stkIndex];
27+
stkIndex--;
28+
int second = stk[stkIndex];
29+
30+
stk[stkIndex] = first * second;
31+
32+
}
33+
else if(strcmp(tokens[i], "/") == 0)
34+
{
35+
int first = stk[stkIndex];
36+
stkIndex--;
37+
int second = stk[stkIndex];
38+
39+
stk[stkIndex] = second / first;
40+
}
41+
else
42+
{
43+
stkIndex++;
44+
stk[stkIndex] = atoi(tokens[i]);
45+
}
46+
47+
}
48+
49+
50+
return stk[stkIndex];
51+
}

0 commit comments

Comments
 (0)