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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
const int TOKEN_NUM = 0, TOKEN_OTHER = 1;
int last_char = 32, num, other;
int cur_token;
int next_char() {
last_char = getch();
return last_char;
}
int is_space(int c) {
if (c == 32 || c == 10) {
return 1;
}
else {
return 0;
}
}
int is_num(int c) {
if (c >= 48 && c <= 57) {
return 1;
}
else {
return 0;
}
}
int next_token() {
while (is_space(last_char)) next_char();
if (is_num(last_char)) {
num = last_char - 48;
while (is_num(next_char())) {
num = num * 10 + last_char - 48;
}
cur_token = TOKEN_NUM;
}
else {
other = last_char;
next_char();
cur_token = TOKEN_OTHER;
}
return cur_token;
}
int panic() {
putch(112);
putch(97);
putch(110);
putch(105);
putch(99);
putch(33);
putch(10);
return -1;
}
int get_op_prec(int op) {
// +, -
if (op == 43 || op == 45) return 10;
// *, /, %
if (op == 42 || op == 47 || op == 37) return 20;
// other
return 0;
}
void stack_push(int s[], int v) {
s[0] = s[0] + 1;
s[s[0]] = v;
}
int stack_pop(int s[]) {
int last = s[s[0]];
s[0] = s[0] - 1;
return last;
}
int stack_peek(int s[]) {
return s[s[0]];
}
int stack_size(int s[]) {
return s[0];
}
int eval_op(int op, int lhs, int rhs) {
// +
if (op == 43) return lhs + rhs;
// -
if (op == 45) return lhs - rhs;
// *
if (op == 42) return lhs * rhs;
// /
if (op == 47) return lhs / rhs;
// %
if (op == 37) return lhs % rhs;
// other
return 0;
}
int eval() {
int oprs[256] = {}, ops[256] = {};
// get the first value
if (cur_token != TOKEN_NUM) return panic();
stack_push(oprs, num);
next_token();
// evaluate
while (cur_token == TOKEN_OTHER) {
// get operator
int op = other;
if (!get_op_prec(op)) break;
next_token();
// handle operator
while (stack_size(ops) && get_op_prec(stack_peek(ops)) >= get_op_prec(op)) {
// evaluate the current operation
int cur_op = stack_pop(ops);
int rhs = stack_pop(oprs), lhs = stack_pop(oprs);
stack_push(oprs, eval_op(cur_op, lhs, rhs));
}
stack_push(ops, op);
// get next expression
if (cur_token != TOKEN_NUM) return panic();
stack_push(oprs, num);
next_token();
}
// eat ';'
next_token();
// clear the operator stack
while (stack_size(ops)) {
int cur_op = stack_pop(ops);
int rhs = stack_pop(oprs), lhs = stack_pop(oprs);
stack_push(oprs, eval_op(cur_op, lhs, rhs));
}
return stack_peek(oprs);
}
int main() {
int count = getint();
getch();
next_token();
while (count) {
putint(eval());
putch(10);
count = count - 1;
}
return 0;
}
|