-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata 8.cpp
More file actions
320 lines (288 loc) · 6.09 KB
/
data 8.cpp
File metadata and controls
320 lines (288 loc) · 6.09 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define Nul 0x00
char chinput[200], *p; /* 运算式存储字符串 */
/* 运算式当前读取位置 */
struct t /* 符号栈 */
{
char dat[200];
int top;
}prt;
struct d /* 数字栈 */
{
long int dat[200];
int top;
}prd;
char PRI[9][9] = /* 优先级表 */
{ {'>','>','<','<','<','<','<','>','>'},
{'>','>','<','<','<','<','<','>','>'},
{'>','>','>','>','<','<','<','>','>'},
{'>','>','>','>','<','<','<','>','>'},
{'>','>','>','>','>','<','<','>','>'},
{'>','>','>','>','>','<','<','>','>'},
{'<','<','<','<','<','<','<','=',Nul},
{'>','>','>','>','>','>',Nul,'>','>'},
{'<','<','<','<','<','<','<',Nul,'='}
};
void pushd(long int a) /* 数字入栈 */
{
prd.dat[prd.top++] = a;
}
void pusht(char a) /* 符号入栈 */
{
prt.dat[prt.top++] = a;
}
long int popd() /* 数字出栈 */
{
return prd.dat[--prd.top];
}
char popt() /* 符号出栈 */
{
return prt.dat[--prt.top];
}
long int numble() // 数字整理。有小问题
{
int flag = 0;
p--;
if (*p == -1) flag = -1;
// p--;
// if(*p <='9' && *p >= '0') {
// ;
// }
// else{
// flag =-1;
// popt();
// }
// p++;
// }
//printf("%c , %c\n",prt.dat[prt.top],prt.dat[prt.top+1]);
p++;
long int b = 0;
int count = 0;
do
{
count++;
b = b * 10 + *p - '0'; // 组成整数
p++;
} while (*p >= '0' && *p <= '9');
p--;
if (flag == -1) b = -b;
//printf("b = %d,count = %d\n",b,count);
if (count == 0) return b / 10;
else return b;
}
long operation(long int x, long int y, char a)
{
switch (a)
{
case '+': //printf("%d + %d = %d\n",x,y,x+y);
return x + y;
case '-': //printf("%d - %d = %d\n",x,y,x-y);
return x - y;
case '*': //printf("%d * %d = %d\n",x,y,x*y);
return x * y;
case '/': if (y) {
//printf("%d * %d = %d\n",x,y,x*y);
return x / y;
break;
}
else {
printf("Divide 0.\n");
//exit(0);
return -20191110;
}
case '%': //printf("%d %% %d = %d\n",x,y,x%y);
if(y == 0){
printf("error.\n");
return -20191110;
}
return (long int)fmod(x, y);
case '^': if (y >= 0) {
//printf("%d ^ %d = %d\n",x,y,(int)pow(x, y));
return (int)pow(x, y);
break;
}
else {
printf("error.\n");
return -20191110;
}
default: printf("error.\n");
//exit(1);
return -20191110;
}
}
int signswitch(char a) /* 符号转换。可优化 */
{
//printf("a:%c\n",a);
switch (a)
{
case '+': return 0; case '-': return 1;
case '*': return 2; case '/': return 3;
case '%': return 4; case '^': return 5;
case '(': return 6; case ')': return 7;
case '#': return 8;
default: printf("error.\n"); return -1;
}
}
char refusal(char a, char b) /* 判定优先级 */
{
if (signswitch(a) == -1 || signswitch(b) == -1) return -1;
return PRI[signswitch(a)][signswitch(b)];
}
int main(void)
{
int nnn;
scanf("%d", &nnn);
while (nnn--) {
for (int i = 0; i < 200; i++) {
prt.dat[i] = 0;
prd.dat[i] = 0;
chinput[i] = 0;
}
int i, j, k, l, flag = 0; // flag=0前一个符号不是)
char b;
prt.dat[0] = '#'; // 表达式起始符入栈
prt.top = 1;
prd.top = 0;
//printf("Enter Expression=");
scanf("%s", chinput); // 接收运算式字符串
//printf("输入的字符串是:%s\n",chinput);
strcat(chinput, "#"); // 在运算式结尾加#号
int len = strlen(chinput);
int ddd = 0;
for(int i=0;i<len;i++){
if(chinput[i] =='(' || chinput[i] == ')'){
if(i-1>=0&&i+1<len){
if((chinput[i-1] >= '0' && chinput[i-1] <= '9') && (chinput[i+1] >= '0' && chinput[i+1] <= '9')){
// printf("error.\n");
ddd=1;
break;
}
}
if(chinput[i] == ')'){
if(i-1>=0){
if(chinput[i-1]=='+' || chinput[i-1]=='^' || chinput[i-1]=='*' || chinput[i-1]=='/' || chinput[i-1]=='-'){
// printf("error.\n");
ddd=1;
break;
}
}
}
if(chinput[i]=='('){
if(i+1<len){
if(chinput[i+1]=='+' || chinput[i+1]=='^' || chinput[i+1]=='*' || chinput[i+1]=='/'){
//printf("error.\n");
ddd=1;
break;
}
else if(chinput[i+1]=='-'){
if(i+2<len){
if(chinput[i+2]>='0'&& chinput[i+2]<='9');
else{
ddd=1;
break;
}
}
}
}
}
}
}
if (ddd == 1) {
printf("error.\n");
continue;
}
ddd=0;
for (int i = 0; i < len - 1; i++) {
if (chinput[i] >= '0' && chinput[i] <= '9');
else {
ddd = 1;
}
}
if (ddd == 0) {
printf("error.\n");
continue;
}
for (int i = 0; i < len - 1; i++) {
if (i == 0) {
if (chinput[i] == '-') {
if (chinput[i + 1] <= '9' && chinput[i + 1] >= '0') {
chinput[i] = -1;
}
}
}
else {
if (chinput[i] == '-' && !(chinput[i - 1] >= '0' && chinput[i - 1] <= '9')) {
if (chinput[i + 1] <= '9' && chinput[i + 1] >= '0') {
chinput[i] = -1;
}
//printf("chinput[%d]=%d\n",i,chinput[i+1]);
}
}
}
p = chinput;
//printf("字符串p是:%s\n",p);
int FF = 0;
while (*p != '#' || prt.dat[prt.top - 1] != '#')
{
//printf("\nbegin loop.\n");
if (*p++ == -1) {
//printf("现在的*p: %d %c\n",*p,*p);
continue;
}
p--;
if (*p >= '0' && *p <= '9')
{
j = numble(); // 若遇数字生成完整整数入栈
pushd(j);
p++;
}
else // 如果是符号与栈顶的符号比较并运算
{
if (flag == 1 && *p == '(')
{
printf("error.\n");
//exit(0);
FF = 1;
break;
}
else if (*p == ')') flag = 1;
else flag = 0;
int FLAG = 0;
switch (refusal(prt.dat[prt.top - 1], *p))
{
case -1:FLAG = 1;
break;
case '<': pusht(*p++); /* 高则入栈 */
break;
case '=': popt(); /* 脱括号 */
p++;
break;
case '>': b = popt(); /* 低则进行栈顶计算 */
k = popd(); /* 第二操作数出栈 */
l = popd(); /* 第一操作数出栈 */
k = operation(l, k, b); /* 计算 */
if (k == -20191110) {
FLAG = 1;
break;
}
pushd(k); /* 计算结果入栈 */
break;
case Nul: printf("error.\n");
//exit(1);
FLAG = 1;
}
//printf("k=%d\n", k);
if (FLAG == 1) {
FF = 1;
break;
}
}
//printf("end loop.\n\n");
}
if (FF == 1) continue;
printf("%d\n", prd.dat[prd.top - 1]); //输出结果
}
}