总时间限制:
1000ms
内存限制:
65536kB
描述

输入一个布尔表达式,请你输出它的真假值。
比如:( V | V ) & F & ( F | V )

V表示true,F表示false,&表示与,|表示或,!表示非。

上式的结果是F

输入
输入包含多行,每行一个布尔表达式,表达式中可以有空格,总长度不超过1000
输出
对每行输入,如果表达式为真,输出"V",否则出来"F"
样例输入
( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))
样例输出
F
V
V 递归和栈的基础应用。
代码丑飞……
Code:
 #include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
string s;
int len;
bool work(int x)
{
char lastopt='&';
bool res=true;
bool sym=true;
for(int i=x;i<len;i++)
if(s[i]==')') return res;
else if(s[i]==' ') continue;
else if(s[i]=='!') sym=false;
else if(s[i]=='&') lastopt='&';
else if(s[i]=='|') lastopt='|';
else if(s[i]=='V')
{
bool tmp=true;
if(!sym) {tmp=false;sym=true;}
if(lastopt=='&') res&=tmp;
else res|=tmp;
}
else if(s[i]=='F')
{
bool tmp=false;
if(!sym) {tmp=true;sym=true;}
if(lastopt=='&') res&=tmp;
else res|=tmp;
}
else
{
bool tmp=work(i+);
if(!sym) {tmp^=true;sym=true;}
if(lastopt=='&') res&=tmp;
else res|=tmp;
int top=;
for(int j=i+;j<len;j++)
{
if(s[j]=='(') top++;
else if(s[j]==')') top--;
if(!top)
{
i=j;
break;
}
}
}
}
int main()
{
while()
{
s.clear();
getline(cin,s);
len=s.length();
if(!len) break;
char lastopt='&';
bool res=true;
bool sym=true;
for(int i=;i<len;i++)
if(s[i]==' ') continue;
else if(s[i]=='(')
{
bool tmp=work(i+);
if(!sym) {tmp^=true;sym=true;}
if(lastopt=='&') res&=tmp;
else res|=tmp;
int top=;
for(int j=i+;j<len;j++)
{
if(s[j]=='(') top++;
else if(s[j]==')') top--;
if(!top)
{
i=j;
break;
}
}
}
else if(s[i]=='!') sym=false;
else if(s[i]=='&') lastopt='&';
else if(s[i]=='|') lastopt='|';
else if(s[i]=='V')
{
bool tmp=true;
if(!sym) {tmp=false;sym=true;}
if(lastopt=='&') res&=tmp;
else res|=tmp;
}
else if(s[i]=='F')
{
bool tmp=false;
if(!sym) {tmp=true;sym=true;}
if(lastopt=='&') res&=tmp;
else res|=tmp;
}
putchar( res ? 'V' : 'F' );puts("");
}
return ;
}

【递归】【栈】先修课 计算概论(A)/函数递归练习(2)5:布尔表达式的更多相关文章

  1. 【递归】先修课 计算概论(A) / 函数递归练习(3)2:分解因数

    #include<cstdio> using namespace std; bool is_prime(int x) { ;i*i<=x;i++) ) return false; r ...

  2. Openjudge计算概论——数组逆序重放【递归练习】

    /*===================================== 数组逆序重放 总时间限制:1000ms 内存限制:65536kB 描述 将一个数组中的值按逆序重新存放. 例如,原来的顺 ...

  3. 计算概论(A)/基础编程练习1(8题)/3:晶晶赴约会

    #include<stdio.h> int main() { int w; scanf("%d", &w); || w==) { printf("%s ...

  4. OpenJudge计算概论-取石子游戏

    OpenJudge计算概论-取石子游戏[函数递归练习] /*====================================================================== ...

  5. OpenJudge计算概论-计算书费

    /*============================================== 计算书费 总时间限制: 1000ms 内存限制: 65536kB 描述 下面是一个图书的单价表: 计算 ...

  6. python全栈开发-Day12 三元表达式、函数递归、匿名函数、内置函数

    一. 三元表达式 一 .三元表达式 仅应用于: 1.条件成立返回,一个值 2.条件不成立返回 ,一个值 def max2(x,y): #普通函数定义 if x > y: return x els ...

  7. 计算概论(A)/基础编程练习(数据成分)/3:整数的个数

    #include<stdio.h> int main() { ] = {}; // 输入k个正整数 scanf("%d",&k); // 循环读入和进行算术 w ...

  8. 计算概论(A)/基础编程练习(数据成分)/2:奥运奖牌计数

    #include<stdio.h> int main() { // n天的决赛项目 int n; scanf("%d",&n); ] = {}; while ( ...

  9. 计算概论(A)/基础编程练习(数据成分)/1:短信计费

    #include<stdio.h> int main() { // 输入当月发送短信的总次数n和每次短信的字数words int n,words; scanf("%d" ...

随机推荐

  1. Dubbo入门介绍---搭建一个最简单的Demo框架

    Dubbo入门---搭建一个最简单的Demo框架 置顶 2017年04月17日 19:10:44 是Guava不是瓜娃 阅读数:320947 标签: dubbozookeeper 更多 个人分类: D ...

  2. 如何最快地实现 ALTER TABLE

    如果您不了解ALTER TABLE的语法,可以先参考: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html 使用ALTER TABLE 可以 ...

  3. POJ2912:Rochambeau(带权并查集)

    Rochambeau Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5208   Accepted: 1778 题目链接:h ...

  4. android studio的弹出层

    <activity android:name=".SecondActivity" android:theme="@style/Theme.AppCompat.Dia ...

  5. codeforces 1015E1&&E2

    E1. Stars Drawing (Easy Edition) time limit per test 3 seconds memory limit per test 256 megabytes i ...

  6. 简单配置oracle11g

    一.配置 Systemd file(开机可以自动oracle,也可以查看启动状态) a.定义环境变量 [oracle@ol7 ~]$ cat /etc/sysconfig/DB11G.oracledb ...

  7. idea讲web项目部署到tomcat,热部署

    idea是自动保存文件的,不需要ctrl+s手动保存. idea使用不习惯,修改了jsp文件后,刷新浏览器并没有立刻显示出来,而是要重新编译一下代码,重新部署才会出现. 在idea tomcat 中s ...

  8. 【BZOJ2223&&3524】PATULJCI [主席树]

    PATULJCI Time Limit: 10 Sec  Memory Limit: 259 MB[Submit][Status][Discuss] Description Input 第一行两个整数 ...

  9. 【洛谷 P3805】 【模板】manacher算法

    题目链接 manacher算法:在线性时间内求一个字符串中所有/最长回文串的算法. 先来考虑一下暴力的算法,枚举每个中点,向两边扩展,时间复杂度\(O(n^2)\). 来分析下此算法的缺点. 1.因为 ...

  10. linux下面which whereis find locate的使用

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.这些是从网上找到的资料,因为有时很长时间不会用到,当要用的时候经常弄混了,所以放到这里方便使用. which    ...