题目:
题解:
int evalRPN(char** tokens, int tokensSize) {
int n = tokensSize;
int stk[(n + 1) / 2];
memset(stk, 0, sizeof(stk));
int index = -1;
for (int i = 0; i < n; i++) {
char* token = tokens[i];
if (strlen(token) > 1 || isdigit(token[0])) {
index++;
stk[index] = atoi(token);
} else {
switch (token[0]) {
case '+':
index--;
stk[index] += stk[index + 1];
break;
case '-':
index--;
stk[index] -= stk[index + 1];
break;
case '*':
index--;
stk[index] *= stk[index + 1];
break;
case '/':
index--;
stk[index] /= stk[index + 1];
break;
}
}
}
return stk[index];
}