-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo3-6.cpp
59 lines (56 loc) · 1.27 KB
/
algo3-6.cpp
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
// algo3-6.cpp 表达式求值(范围为int类型,输入负数要用(0-正数)表示)
typedef int SElemType;
#include"c1.h"
#include"c3-1.h"
#include"bo3-1.cpp"
#include"func3-1.cpp"
SElemType EvaluateExpression()
{
SqStack OPTR,OPND;
SElemType a,b,d,x;
char c;
InitStack(OPTR);
InitStack(OPND);
Push(OPTR,'\n');
c=getchar();
GetTop(OPTR,x);
while(c!='\n'||x!='\n')
{ if(In(c))
switch(Precede(x,c))
{ case'<':Push(OPTR,c);
c=getchar();
break;
case'=':Pop(OPTR,x);
c=getchar();
break;
case'>':Pop(OPTR,x);
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,x,b));
}
else if(c>='0'&&c<='9')
{ d=0;
while(c>='0'&&c<='9')
{ d=d*10+c-'0';
c=getchar();
}
Push(OPND,d);
}
else
{ printf("出现非法字符\n");
exit(OVERFLOW);
}
GetTop(OPTR,x);
}
Pop(OPND,x);
if(!StackEmpty(OPND))
{ printf("表达式不正确\n");
exit(OVERFLOW);
}
return x;
}
void main()
{
printf("请输入算术表达式,负数要用(0-正数)表示\n");
printf("%d\n",EvaluateExpression());
}