открытый класс Project_t {
private static final String fpath = "../COSC241_P4_Input.txt";
private static MyExpressionTree exTree = new MyExpressionTree();
private static List<String> infList;
private static final MyDeque d = new MyDeque();
private static final MyStack st = new MyStack();
private static int result;
/**
*
* Test method for the project. Handles file input and output, as well as *
* running tests on each expression in the input file. *
*/
public static void test() {
result = 0;
File out = new File("../COSC241_P4_Output_tdreed0.txt");
try {
//Stores each line of the file in a list.
infList = FP.readFile(fpath).stream().filter((s) -> !s.isEmpty()).collect(Collectors.toList());
//If the output file does not currently exist, create it.
if (!out.exists()) {
out.createNewFile();
}
try (PrintWriter writer = new PrintWriter(out)) {
for (String s : infList) {
String root = s.trim().replaceAll(" ", "").replaceAll("\\[", "(").replaceAll("\\]", ")");
writer.println("Original Infix:\t" + root);
if (!ExpressionRegulators.allPMatched(root) || !ExpressionRegulators.allValid(root) || !ExpressionRegulators.isValidOrder(root)) {
writer.println("Invalid Expression");
writer.println();
} else {
buildExpressionTree(root);
//Performs traversals on the tree
exTree.preorderTraversal();
writer.println("Preorder: " + exTree.preorderStr);
exTree.inorderTraversal();
writer.println("Inorder: " + exTree.inorderStr);
exTree.postorderTraversal();
writer.println("Postorder: " + exTree.postorderStr);
result = exTree.evaluate();
writer.println("Result: " + result);
writer.println();
exTree.clear();
}
}
}
} catch (IOException e) {
System.out.println(e.toString());
System.exit(0);
}
}