diff --git a/c/150-Evaluate-Reverse-Polish-Notation.c b/c/150-Evaluate-Reverse-Polish-Notation.c new file mode 100644 index 000000000..8918ab206 --- /dev/null +++ b/c/150-Evaluate-Reverse-Polish-Notation.c @@ -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]; +}