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,这个例子中因为三个函数之间要相互利用,所以得在前面先声明定义。啊,不管怎么样,一定要坚持下去,坚持一定会有结果。

  • 相关阅读:
    将SL4 程序移植到WP7(附Teched2010“.NET研究”真机图) 狼人:
    WP7有约(二“.NET研究”):课后作业 狼人:
    Android平台SQLite快速入门“.NET研究”实践 狼人:
    移动Web界面构建最佳“.NET研究”实践 狼人:
    Androi“.NET研究”d如何在三年时间里征服移动世界的 狼人:
    详解如何让Android UI“.NET研究”设计性能更高效 狼人:
    Android 2.3预计下周发布 十大惊“.NET研究”喜不容错过 狼人:
    Android的移动存储解决方案“.NET研究”之SharedPreferences 狼人:
    C++开发者快速学习ObjectiveC语言核“.NET研究”心语法 狼人:
    图像像素OpenCV4Android一点关于图像的基础
  • 原文地址:https://www.cnblogs.com/rimochiko/p/7486941.html
Copyright © 2011-2022 走看看