-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-10.c
47 lines (39 loc) · 776 Bytes
/
5-10.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
// 5.10 : RPN expr evaluator
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
bool isnumber(const char* str) {
while (*str) {
if (!isdigit(*str))
return false;
str++;
}
return true;
}
int main(int argc, char* argv[]) {
int operands[1000];
int* ptr = operands;
while (--argc > 0 && isnumber(*++argv))
*ptr++ = atoi(*argv);
int operator, a, b;
while (argc-- > 0 && (operator = (*argv++)[0])) {
switch (operator) {
case '+':
a = *--ptr;
b = *--ptr;
*ptr++ = a + b;
break;
case 'x':
a = *--ptr;
b = *--ptr;
*ptr++ = a * b;
break;
default:
printf("Usage: <OPERANDS>... <OPERATORS>...\n");
break;
}
}
printf("%d\n", *--ptr);
return 0;
}