nyoj35 表达式求值

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #define N 1010
  4 char s[N];
  5 //字符栈的操作 
  6 typedef struct
  7 {
  8   char *base;
  9   char *top;
 10 }SqStack1;
 11 int InitStack1(SqStack1 &S)
 12 {
 13   S.base=(char *)malloc(N*sizeof(char));
 14   if(!S.base)  exit(1);
 15   S.top=S.base;
 16   return 1;
 17 }
 18 int StackEmpty1(SqStack1 &S)
 19 {
 20   if(S.top==S.base)
 21       return 1;
 22   return 0;
 23 }
 24 char GetTop1(SqStack1 S)
 25 {
 26     char e;
 27     if(S.top==S.base)  return 0;
 28     e=*(S.top-1);
 29     return e;
 30 }
 31 int Push1(SqStack1 &S,char e)
 32 {
 33   *S.top++=e;
 34   return 1;
 35 }
 36 int Pop1(SqStack1 &S,char &e)
 37 {
 38   if(S.top==S.base)  return 0;
 39   e=*--S.top;
 40   return 1;
 41 }
 42 //数字栈的操作
 43 typedef struct
 44 {
 45   float *base;
 46   float *top;
 47 }SqStack2;
 48 int InitStack2(SqStack2 &S)
 49 {
 50   S.base=(float *)malloc(N*sizeof(float));
 51   if(!S.base)  exit(1);
 52   S.top=S.base;
 53   return 1;
 54 }
 55 int StackEmpty2(SqStack2 &S)
 56 {
 57   if(S.top==S.base)
 58   return 1;
 59   else return -1;
 60 }
 61 float GetTop2(SqStack2 S)
 62 {
 63     float e;
 64     if(S.top==S.base)  return 0;
 65     e=*(S.top-1);
 66     return e;
 67 }
 68 int Push2(SqStack2 &S,float e)
 69 {
 70   *S.top++=e;
 71   return 1;
 72 }
 73 int Pop2(SqStack2 &S,float &e)
 74 {
 75   if(S.top==S.base)  return 0;
 76   e=*--S.top;
 77   return 1;
 78 }
 79 //转化的操作过程 
 80 float Operate(float a,char theta,float b)
 81 {
 82     switch(theta)
 83     {
 84         case '+': return a+b;
 85         case '-': return a-b;
 86         case '*': return a*b;
 87         case '/': return a/b;
 88         default: return 0;
 89     }
 90 }
 91 int level(char c,int i)
 92 {
 93     switch(c)
 94     {
 95         case '=': return 1;
 96         case ')': return i?2:5;
 97         case '+':
 98         case '-': return 3;
 99         case '*':
100         case '/': return 4;
101         case '(': return i?5:2;
102         default : return 0;
103     }
104 }
105 char Precede(char c1,char c2)
106 {
107     int k=(c1=='('&&c2=='=')||(c1=='='&&c2==')')||(c1==')'&&c2=='(');
108     if(!k)
109     {
110         if(c1=='('&&c2==')') return '=';
111         else 
112         {
113             if(level(c1,0)<level(c2,1)) return '<';
114             else return '>';
115         } 
116     }
117     return 0;
118 } 
119 float EvaluateExpression()
120 {
121     SqStack1 OPTR; SqStack2 OPND;
122     char theta,c,x; float a,b,k,t; char *p1,*p=s;
123     InitStack1(OPTR); Push1(OPTR,'=');
124     InitStack2(OPND); c=*p;
125     while(c!='='||GetTop1(OPTR)!='=')
126     {
127         if(!level(c,1))
128         {
129             for(t=k=0;!level(c,1)&&c!='.';c=*++p)
130                 k=10*k+c-'0';
131             if(c=='.')
132             {
133                 while(!level(*++p,1)); c=*p;
134                 for(p1=p-1;*p1!='.';p1--)
135                 t=0.1*t+*p1-'0';
136                 t*=0.1;
137             }
138             Push2(OPND,k+t);
139         }
140         else
141         switch(Precede(GetTop1(OPTR),c))
142         {
143             case'<': Push1(OPTR,c);c=*++p;break;
144             case'=': Pop1(OPTR,x); c=*++p;break;
145             case'>': 
146                      Pop1(OPTR,theta);
147                      Pop2(OPND,b); Pop2(OPND,a);
148                      Push2(OPND,Operate(a,theta,b));
149         }
150     }
151     return GetTop2(OPND);
152 }
153 //主函数 
154 int main()
155 {
156     int n;
157     scanf("%d%*c",&n);
158     while(n--)
159     {
160         gets(s);
161         printf("%.2f\n",EvaluateExpression());
162     }
163     return 0;
164 }

 

posted on 2012-08-02 08:36  小花熊  阅读(347)  评论(0编辑  收藏  举报

导航