【递归】【栈】先修课 计算概论(A)/函数递归练习(2)5:布尔表达式
- 总时间限制:
- 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:布尔表达式的更多相关文章
- 【递归】先修课 计算概论(A) / 函数递归练习(3)2:分解因数
#include<cstdio> using namespace std; bool is_prime(int x) { ;i*i<=x;i++) ) return false; r ...
- Openjudge计算概论——数组逆序重放【递归练习】
/*===================================== 数组逆序重放 总时间限制:1000ms 内存限制:65536kB 描述 将一个数组中的值按逆序重新存放. 例如,原来的顺 ...
- 计算概论(A)/基础编程练习1(8题)/3:晶晶赴约会
#include<stdio.h> int main() { int w; scanf("%d", &w); || w==) { printf("%s ...
- OpenJudge计算概论-取石子游戏
OpenJudge计算概论-取石子游戏[函数递归练习] /*====================================================================== ...
- OpenJudge计算概论-计算书费
/*============================================== 计算书费 总时间限制: 1000ms 内存限制: 65536kB 描述 下面是一个图书的单价表: 计算 ...
- python全栈开发-Day12 三元表达式、函数递归、匿名函数、内置函数
一. 三元表达式 一 .三元表达式 仅应用于: 1.条件成立返回,一个值 2.条件不成立返回 ,一个值 def max2(x,y): #普通函数定义 if x > y: return x els ...
- 计算概论(A)/基础编程练习(数据成分)/3:整数的个数
#include<stdio.h> int main() { ] = {}; // 输入k个正整数 scanf("%d",&k); // 循环读入和进行算术 w ...
- 计算概论(A)/基础编程练习(数据成分)/2:奥运奖牌计数
#include<stdio.h> int main() { // n天的决赛项目 int n; scanf("%d",&n); ] = {}; while ( ...
- 计算概论(A)/基础编程练习(数据成分)/1:短信计费
#include<stdio.h> int main() { // 输入当月发送短信的总次数n和每次短信的字数words int n,words; scanf("%d" ...
随机推荐
- pmap用法小计
By francis_hao Aug 4,2017 pmap-报告进程的内存映射. 概要 pmap [options] pid [...] 描述 pmap命令用来报告一个或多个进程的 ...
- python构建一个项目
二.实验步骤 2.1 实验准备 我们的实验项目名为 factorial. $ mkdir factorial $ cd factorial/ 2.2 主代码 我们给将要创建的 Python 模块取名为 ...
- __cdecl,__stdcall,__fastcall,__pascal,__thiscall 的区别
关于函数的调用规则(调用约定),大多数时候是不需要了解的,但是如果需要跨语言的编程,比如VC写的dll要delphi调用,则需要了解. microsoft的vc默认的是__cdecl方式,而windo ...
- python 调用Linux shell
有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 ...
- jquery——通过name属性查找元素
Js代码 : $("div[id]") 选择所有含有id属性的div元素 $("input[name='newsletter']") 选择所有的name属性 ...
- 进一步认识golang中的并发
如果你成天与编程为伍,那么并发这个名词对你而言一定特别耳熟.需要并发的场景太多了,例如一个聊天程序,如果你想让这个聊天程序能够同时接收信息和发送信息,就一定会用到并发,无论那是什么样的并发. 并发的意 ...
- (十二)Linux内核驱动之poll和select
使用非阻塞 I/O 的应用程序常常使用 poll, select, 每个允许一个进程来决定它是否可读或者写一个或多个文件而不阻塞. 这些调用也可阻塞进程直到任何一个给定集合的文件描述符可用来读或写. ...
- Google Breakpad 之一,跨平台crash 处理上报系统简介
Google Breakpad 之一,跨平台crash 处理上报系统简介 http://blog.csdn.net/wpc320/article/details/8290501 Google Brea ...
- Chrome扩展及应用开发
Chrome扩展及应用开发(电子书) http://www.ituring.com.cn/minibook/950 文档 官方 https://developer.chrome.com/extensi ...
- linux sun/awt/X11GraphicsEnvironment 找不到错误
转载自:http://blog.csdn.net/bolg_hero/article/details/46594915 在Linux下,我们在用Java的图片包来处理图片时,经常遇到java.lang ...