计算带圆括号的布尔表达式(使用堆栈)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算带圆括号的布尔表达式(使用堆栈)相关的知识,希望对你有一定的参考价值。
An [example][id]. Then, **anywhere**else in the doc, define the link:
[id]: http://example.com/ "Title"
// This program reads a reads and evaluates fully parenthesized Boolean // expressions. The purpose is to illustrate a fundamental use of stacks. import java.util.Stack; import java.util.Scanner; import java.util.regex.Pattern; public class KauffmanW7_LargeProgram05_EvalBoolean { { String expression; boolean answer; // Testing Expressions. expression = "(5>1)"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(5>=1)"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(5<1)"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(5<=1)"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(5==1)"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(5!=1)"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(10 > 5)"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(10 >= 5)"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(10 < 5)"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(10 <= 5)"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(10 == 5)"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(10 != 5)"; answer = evaluate(expression); if (answer == true) { } else { } expression = "((10 > 5) && (10 > 15))"; answer = evaluate(expression); if (answer == true) { } else { } expression = "((10 > 5) || (10 > 15))"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(((10 > 5) && (10 > 15)) || (20 > 15))"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(((10 > 5) || (10 > 15)) && (15 >= 20))"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(!(5<1))"; answer = evaluate(expression); if (answer == true) { } else { } expression = "(!(5>1))"; answer = evaluate(expression); if (answer == true) { } else { } answer = query(stdin, " Would you like to try some expressions of your own?"); do { expression = stdin.nextLine( ); try { answer = evaluate(expression); } { } } while (query(stdin, "Another string?")); } String answer; answer = input.nextLine( ).toUpperCase( ); while (!answer.startsWith("Y") && !answer.startsWith("N")) { answer = input.nextLine( ).toUpperCase( ); } return answer.startsWith("Y"); } // Precondition: The string is a fully parenthesized Boolean expression // formed from non-negative numbers, parentheses, comparisons, and the three // logical operations: !(NOT, unary), && (AND, binary), and || (OR, binary). // Postcondition: The string has been evaluated and the value returned. // Exceptions: Can throw an NumberFormatException if the expression contains // characters other than digits, operations, parentheses and whitespace. // Can throw IllegalArgumentException if the input line is an // illegal expression, such as unbalanced parentheses or an unknown symbol. // KEY //**************** // >= G * // <= L * // != N * // || O * // && A * // == E * //**************** s = s.replace(">=","G"); s = s.replace("<=","L"); s = s.replace("!=","N"); s = s.replace("||","O"); s = s.replace("&&","A"); s = s.replace("==","E"); // System.out.println(s); Scanner input = new Scanner(s); Stack<Integer> numbers = new Stack<Integer>( ); Stack<Character> operations = new Stack<Character>( ); Stack<Boolean> booleans = new Stack<Boolean>( ); String next; char first; while (input.hasNext( )) { if (input.hasNext(UNSIGNED_INT)) { next = input.findInLine(UNSIGNED_INT); } else { next = input.findInLine(CHARACTER); first = next.charAt(0); switch (first) { case '>': // Greater Than case 'G': // >= Greater Than or Equal to. case '<': // Less Than case 'L': // Less Than or Equal to. case 'E': // == Equal case 'N': // != Not Equal case '!': // Not case 'A': // && And case 'O': // || Or operations.push(first); break; case ')': // Right parenthesis evaluateStackTops(numbers, operations,booleans); break; case '(': // Left parenthesis break; default : // Illegal character } } } if (booleans.size( ) != 1) return booleans.pop( ); } public static boolean evalBoolean(int oper1, int oper2, char bool) { switch (bool) { case '>': // Greater Than return (oper1 > oper2); case 'G': // >= Greater Than or Equal to. return (oper1 >= oper2); case '<': // Less Than return (oper1 < oper2); case 'L': // Less Than or Equal to. return (oper1 <= oper2); case 'E': // == Equal return (oper1 == oper2); case 'N': // != Not Equal return (oper1 != oper2); default : // This statement should be unreachable since only the aforementioned values get sent to the method... } } public static void evaluateStackTops(Stack<Integer> numbers, Stack<Character> operations,Stack<Boolean> booleans) { int operand1, operand2; boolean bool1,bool2; if (operations.size( ) < 1) char oper = operations.pop(); switch (oper) { case '>': // Greater Than case 'G': // >= Greater Than or Equal to. case '<': // Less Than case 'L': // Less Than or Equal to. case 'E': // == Equal case 'N': // != Not Equal if (numbers.size( ) < 2) operand2 = numbers.pop( ); operand1 = numbers.pop( ); booleans.push(evalBoolean(operand1,operand2,oper)); break; case '!': if (booleans.size( ) < 1) booleans.push (!(booleans.pop( ))); break; case 'A': case 'O': if (booleans.size( ) < 2) bool2 = booleans.pop( ); bool1 = booleans.pop( ); if (oper == 'A') booleans.push ( (bool1 && bool2)); else booleans.push ( (bool1 || bool2)); break; default : } } // These patterns are from Appendix B of Data Structures and Other Objects. // They may be used in hasNext and findInLine to read certain patterns // from a Scanner. public static final Pattern CHARACTER = Pattern.compile("\S.*?"); public static final Pattern UNSIGNED_INT = Pattern.compile("\d+.*?"); }
以上是关于计算带圆括号的布尔表达式(使用堆栈)的主要内容,如果未能解决你的问题,请参考以下文章