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.

Input

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.

Output

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.

Sample Input

( V | V ) & F & ( F| V)

!V | V & V & !F & (F | V ) & (!F | F | !V & V)

(F&F|V|!V&!F&!(F|F&V))

Sample Output

Expression 1: F

Expression 2: V

Expression 3: V

//栈
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; const int INF=1e9+7;
const int maxn=110;
typedef long long ll; char xsk[maxn],sum[maxn];
int cnt,pos;
char c; int insert(int t)
{
while(cnt&&xsk[cnt-1]==3)
{
t=!t;
--cnt;
}
sum[pos++]=t;
} void solve()
{
int a=sum[--pos];
int b=sum[--pos];
int d=xsk[--cnt];
int ss=(a&b);
if(d==1) ss=(a|b);
insert(ss);
} int main()
{
int k=1;
while((c=getchar())!=EOF)
{
cnt=pos=0;
do
{
if(c=='(')
{
xsk[cnt++]=0;
}
else if(c==')')
{
while(cnt&&xsk[cnt-1]!=0)
solve();
--cnt;
insert(sum[--pos]);
}
else if(c=='!')
{
xsk[cnt++]=3;
}
else if(c=='&')
{
while(cnt&&xsk[cnt-1]>=2)
solve();
xsk[cnt++]=2;
}
else if(c=='|')
{
while(cnt&&xsk[cnt-1]>=1)
solve();
xsk[cnt++]=1;
}
else if(c=='V'||c=='F')
{
insert(c=='V'?1:0);
}
}
while((c=getchar())!='\n'&&c!=EOF);
while(cnt) solve();
printf("Expression %d: %c\n",k++,(sum[0]?'V':'F'));
}
return 0;
} //递归,看的别人的代码,能看就看,不懂就算了
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char s[100001]= {0};
int my_cnt=0;
bool expression_bool();
bool term_bool();
bool factor_bool(); bool expression_bool() //求一个表达式的值
{
bool result=term_bool();//求第一项的值
bool more=true;
while(more)
{
char op=s[my_cnt];//看一个字符,不取走
if(op=='&'||op=='|')
{
my_cnt++;//从数组中取走一个字符
bool value=term_bool();
if(op=='&') result=result&value;
else result=result|value;
}
else
{
more=false;
}
}
return result;
} bool term_bool() //因为!的运算优先级更高,所以把!xxx也当成一个项
{
bool result;
char op=s[my_cnt];
if(op=='!')
{
my_cnt++;
result=!factor_bool();
}
else
{
result=factor_bool();
} return result;
} bool factor_bool() //求一个因子的值
{
bool result;
char c=s[my_cnt];
if(c=='(') //如果该因子是由括号括起来的表达式组成的话
{
my_cnt++;
result=expression_bool();
my_cnt++;
}
else if(c=='V')
{
result=true;
my_cnt++;
}
else if(c=='F')
{
result=false;
my_cnt++;
}
else if(c=='!') //出现了!,说明读取到了一个因子
{
result=term_bool();
} return result;
} int main()
{
int k=0;
while(cin.getline(s,100000))
{
char t[100001]= {0};
int len=strlen(s);
for(int i=0,k=0; i<len; ++i)
{
if(s[i]!=' ')
{
t[k++]=s[i];
}
} len=strlen(t);
for(int i=0; i<len; ++i)
{
s[i]=t[i];
}
s[len]='\0';
//到这里输入中的空格已经被去除干净了
cout<<"Expression "<<++k<<": "<<(expression_bool()?"V":"F")<<endl;
//初始化工作
my_cnt=0;
memset(s,0,100000);
}
return 0;
}

Boolean Expressions POJ - 2106 (表达式求值)的更多相关文章

  1. 利用Z.Expressions.Eval表达式求值

    Z.Expression.Eval是一个开源的(OpenSource),可扩展的(Extensible),超轻量级(Super lightweight)的公式化语言解析执行工具包. 使用方法:1.从n ...

  2. 利用栈实现算术表达式求值(Java语言描述)

    利用栈实现算术表达式求值(Java语言描述) 算术表达式求值是栈的典型应用,自己写栈,实现Java栈算术表达式求值,涉及栈,编译原理方面的知识.声明:部分代码参考自茫茫大海的专栏. 链栈的实现: pa ...

  3. java实现算术表达式求值

    需要根据配置的表达式(例如:5+12*(3+5)/7.0)计算出相应的结果,因此使用java中的栈利用后缀表达式的方式实现该工具类. 后缀表达式就是将操作符放在操作数的后面展示的方式,例如:3+2 后 ...

  4. Matrix Chain Multiplication(表达式求值用栈操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...

  5. Aviator 表达式求值引擎开源框架

    简介¶ Aviator是一个高性能.轻量级的java语言实现的表达式求值引擎,主要用于各种表达式的动态求值.现在已经有很多开源可用的java表达式求值引擎,为什么还需要Avaitor呢? Aviato ...

  6. 蓝桥杯算法训练 java算法 表达式求值

    问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输出格式 输出这个表达式的值. 样例输入 1-2+3*(4-5) 样例输出 - ...

  7. leetcode算法学习----逆波兰表达式求值(后缀表达式)

    下面题目是LeetCode算法:逆波兰表达式求值(java实现) 逆波兰表达式即后缀表达式. 题目:  有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式.同 ...

  8. 表达式求值(noip2015等价表达式)

    题目大意 给一个含字母a的表达式,求n个选项中表达式跟一开始那个等价的有哪些 做法 模拟一个多项式显然难以实现那么我们高兴的找一些素数代入表达式,再随便找一个素数做模表达式求值优先级表 - ( ) + ...

  9. 用Python3实现表达式求值

    一.题目描述 请用 python3 编写一个计算器的控制台程序,支持加减乘除.乘方.括号.小数点,运算符优先级为括号>乘方>乘除>加减,同级别运算按照从左向右的顺序计算. 二.输入描 ...

随机推荐

  1. Linux系统调用和库函数

    #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unist ...

  2. HTML入门(二)表格_字体_超链接_布局

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. UIWebView---iOS-Apple苹果官方文档翻译

    CHENYILONG Blog UIWebView---iOS-Apple苹果官方文档翻译 UIWebView 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博h ...

  4. 简易安装sqoop

    版本 :hive-0.13.1-cdh5.3.6.tar.gz 1:解压 然后 进到 conf 目录 修改 sqoop-env.sh   2:如果使用mysql 数据库 要将 mysql驱动包拷贝到 ...

  5. 【leetcode 简单】第三十九题 Excel表列名称

    给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> ...

  6. 前端QRCode.js生成二维码(解决长字符串模块和报错问题)

    QRCode 用法 1.使用npm安装到你的项目中 npm install qrcode2 --save 使用commonjs或者es6模块方式导入 var QRCode = require('qrc ...

  7. tf.name_scope tf.variable_scope学习

    1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...

  8. ARP投毒攻击

    原理:通过分别伪装成客户机和服务器IP,将自己的MAC地址绑定在IP上,ARP错误的将IP解析为中间人MAC地址,从而来欺骗服务器网关和客户机,使信息必须通过客户机.

  9. Mybatis Common Mapper文件

    表名/条件/字段 都可以传入进去 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mappe ...

  10. 转:PHP环境搭建 - Linux

    本文PHP环境采用,nginx + PHP7 + mysql 5.6 一.安装mysql 5.6 参见:http://www.cnblogs.com/rslai/p/7853465.html 二.Ng ...