zoukankan      html  css  js  c++  java
  • (递归)2106:Boolean Expressions

    描述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. 
    输入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. 
    输出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. 
    样例输入

    ( V | V ) & F & ( F| V)
    !V | V & V & !F & (F | V ) & (!F | F | !V & V)
    (F&F|V|!V&!F&!(F|F&V))

    样例输出

    Expression 1: F
    Expression 2: V
    Expression 3: V

    我の思考

    用递归的思维来实现这个题目,首先要去除包含的空格,把各项拆成子表达式,是否以!开头的算一个,还有表达式(即一个整体的)~还有就是单项,比如V和F。

     

    我の代码

    #include <iostream>  
    #include <string.h>  
    using namespace std;  
    string a="";  
    bool result;  
    int len=0;  
    int j=0;  
    bool term();  
    bool expression();  
    bool sub();  
      
    bool term(){  
        bool result1;  
        if(a[j]=='!'){  
            j++;  
            result1 = !sub();  
        }else{  
            result1 = sub();  
        }  
      
        return result1;  
    }  
      
    bool expression(){  
        bool result1 = term();  
        while(j<len-1){  
            if(a[j]=='&'){  
                j++;  
                bool value = term();  
                result1 = result1 &&value;  
            }  
      
            if(a[j]=='|'){  
                j++;  
                bool value = term();  
                result1 = result1 ||value;  
            }  
      
            if(a[j]==')'){  
                //cout<<"b"<<endl;  
                j++;  
                break;  
            }  
      
      
        }  
        return result1;  
    }  
      
    bool sub(){  
        bool result2;  
        if(a[j]=='('){  
            j++;  
            result2 = expression();  
        }  
      
        if(a[j]=='V'){  
            j++;  
            result2 = true;  
        }  
      
        if(a[j]=='F'){  
            j++;  
            result2 = false;  
        }  
      
        if(a[j]=='!'){  
            result2 = term();  
        }  
        return result2;  
    }  
      
    int main()  
    {  
        char b[100000];  
        int m=1;  
        while(cin.getline(b,100000)){  
            for(int i=0;i<strlen(b);i++){  
                if(b[i]!=' '){  
                a+=b[i];  
                }  
            }  
            len=a.length();  
            cout<<"Expression "<<m++<<":"<<(expression()?" V":" F")<<endl;  
            j=0;  
            a="";  
            memset(b,0,100000);  
        }  
        return 0;  
    }  

    我の小结

    还是觉得自己很愚笨QAQ,这个例子中因为三个函数之间要相互利用,所以得在前面先声明定义。啊,不管怎么样,一定要坚持下去,坚持一定会有结果。

  • 相关阅读:
    HTML标签
    Web标准
    Web服务器 (获取域名)
    网站favicon.ico 图标
    模块化
    外边距
    h5css产品模块设计
    用策略模式(自定义注解+包扫描)解决if else 太多的问题
    方法区 永久代 元空间 常量池
    javac jar java
  • 原文地址:https://www.cnblogs.com/rimochiko/p/7486941.html
Copyright © 2011-2022 走看看