当前位置: 代码迷 >> 综合 >> Boolean Expressions(表达式求值)
  详细解决方案

Boolean Expressions(表达式求值)

热度:31   发布时间:2023-11-24 22:39:00.0

1:Boolean Expressions

  • 查看
  • 提交
  • 统计
  • 提问
总时间限制:
1000ms
内存限制:
65536kB
描述
The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next:
Expression: ( V | V ) & F & ( F | V )

where V is for True, and F is for False. The expressions may include the following operators: ! for not , & for and, | for or , the use of parenthesis for operations grouping is also allowed.

To perform the evaluation of an expression, it will be considered the priority of the operators, the not having the highest, and the or the lowest. The program must yield V or F , as the result for each expression in the input file.
输入
The expressions are of a variable length, although will never exceed 100 symbols. Symbols may be separated by any number of spaces or no spaces at all, therefore, the total length of an expression, as a number of characters, is unknown.

The number of expressions in the input file is variable and will never be greater than 20. Each expression is presented in a new line, as shown below.
输出
For each test expression, print "Expression " followed by its sequence number, ": ", and the resulting value of the corresponding test expression. Separate the output for consecutive test expressions with a new line.

Use the same format as that shown in the sample output shown below.
样例输入
( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))
样例输出
Expression 1: F
Expression 2: V
Expression 3: V
来源
México and Central America 2004
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <stack>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
using namespace std;
#define INF 0x3f3f3f3f
#define mod 1000000007
#define N 11000
struct node{char op;char num;int flog;
}t,sum;
char ch[N];
stack<node>s;
queue<node>q;
map<char,int>mp;
void change()
{int len=strlen(ch);for(int i=0;i<len;) {if(ch[i]==' ') {i++;continue;}else if(ch[i]=='V'||ch[i]=='F') {t.flog=1;t.num=ch[i];q.push(t);i++;while(!s.empty()&&s.top().op=='!') {//如果栈顶是!直接取出放在队列q.push(s.top());s.pop();}}else if(ch[i]=='('||ch[i]==')') {if(ch[i]=='(') {t.flog=0;t.op='(';s.push(t);}else {while(!s.empty()&&s.top().op!='(') {q.push(s.top());s.pop();}s.pop();}i++;}else {t.flog=0;if(ch[i]=='!') {//如果是非直接压入栈t.op=ch[i];s.push(t);}else {while(!s.empty()&&s.top().op!='('&&mp[ch[i]]<=mp[s.top().op]) {q.push(s.top());s.pop();}t.op=ch[i];s.push(t);}i++;}}while(!s.empty()) {q.push(s.top());s.pop();}
}
char cal()
{char t1,t2;while(!q.empty()) {t=q.front();q.pop();t.num;if(t.flog==1)   s.push(t);else if(t.op=='|'||t.op=='&') {t2=s.top().num;s.pop();t1=s.top().num;s.pop();sum.flog=1;if(t.op=='|') {if(t1!=t2)sum.num='V';else {if(t1=='V')sum.num='V';elsesum.num='F';}}if(t.op=='&') {if(t1!=t2)sum.num='F';else {if(t1=='V')sum.num='V';elsesum.num='F';}}s.push(sum);}else {t1=s.top().num;s.pop();sum.flog=1;if(t1=='V')sum.num='F';elsesum.num='V';s.push(sum);}}return s.top().num;
}
int main()
{mp['|']=1;mp['&']=2;mp['!']=3;int T=1;while(gets(ch)) {while(!s.empty())s.pop();change();printf("Expression %d: %c\n",T++,cal());}return 0;
}


  相关解决方案