hdu 3596 表达式求值

  1 #include <iostream>
2 #include <stack>
3 #include <string.h>
4 #include <stdio.h>
5 #include <cmath>
6 using namespace std;
7
8 const int maxx=10000;
9 const int inf=0x7fffffff;
10
11 int precede(char c)
12 {
13 switch(c)
14 {
15 case '+':
16 case '-':return 1;
17 case '^':
18 case '*':
19 case '/':return 2;
20 default:return 0;
21 }
22 }
23
24 double getvalue(char c,double a,double b)
25 {
26 switch(c)
27 {
28 case '+':return b+a;
29 case '-':return b-a;
30 case '*':return b*a;
31 case '/':if(a==0) return inf; return b/a;
32 case '^':return pow(b,a);
33 }
34 }
35
36 bool isnum(char c)
37 {
38 if((c>='0' && c<='9')|| c=='.') return 1;
39 else return 0;
40 }
41
42 void change(char c[],char s[])
43 {
44 int i,cnt=-1,len=strlen(c);
45 for(i=0;i<len;)
46 {
47 if((c[i]=='+' || c[i]=='-') && cnt==-1)
48 {
49 s[++cnt]='0';
50 s[++cnt]=c[i];
51 i++;
52 }
53 else if(c[i]=='(')
54 {
55 s[++cnt]=c[i];
56 i++;
57 while(c[i]==' ') i++;
58 if(c[i]=='+' || c[i]=='-')
59 {
60 s[++cnt]='0';
61 s[++cnt]=c[i];
62 i++;
63 }
64 }
65 else if(c[i]==' '||c[i]=='\t') i++;
66 else {s[++cnt]=c[i];i++;}
67 }
68 s[++cnt]='\0';
69 }
70
71 double strtonum(char s[],int &i)
72 {
73 double cas=0.1,dot=0.0,a=0.0;
74 if(s[i]=='.')
75 {
76 i++;
77 while(isnum(s[i]))
78 {
79 dot=dot+(s[i]-'0')*cas;
80 cas*=0.1;
81 i++;
82 }
83 }
84 else
85 {
86 a=(s[i]-'0')*1.0;
87 i++;
88 while(isnum(s[i]) && s[i]!='.')
89 {
90 a=a*10.0+(s[i]-'0')*1.0;
91 i++;
92 }
93 if(s[i]=='.')
94 {
95 i++;
96 while(isnum(s[i]))
97 {
98 dot=dot+(s[i]-'0')*cas;
99 cas*=0.1;
100 i++;
101 }
102 }
103 }
104 return a+dot;
105 }
106
107 double result(char s[])
108 {
109 stack <char> ope;
110 stack <double> nd;
111 char c;
112 double a,b,ans;
113 int i=0;
114 bool flag=0;
115 if(s[i]=='+') i++;
116 else if(s[i]=='-') {i++;a=strtonum(s,i);nd.push(-a);}
117 while(s[i]!='\0')
118 {
119 if(isnum(s[i]))
120 {
121 a=strtonum(s,i);
122 nd.push(a);
123 }
124 else if(s[i]=='(')
125 {
126 ope.push(s[i]);
127 i++;
128 if(s[i]=='+') i++;
129 else if(s[i]=='-') {i++;a=strtonum(s,i);nd.push(-a);}
130
131 }
132 else if(s[i]==')')
133 {
134 while(ope.top()!='(')
135 {
136 c=ope.top();ope.pop();
137 a=nd.top();nd.pop();
138 b=nd.top();nd.pop();
139 ans=getvalue(c,a,b);
140
141 if(ans==inf)
142 { flag=1;break;}
143 nd.push(ans);
144 }
145 ope.pop();
146 i++;
147 }
148 else if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || s[i]=='^')
149 {
150 if(!ope.empty() && precede(s[i])<=precede(ope.top()))//若当前符号优先级比栈里边的小,则一直弹栈
151 {
152 c=ope.top();ope.pop();
153 a=nd.top();nd.pop();
154 b=nd.top();nd.pop();
155 ans=getvalue(c,a,b);
156
157 if(ans==inf)
158 {flag=1;break;}
159 nd.push(ans);
160 }
161 else
162 {
163 ope.push(s[i]);
164 i++;
165 }
166 }
167 else i++;
168 }
169 while(!ope.empty())
170 {
171 c=ope.top();ope.pop();
172 a=nd.top();nd.pop();
173 b=nd.top();nd.pop();
174 ans=getvalue(c,a,b);
175 if(ans==inf)
176 {flag=1;break;}
177 nd.push(ans);
178 }
179 if(flag==1) return inf;
180 return nd.top();
181 }
182
183 bool expression(char str[])
184 {
185 int len = strlen(str);
186 bool flag = false;
187 int zk = 0, yk = 0;
188 if(len == 1)
189 {
190 if(str[0]>='0'&&str[0]<='9') return 1;
191 else return 0;
192 }
193 for(int i = 0; i < len-1; i++)
194 {
195 if(str[i] == ' ') continue;
196 else if(str[i]>='0'&&str[i]<='9')
197 {
198 if(str[i+1]=='(')
199 {
200 flag = true;
201 break;
202 }
203 }
204 else if(str[i] == '(')
205 {
206 if(str[i+1]==')')
207 {
208 flag = true;
209 break;
210 }
211 zk++;
212 }
213 else if(str[i] == ')')
214 {
215 if(zk == 0)
216 {
217 flag = true;
218 break;
219 }
220 if(str[i+1]>='0'&&str[i+1]<='9')
221 {
222 flag = true;
223 break;
224 }
225 if(str[i+1]=='(')
226 {
227 flag = true;
228 break;
229 }
230 zk--;
231 }
232 else//+-*/
233 {
234 if(str[i+1]=='+'||str[i+1]=='-'||str[i+1]=='*'||str[i+1]=='/' || str[i+1]=='^')
235 {
236 flag = true;
237 break;
238 }
239 if(str[i+1]==')')
240 {
241 flag = true;
242 break;
243 }
244 }
245 }
246 if(str[len-1]=='(') return 0;
247 else if(str[len-1]==')'&& zk!=1) return 0;
248 else if(str[len-1]=='+'||str[len-1]=='-'||str[len-1]=='*'||str[len-1]=='/'||str[len-1]=='^') return 0;
249 else if(str[len-1]!=')'&&zk) return 0;
250 else if(flag) return 0;
251 else return 1;
252 }
253
254 int main()
255 {
256 char s[maxx+10],c[maxx+10];
257 double ans;
258 // freopen("in.txt","r",stdin);
259 while(gets(c))
260 {
261 change(c,s);
262 if(!expression(s)){printf("The teacher is so lazy!\n");continue;}
263 ans=result(s);
264 if(ans==inf)
265 printf("The teacher is so lazy!\n");
266 else
267 printf("%.8lf\n",ans);
268
269 }
270 return 0;
271 }

posted on 2012-04-02 22:04  Inpeace7  阅读(567)  评论(0编辑  收藏  举报

导航