• 【递归】【栈】先修课 计算概论(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 }
  • 相关阅读:
    C# winform应用程序运行后,bin文件夹中会自动生成3个文件和一个应用程序
    c# 进程间通信
    C# Winform 右下角弹出框
    C#面向对象设计模式纵横谈(1):面向对象设计模式与原则 笔记
    C#面向对象设计模式纵横谈(2):Singleton 单件(创建型模式) 笔记
    《C# 设计模式》笔记: 第5章 继承
    《C# 设计模式》笔记: 第8章 简单工厂模式
    手动汉化 VS 2005 的代价
    《C# 设计模式》笔记: 第7章 C#中的数组、文件和异常
    收到微软寄给我的 SQL Server 2005 Beta3 光盘
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/3986540.html
Copyright © 2020-2023  润新知