Boolean Expressions(表达式求值)
来源:互联网 发布:谷歌人工智能 开源 编辑:程序博客网 时间:2024/06/06 17:57
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: FExpression 2: VExpression 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 11000struct 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'; else sum.num='F'; } } if(t.op=='&') { if(t1!=t2) sum.num='F'; else { if(t1=='V') sum.num='V'; else sum.num='F'; } } s.push(sum); } else { t1=s.top().num; s.pop(); sum.flog=1; if(t1=='V') sum.num='F'; else sum.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;}
阅读全文
0 0
- Boolean Expressions(表达式求值)
- poj 2106 Boolean Expressions(特殊的表达式求值)
- POJ 2106-Boolean Expressions,双栈运用类似表达式求值!
- poj 2106 Boolean Expressions(字符串bool表达式求值)
- 前缀表达式求值(Prefix expressions)
- (栈的应用5.2.2)POJ 2106 Boolean Expressions(表达式求值)
- 索引布尔表达式 [ Indexing Boolean Expressions ]
- Boolean Expressions
- Boolean Expressions
- Boolean Expressions
- Boolean Expressions
- Boolean Expressions
- Boolean Expressions
- Boolean expressions
- POJ 2106 Boolean Expressions(模拟+LL1)
- POJ---2106-Boolean Expressions(stack)
- poj 2106 Boolean Expressions(模拟)
- POJ2106 Boolean Expressions(模拟,双栈法)
- 基于Scrapy的爬虫爬取京东商品信息与评论
- POJ 3750 小孩报数问题(约瑟夫问题 水题 考验细节、编码能力)
- CNN卷积神经网络基础
- 《零基础入门学习Python》学习过程笔记【014字符串的使用】
- Vue mock & vue-resource & JSONP
- Boolean Expressions(表达式求值)
- python爬虫慕课网实例(二)
- 邮箱不可用 550 User has no permission
- Hadoop集群环境搭建笔记
- Winform 巧用组合界面组合,实现业务解耦和复杂界面呈现
- poj 2376
- Unity3D学习笔记之AssetBundle包加密
- hpuoj【1053】进制转换【水题】
- css浮动