1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 #define N 1100
5 char s[N];
6 //数字栈的操作
7 typedef struct
8 {
9 float *base;
10 float *top;
11 }SqStack2;
12 int InitStack2(SqStack2 &S)
13 {
14 S.base=(float *)malloc(505*sizeof(float));
15 S.top=S.base;
16 return 1;
17 }
18 int StackEmpty2(SqStack2 &S)
19 {
20 if(S.top==S.base)
21 return 1;
22 else return -1;
23 }
24 float GetTop2(SqStack2 S)
25 {
26 float e;
27 if(S.top==S.base) return 0;
28 e=*(S.top-1);
29 return e;
30 }
31 int Push2(SqStack2 &S,float e)
32 {
33 *S.top++=e;
34 return 1;
35 }
36 int Pop2(SqStack2 &S,float &e)
37 {
38 if(S.top==S.base) return 0;
39 e=*--S.top;
40 return 1;
41 }
42 //转化的操作过程
43 float Operate(float a,char theta,float b)
44 {
45 switch(theta){
46 case '+': return a+b;
47 case '-': return a-b;
48 case '*': return a*b;
49 case '/': return a/b;
50 default: return 0;
51 }
52 }
53 int level(char c)
54 {
55 switch(c){
56 case ' ': return 1;
57 case '+':
58 case '-': return 3;
59 case '*':
60 case '/': return 4;
61 default : return 0;
62 }
63 }
64 float EvaluateExpression()
65 {
66 SqStack2 OPND;
67 InitStack2(OPND);
68 bool flag;
69 float k,t;
70 char *p=s+strlen(s)-1;
71 char c=*p;
72 while(p!=s){
73 flag=0;
74 if(!level(c)){
75 k=c-'0';
76 c=*--p;
77 flag=1;
78 }
79 if(c!=' '){
80 for(t=k;*p<='9'&&*p>='0';p--)
81 t=0.1*t+*p-'0';
82 t*=0.1;
83 c=*--p;
84 k=c-'0';
85 Push2(OPND,k+t);
86 p--;
87 }
88 else if(flag) Push2(OPND,k);
89 if(p>s) c=*--p;
90 while(level(c)==3||level(c)==4){
91 Pop2(OPND,k);
92 Pop2(OPND,t);
93 Push2(OPND,Operate(k,c,t));
94 if(p<=s) break;
95 c=*--p;
96 }
97 }
98 return GetTop2(OPND);
99 }
100 //主函数
101 int main()
102 {
103 while(gets(s)){
104 printf("%.2f\n",EvaluateExpression());
105 }
106 return 0;
107 }
108