import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java class having as its purpose to test the following classes: ** Operator, ExprTree, ExprTreeAtomic, ExprTreeCompound, ExprTreeBuilder. ** ** Author: R. McCloskey ** Date: Feb. 2022 */ public class ExprTreeTester { private static Scanner input; // either keyboard or file named by args[0] private static boolean echo; // should input be echoed? private static ExprTreeBuilder exprBuilder; public static void main(String[] args) { input = establishInputSource(args.length == 0 ? null : args[0]); exprBuilder = new ExprTreeBuilder(); System.out.println("Welcome to the Expression Tree Tester Program"); boolean keepGoing = true; while (keepGoing) { try { keepGoing = testOneExprString(); } catch (Exception e) { e.printStackTrace(System.out); } } System.out.println("\nGoodbye from the Expression Tree Tester Program"); } /* Prompts the user to enter an expression-string, transforms that string ** into an expression-tree (i.e., an instance of ExprTree) using an ** instance of ExprTreeBuilder, then uses the methods of that ** expression-tree to compute the value of the expression and its infix, ** prefix, and postfix expression-string forms. It prints all these things. ** Returns true if the user entered an expression, but false if ** they chose the Quit option. */ public static boolean testOneExprString() { System.out.println("\n(0) Quit"); System.out.println("(1) Infix Expression"); System.out.println("(2) Prefix Expression"); System.out.println("(3) Postfix Expression"); System.out.print("> "); String response = input.nextLine().trim(); if (echo) { System.out.println(response); } int k = Integer.parseInt(response); boolean quit = false; if (k == 0) { quit = true; } else if (k < 0 || k > 3) { System.out.println("Invalid response!"); } else { ExprTree exprTree = null; if (k == 1) { // infix System.out.print("Enter infix expression: "); String exprStr = input.nextLine(); exprTree = exprBuilder.fromInfix(exprStr); } else if (k == 2) { // prefix System.out.print("Enter prefix expression: "); String exprStr = input.nextLine(); exprTree = exprBuilder.fromPrefix(exprStr); } else if (k == 3) { // postfix System.out.print("Enter postfix expression: "); String exprStr = input.nextLine(); exprTree = exprBuilder.fromPostfix(exprStr); } else { // impossible!! } // Having built the expression-tree, now evaluate the expression // it represents and display that expression in all three forms. System.out.println("Value is " + exprTree.valueOf()); System.out.println("Prefix form: " + exprTree.toString(ExprTree.PrefixMode)); System.out.println("Infix form: " + exprTree.toString(ExprTree.InfixMode)); System.out.println("Postfix form: " + exprTree.toString(ExprTree.PostfixMode)); } return !quit; } /* If the given parameter is null, returns a Scanner attached to the ** keyboard; otherwise returns a Scanner attached to the file named ** by the parameter. */ private static Scanner establishInputSource(String fileName) { Scanner result = null; if (fileName == null) { result = new Scanner(System.in); // input comes from keyboard echo = false; } else { try { result = new Scanner(new File(fileName)); echo = true; } catch (FileNotFoundException e) { System.out.println("There is no file named " + fileName); System.exit(1); } } return result; } }