做了两题

Problem D: 逻辑运算

Description

 还记得大学里学过的模电么,今天就让我们将与或非变成一道题吧。

给你一个与或非的表达式,求出这个表达式的值,表达式总共有八种字符。

三种逻辑运算符按照优先级排列如下。

‘!’:表示取反。

‘&’:逻辑与。

‘|’:逻辑或。

两个字符‘T’,‘F‘分别表示true和 false。

另外还有左右括号,空格三种字符。跟一般的表达式一样,括号可以改变优先级。

 

Input

每组数据输入一行字符串,字符串长度小于等于100.

Output

 输出一个数0或1,表示逻辑表达式的答案。

Sample Input

T

Sample Output

1
思路:还是波兰式,关于!这个单目运算符想了半天,后来在它前面加了个废操作数,变成了双目运算符,结果就好了
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <string.h>
 5 #include <stack>
 6 #define maxn 100009
 7 using namespace std;
 8 stack<char>q;
 9 stack<int>p;
10 long long priorit[1000],ans[maxn],h;
11 bool opp[maxn];
12 char ch[maxn];
13 int main()
14 {
15     priorit[(int)'!']=3;
16     priorit[(int)'&']=2;
17     priorit[(int)'|']=1;
18     priorit[(int)'(']=0;
19     while(gets(ch+1)!=NULL)
20     {
21         while(!q.empty())q.pop();
22         while(!p.empty())p.pop();
23         h=0;
24         memset(ans,0,sizeof(ans));
25         memset(opp,0,sizeof(opp));
26         long long len=strlen(ch+1),idx=1,j=1;
27         for(int i=1;i<=len;i++)
28         {
29             if(ch[i]!=' ')ch[j++]=ch[i];
30         }
31         len=j-1;
32         while(idx<=len)
33         {
34             long long num=0,flag=0;
35             if(ch[idx]=='!')
36             {
37                         ans[++h]=2;
38             }
39             if(ch[idx]=='T')num=1,flag=1;
40             if(ch[idx]=='F')num=0,flag=1;
41             if(flag==0)
42             {
43                 if(ch[idx]=='(')q.push('(');
44                 else if(ch[idx]==')')
45                 {
46                     while(!q.empty()&&q.top()!='(')
47                     {
48                         ans[++h]=-(long long)q.top();
49                         opp[h]=1;
50                         q.pop();
51                     }
52                     q.pop();
53                 }
54                 else
55                 {
56                     while(!q.empty()&&priorit[(long long)q.top()]>=priorit[(int)ch[idx]])
57                     {
58                         ans[++h]=-(int)q.top();
59                         opp[h]=1;
60                         q.pop();
61                     }
62                     q.push(ch[idx]);
63                 }
64             }
65             else
66             {
67                 ans[++h]=num;
68             }
69             idx++;
70         }
71         while(!q.empty())
72         {
73             ans[++h]=-(long long)q.top();
74             opp[h]=1;
75             q.pop();
76         }
77         for(int i=1;i<=h;i++)
78         {
79            // printf("%I64d ",ans[i]);
80             if(opp[i]==0)p.push(ans[i]);
81             else
82             {
83                 long long u=p.top();
84                 p.pop();
85                 long long v=p.top();
86                 p.pop();
87                 if(ans[i]==-(int)'!')
88                 {
89                         if(u==2)p.push(!v);
90                         else p.push(!u);
91                 }
92                 if(ans[i]==-(int)'&')p.push(u&v);
93                 if(ans[i]==-(int)'|')p.push(u|v);
94             }
95         }
96         if(!p.empty())printf("%d\n",p.top());
97         else printf("0\n");
98     }
99 }
View Code

相关文章: