Boolean Expressions POJ - 2106 (表达式求值)
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 (表达式求值)的更多相关文章
- 利用Z.Expressions.Eval表达式求值
Z.Expression.Eval是一个开源的(OpenSource),可扩展的(Extensible),超轻量级(Super lightweight)的公式化语言解析执行工具包. 使用方法:1.从n ...
- 利用栈实现算术表达式求值(Java语言描述)
利用栈实现算术表达式求值(Java语言描述) 算术表达式求值是栈的典型应用,自己写栈,实现Java栈算术表达式求值,涉及栈,编译原理方面的知识.声明:部分代码参考自茫茫大海的专栏. 链栈的实现: pa ...
- java实现算术表达式求值
需要根据配置的表达式(例如:5+12*(3+5)/7.0)计算出相应的结果,因此使用java中的栈利用后缀表达式的方式实现该工具类. 后缀表达式就是将操作符放在操作数的后面展示的方式,例如:3+2 后 ...
- Matrix Chain Multiplication(表达式求值用栈操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...
- Aviator 表达式求值引擎开源框架
简介¶ Aviator是一个高性能.轻量级的java语言实现的表达式求值引擎,主要用于各种表达式的动态求值.现在已经有很多开源可用的java表达式求值引擎,为什么还需要Avaitor呢? Aviato ...
- 蓝桥杯算法训练 java算法 表达式求值
问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输出格式 输出这个表达式的值. 样例输入 1-2+3*(4-5) 样例输出 - ...
- leetcode算法学习----逆波兰表达式求值(后缀表达式)
下面题目是LeetCode算法:逆波兰表达式求值(java实现) 逆波兰表达式即后缀表达式. 题目: 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式.同 ...
- 表达式求值(noip2015等价表达式)
题目大意 给一个含字母a的表达式,求n个选项中表达式跟一开始那个等价的有哪些 做法 模拟一个多项式显然难以实现那么我们高兴的找一些素数代入表达式,再随便找一个素数做模表达式求值优先级表 - ( ) + ...
- 用Python3实现表达式求值
一.题目描述 请用 python3 编写一个计算器的控制台程序,支持加减乘除.乘方.括号.小数点,运算符优先级为括号>乘方>乘除>加减,同级别运算按照从左向右的顺序计算. 二.输入描 ...
随机推荐
- JQuery对RadioButton和CheckButton的操作
js对RadioButton和CheckButton的操作,在网站开发中会经常遇到,而JQuery操作RadioButton和CheckButton非常便捷.小编觉得网站开发人员有必要熟练掌握.所以小 ...
- AngularJs几种服务区别
下面说说这几种函数之间的区别: 函数 定义 适合场景 provider(name, Object OR constructor() ) 一个可配置的.有复杂逻辑的服务.如果你传递了一个对象,那么它应该 ...
- Broken Necklace
Description 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个 例子: 1 2 1 2 r b b r b ...
- 大数mod的技巧
1.mod 3 将各个位上的数字相加对3求余. 2.mod 11 设这个数为abcdefghijklmnopqrst. ans=(t-s+r-q+p-o+n-m+l-k+j-i(以此类推))mod 1 ...
- Django初探(模板渲染、模板语音、simple_tag、母版子版、静态配置文件)
一.首先我们用PyCharm来创建一个Django项目 终端命令:django-admin startproject sitename 图形创建: 这样一个Django项目就创建完成了,上面可以看 ...
- VS 2010 应用程序无法启动
其实一般遇到这种问题, 不管是debug还是release, 也不用看提示的内存地址, 首先应该想到库是否包含正确. 一个可能的错误就是32位或64位不匹配的错误. 比如环境变量设的是64位的Open ...
- python 输出 a+b
AC代码: 单组输入: s=input().split() print(int(s[0])+int(s[1]))
- Linux 下解决安装多个node冲突的问题(重新安装node)
一个系统中不经意安装了多个node版本,结果更新后还是原来的版本,下面思考一下解决办法: 敲黑板: 1. nodejs 用 包管理器安装一般在 /usr/local/bin 2. 查看当前目录下的no ...
- 超级ping(多线程版)
发现学校公共wifi的ip段是10.1.0-255.0-255段的,还是之前的思路批量ping一波. 其实可以使用nmap的.但是脚本写都写了.是吧.你懂的. #!/usr/bin/env pytho ...
- 【并行计算】用MPI进行分布式内存编程(二)
通过上一篇中,知道了基本的MPI编写并行程序,最后的例子中,让使用0号进程做全局的求和的所有工作,而其他的进程却都不工作,这种方式也许是某种特定情况下的方案,但明显不是最好的方案.举个例子,如果我们让 ...