Skip to content

Commit

Permalink
Create 150-Evaluate-Reverse-Polish-Notation.c
Browse files Browse the repository at this point in the history
Create the C solution as the same solution in the YouTube video "https://www.youtube.com/watch?v=iu0082c4HDE"
  • Loading branch information
MHamiid authored Nov 16, 2022
1 parent 82e9776 commit a3b54d3
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions c/150-Evaluate-Reverse-Polish-Notation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
int evalRPN(char ** tokens, int tokensSize){
long int stk[tokensSize];

int stkIndex = -1;

for(int i = 0; i < tokensSize; i++)
{
if(strcmp(tokens[i], "+") == 0)
{
int first = stk[stkIndex];
stkIndex--;
int second = stk[stkIndex];

stk[stkIndex] = first + second;
}
else if(strcmp(tokens[i], "-") == 0)
{
int first = stk[stkIndex];
stkIndex--;
int second = stk[stkIndex];

stk[stkIndex] = second - first;
}
else if(strcmp(tokens[i], "*") == 0)
{
long first = stk[stkIndex];
stkIndex--;
int second = stk[stkIndex];

stk[stkIndex] = first * second;

}
else if(strcmp(tokens[i], "/") == 0)
{
int first = stk[stkIndex];
stkIndex--;
int second = stk[stkIndex];

stk[stkIndex] = second / first;
}
else
{
stkIndex++;
stk[stkIndex] = atoi(tokens[i]);
}

}


return stk[stkIndex];
}

0 comments on commit a3b54d3

Please sign in to comment.