-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_reverse_polish_notation.c
65 lines (52 loc) · 1.76 KB
/
_reverse_polish_notation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
typedef struct stack {
size_t SP;
size_t cap;
int* data;
} stack;
// NEW(stack*, size_t), allocate a stack of given size.
#define NEW(s, n) ((s = (stack*)malloc(sizeof(stack))),\
(s -> cap = n), \
(s -> SP = 0), \
(s -> data = (int*)calloc(n, sizeof(int))))
// PUSH(stack*, size_t), put a given value on the top of the stack
#define PUSH(s, v) (s -> data[(s -> SP)++] = (int)v)
// POP(s), return the value on top of the stack, or 0 if the stack is empty.
#define POP(s) (s -> data[--(s -> SP)])
int evalRPN(char** tokens, int tokensSize) {
stack* STACK; NEW(STACK, tokensSize);
int val, x, y;
for(int index = 0; index < tokensSize; index++) {
// Numbers starting with sign or multi-digit
if(strlen(tokens[index]) != 1) {
PUSH(STACK, atoi(tokens[index]));
continue;
}
switch(*tokens[index]) {
// Operations
case '+':
x = POP(STACK);
y = POP(STACK);
val = x + y;
PUSH(STACK, val); break;
case '-':
x = POP(STACK);
y = POP(STACK);
val = y - x;
PUSH(STACK, val); break;
case '*':
x = POP(STACK);
y = POP(STACK);
val = x * y;
PUSH(STACK, val); break;
case '/':
x = POP(STACK);
y = POP(STACK);
val = y / x;
PUSH(STACK, val); break;
// New single digit value
default:
PUSH(STACK, *tokens[index]%'0');
} // switch
} // for
return POP(STACK);
}