zoukankan      html  css  js  c++  java
  • 【递归】【栈】先修课 计算概论(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:
      1 #include<cstring>
      2 #include<cstdio>
      3 #include<iostream>
      4 using namespace std;
      5 string s;
      6 int len;
      7 bool work(int x)
      8 {
      9     char lastopt='&';
     10     bool res=true;
     11     bool sym=true;
     12     for(int i=x;i<len;i++)
     13       if(s[i]==')') return res;
     14       else if(s[i]==' ') continue;
     15       else if(s[i]=='!') sym=false;
     16       else if(s[i]=='&') lastopt='&';
     17       else if(s[i]=='|') lastopt='|';
     18       else if(s[i]=='V')
     19         {
     20           bool tmp=true;
     21           if(!sym) {tmp=false;sym=true;}
     22           if(lastopt=='&') res&=tmp;
     23           else res|=tmp;
     24         }
     25       else if(s[i]=='F')
     26         {
     27           bool tmp=false;
     28           if(!sym) {tmp=true;sym=true;}
     29           if(lastopt=='&') res&=tmp;
     30           else res|=tmp;
     31         }
     32       else 
     33         {
     34           bool tmp=work(i+1);
     35           if(!sym) {tmp^=true;sym=true;}
     36           if(lastopt=='&') res&=tmp;
     37           else res|=tmp;
     38           int top=1;
     39           for(int j=i+1;j<len;j++)
     40             {
     41               if(s[j]=='(') top++;
     42               else if(s[j]==')') top--;
     43               if(!top)
     44                 {
     45                   i=j;
     46                   break;
     47                 }
     48             }
     49         }
     50 }
     51 int main()
     52 {
     53     while(1)
     54       {
     55           s.clear();
     56           getline(cin,s);
     57           len=s.length();
     58           if(!len) break;
     59           char lastopt='&';
     60         bool res=true;
     61         bool sym=true;
     62           for(int i=0;i<len;i++)
     63             if(s[i]==' ') continue;
     64             else if(s[i]=='(')
     65               {
     66                 bool tmp=work(i+1);
     67                 if(!sym) {tmp^=true;sym=true;}
     68                 if(lastopt=='&') res&=tmp;
     69               else res|=tmp;
     70               int top=1;
     71               for(int j=i+1;j<len;j++)
     72                 {
     73                   if(s[j]=='(') top++;
     74                   else if(s[j]==')') top--;
     75                   if(!top)
     76                     {
     77                         i=j;
     78                         break;
     79                     }
     80                 }
     81               }
     82           else if(s[i]=='!') sym=false;
     83           else if(s[i]=='&') lastopt='&';
     84           else if(s[i]=='|') lastopt='|';
     85           else if(s[i]=='V')
     86             {
     87               bool tmp=true;
     88               if(!sym) {tmp=false;sym=true;}
     89               if(lastopt=='&') res&=tmp;
     90               else res|=tmp;
     91             }
     92           else if(s[i]=='F')
     93             {
     94               bool tmp=false;
     95               if(!sym) {tmp=true;sym=true;}
     96               if(lastopt=='&') res&=tmp;
     97               else res|=tmp;
     98             }
     99         putchar( res ? 'V' : 'F' );puts("");
    100       }
    101     return 0;
    102 }
  • 相关阅读:
    d3.js(v5.7)的比例尺以及坐标轴
    d3.js(v5.7)的node与数据匹配(自动匹配扩展函数)
    d3.js(v5.7)的attr()函数完善(添加obj支持)
    d3.js入门之DOM操作
    d3.js入门学习
    最近在写个人网站,忙碌中。。。
    构建vue项目(vue 2.x)时的一些配置问题(持续更新)
    Python之元组
    Python之列表
    Python之字符串
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/3986540.html
Copyright © 2011-2022 走看看