前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Boolean Expressions

Boolean Expressions

作者头像
AI那点小事
发布2020-04-20 16:41:51
8070
发布2020-04-20 16:41:51
举报
文章被收录于专栏:AI那点小事

描述 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


解题思路: 和表达式求值一样,用两个栈完成,一个存值,一个存运算符号。


Java代码如下:

代码语言:javascript
复制
import java.util.Scanner;
import java.util.Stack;

public class Main {

    public static int compare(char a,char b){
        if ( b == '('){
            return -1;
        }
        switch (a) {
        case '(':
            if ( b == ')'){
                return 0;
            }
            return -1;
        case '!':
            if ( b != '!'){
                return 1;
            }
            return -1;
        case '&':
            if ( b == '!'){
                return -1;
            }
            return 1;
        case '|':
            if ( b == '!' || b == '&'){
                return -1;
            }
            return 1;
        case '#':
            if ( b == '#'){
                return 0;
            }
            return -1;
        }
        return 0;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        StringBuffer buffer = new StringBuffer(in.nextLine());
        //表达式最后插入一个‘#’,用来确保表达式能处理完
        buffer.append("#");
        Stack<Character> pra = new Stack<Character>();
        Stack<Character> ope = new Stack<Character>();
        ope.push('#');
        int i = 0;
        int len = buffer.length();
        while(i < len){
            if(buffer.charAt(i) == ' '){
                i++;
                continue;
            }
            if ( buffer.charAt(i) == 'F' || buffer.charAt(i) == 'V'){
                pra.push(buffer.charAt(i));
            }else{
                if (compare(ope.peek(), buffer.charAt(i))<0){
                    ope.push(buffer.charAt(i));
                }else if (compare(ope.peek(), buffer.charAt(i))>0){
                    char a,b;
                    switch (ope.peek()) {
                    case '!':
                        a = ope.peek();
                        pra.pop();
                        if (a == 'V'){
                            a = 'F';
                        }else{
                            a = 'V';
                        }
                        pra.push(a);
                        break;
                    case '&':
                        a = pra.peek();
                        pra.pop();
                        b = pra.peek();
                        pra.pop();
                        if (a == 'F'|| b == 'F'){
                            pra.push('F');
                        }else{
                            pra.push('V');
                        }
                        break;
                    case '|':
                        a = pra.peek();
                        pra.pop();
                        b = pra.peek();
                        pra.pop();
                        if (a == 'V'|| b == 'V'){
                            pra.push('V');
                        }else{
                            pra.push('F');
                        }
                        break;
                    }
                    ope.pop();
                    continue;
                }else{
                    ope.pop();
                }
            }
            i++;
        }
        System.out.print("Expression : "+pra.pop());
        in.close();
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档