Boolean Expressions

2020-04-20 16:41:51 浏览数 (1)

描述 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();
    }

}

0 人点赞