poj 3295 前缀表达式求值

前缀表达式求值,和后缀一样,只不过是从后往前读

 1 #include <iostream>
2 #include <string.h>
3 #include <stdio.h>
4 #include <stack>
5 #include <algorithm>
6 using namespace std;
7
8 bool change(char s[],bool val[])
9 {
10 stack <bool> nd;
11 bool a,b;
12 int len=strlen(s),i,j;
13 for(i=0;i<len;)
14 {
15 if(s[i]=='p' || s[i]=='q' || s[i]=='r' || s[i]=='s' || s[i]=='t'){nd.push(val[s[i]-'p']);i++;}
16 else
17 {
18 switch(s[i])
19 {
20 case 'K': a=nd.top();nd.pop();
21 b=nd.top();nd.pop();
22 nd.push(a&&b);
23 break; //&
24 case 'A': a=nd.top();nd.pop();
25 b=nd.top();nd.pop();
26 nd.push(a||b);
27 break; //|
28 case 'N': a=nd.top();nd.pop();
29 nd.push(!a);
30 break;// !
31 case 'C': a=nd.top();nd.pop();
32 b=nd.top();nd.pop();
33 nd.push(!b||a);
34 break; // !|
35 case 'E': a=nd.top();nd.pop();
36 b=nd.top();nd.pop();
37 nd.push(b==a);
38 break;//==
39 }
40 i++;
41 }
42 }
43 return nd.top();
44 }
45
46 int main()
47 {
48 char s[111];
49 int i,j;
50 bool val[32],flag;
51 // freopen("in.txt","r",stdin);
52 while(gets(s))
53 {
54 flag=1;
55 if(s[0]=='0')
56 break;
57 int len=strlen(s)-1;
58 for(i=0;i<=len/2;i++)
59 swap(s[i],s[len-i]);
60 for(i=0;i<=31;i++)
61 {
62 memset(val,0,sizeof(val));
63 for(j=0;j<5;j++)
64 val[j]=(i&(1<<j));
65 if(!change(s,val)){flag=0;break;}
66 }
67 if(flag)
68 printf("tautology\n");
69 else
70 printf("not\n");
71
72 }
73
74 return 0;
75 }



posted on 2012-04-05 16:35  Inpeace7  阅读(247)  评论(0编辑  收藏  举报

导航