import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* BI_Calculator.java ** Author: R. McCloskey ** Date: March 2019 ** ** Java application having as its purpose to test the BigInt class. ** It serves as, in effect, a calculator that interprets each line of ** input as an expression to be evaluated, where the expression is ** expected to have the form ** ** ** ** An is a sequence of (decimal) digits, optionally preceded ** by either '+' or '-' and an is any of "+", "-", "*", "/", "%", ** "=", "<", or ">". ** Examples: "-4509345 * 23456", "98345643345543333 < +4" ** ** In response to each expression provided as input, the program displays ** the result of applying the given operator to the given numbers, making ** use of the methods of the BigInt class. ** ** If a command line argument is provided, it is taken to be the name of ** the file containing input data. Otherwise, input is read from standard ** input. */ public class BI_Calculator { public static void main(String[] args) throws FileNotFoundException { Scanner scanner; boolean echoInput; if (args.length != 0) { scanner = new Scanner(new File(args[0])); echoInput = true; } else { scanner = new Scanner(System.in); echoInput = false; } System.out.println("Welcome to the BigInt Calculator!\n"); System.out.print("> "); while (scanner.hasNextLine()) { String expression = scanner.nextLine(); if (echoInput) { System.out.println(expression); } evaluateExpression(expression); System.out.print("\n> "); } System.out.println("\nGoodbye."); } /* Evaluates the given expression and displays the result. */ public static void evaluateExpression(String expression) { Scanner exprScanner = new Scanner(expression); String numeral1 = exprScanner.next(); String operator = exprScanner.next(); String numeral2 = exprScanner.next(); BigInt left = new BigInt(numeral1); BigInt right = new BigInt(numeral2); System.out.printf("Computing %s %s %s\n", left, operator, right); BigInt numberResult = null; Boolean boolResult = null; String arithOperators = "+-*/%"; String relationalOperators = "=<>"; if (arithOperators.contains(operator)) { if (operator.equals("+")) { numberResult = left.plus(right); } else if (operator.equals("-")) { numberResult = left.minus(right); } else if (operator.equals("*")) { numberResult = left.times(right); } else if (operator.equals("/")) { numberResult = left.dividedBy(right); } else if (operator.equals("%")) { numberResult = left.mod(right); } else { System.out.println("Unrecognized arithmetic operator"); } } else if (relationalOperators.contains(operator)) { if (operator.equals("=")) { boolResult = left.equals(right); } else if (operator.equals("<")) { boolResult = left.isLessThan(right); } else if (operator.equals(">")) { boolResult = left.isGreaterThan(right); } else { System.out.println("Unrecognized relational operator"); } } else { System.out.println("Unrecognized operator"); } if (numberResult != null) { System.out.println(numberResult); } else if (boolResult != null) { System.out.println(boolResult); } else { System.out.println("No result was computed."); } } }