import java.math.BigInteger; public class Calculator { public static void runCommandLoop() { Display display = new Display(); Memory memory = new Memory(); memory.resetMemory(); DisplayMode conversion = new DisplayMode(); int modeOrdinal = 0; int trigMode = 0; //0 - degrees (default); 1 - radians String initial = Console.getStringInput("Enter number"); display.setDisplay(initial); while (true) { String command = Console.getStringInput("Choose a command"); if (command.equalsIgnoreCase("off") || command.equalsIgnoreCase("exit")) { Console.println("Thank you for using our product!"); break; } if (command.equalsIgnoreCase("c") || command.equalsIgnoreCase("clear")) { display.setDisplay("0"); Console.println(display.getDisplay()); String resetValue = Console.getStringInput("Enter number"); display.setDisplay(resetValue); } if (command.equalsIgnoreCase("+") || command.equalsIgnoreCase("add")) { double first = Double.parseDouble(display.getDisplay()); double second = Console.getDoubleInput("Enter number"); double out = Core.add(first, second); Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } if (command.equalsIgnoreCase("-") || command.equalsIgnoreCase("subtract")) { double first = Double.parseDouble(display.getDisplay()); double second = Console.getDoubleInput("Enter number"); double out = Core.subtract(first, second); Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } if (command.equalsIgnoreCase("*") || command.equalsIgnoreCase("multiply")) { double first = Double.parseDouble(display.getDisplay()); double second = Console.getDoubleInput("Enter number"); double out = Core.multiply(first, second); Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } if (command.equalsIgnoreCase("/") || command.equalsIgnoreCase("divide")) { double first = Double.parseDouble(display.getDisplay()); double second = Console.getDoubleInput("Enter number"); double out = Core.divide(first, second); if (Double.toString(out) == "NaN" || Double.toString(out) == "Infinity" || Double.toString(out) == "-Infinity") { display.errorDisplay(); } else { Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } } if (command.equalsIgnoreCase("sq")) { double first = Double.parseDouble(display.getDisplay()); double out = Core.multiply(first, first); Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } if (command.equalsIgnoreCase("sqrt")) { double first = Double.parseDouble(display.getDisplay()); double out = Core.squareRoot(first); Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } if (command.equalsIgnoreCase("^") || command.equalsIgnoreCase("pow")) { double first = Double.parseDouble(display.getDisplay()); double second = Console.getDoubleInput("Enter number"); double out = Core.exponentiation(first, second); Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } if (command.equalsIgnoreCase("/x") || command.equalsIgnoreCase("inverse")) { double first = Double.parseDouble(display.getDisplay()); double out = Core.inverse(first); if (Double.toString(out) == "NaN" || Double.toString(out) == "Infinity" || Double.toString(out) == "-Infinity") { display.errorDisplay(); } else { Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } } if (command.equalsIgnoreCase("+-") || command.equalsIgnoreCase("reverse")) { double first = Double.parseDouble(display.getDisplay()); double out = Core.reverse(first); Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } if (command.equalsIgnoreCase("m+")) { memory.addMemory(display.getDisplay()); display.setDisplay(memory.recallMemory()); Console.println(display.getDisplay()); } if (command.equalsIgnoreCase("mrc")) { Console.println(memory.recallMemory()); display.setDisplay(memory.recallMemory()); } if (command.equalsIgnoreCase("mc")) { memory.resetMemory(); Console.println(display.getDisplay()); } if (command.equalsIgnoreCase("bin") || command.equalsIgnoreCase("binary")) { Console.println(conversion.switchDisplayMode(DisplayMode.Mode.Binary, display.getDisplay())); } if (command.equalsIgnoreCase("hex") || command.equalsIgnoreCase("hexadecimal")) { Console.println(conversion.switchDisplayMode(DisplayMode.Mode.Hexadecimal, display.getDisplay())); } if (command.equalsIgnoreCase("oct") || command.equalsIgnoreCase("octal")) { Console.println(conversion.switchDisplayMode(DisplayMode.Mode.Octal, display.getDisplay())); } if (command.equalsIgnoreCase("dec") || command.equalsIgnoreCase("decimal")) { Console.println(conversion.switchDisplayMode(DisplayMode.Mode.Decimal, display.getDisplay())); } if (command.equalsIgnoreCase("switch")) { Console.println(conversion.switchDisplayMode(DisplayMode.Mode.values()[modeOrdinal], display.getDisplay())); if (modeOrdinal < 3) { modeOrdinal++; } else { modeOrdinal = 0; } } if (command.equalsIgnoreCase("round") || command.equalsIgnoreCase("R")) { double out = Custom.round(Double.parseDouble(display.getDisplay())); Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } if (command.equalsIgnoreCase("abs")) { double out = Custom.abs(Double.parseDouble(display.getDisplay())); Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } if (command.equalsIgnoreCase("deg")) { trigMode = 0; Console.println("Trigonometric units are degrees"); } if (command.equalsIgnoreCase("rad")) { trigMode = 1; Console.println("Trigonometric units are radians"); } if (command.equalsIgnoreCase("trigSwitch")) { if (trigMode == 0) { trigMode = 1; Console.println("Trigonometric units are radians"); } else { trigMode = 0; Console.println("Trigonometric units are degrees"); } } if (command.equalsIgnoreCase("sin")) { double first = Double.parseDouble(display.getDisplay()); double out = Scientific.sine(first, trigMode); if (Double.toString(out) == "NaN") { display.errorDisplay(); } else { Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } } if (command.equalsIgnoreCase("cos")) { double first = Double.parseDouble(display.getDisplay()); double out = Scientific.cosine(first, trigMode); if (Double.toString(out) == "NaN") { display.errorDisplay(); } else { Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } } if (command.equalsIgnoreCase("tan")) { double first = Double.parseDouble(display.getDisplay()); double out = Scientific.tangent(first, trigMode); if (Double.toString(out) == "NaN") { display.errorDisplay(); } else { Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } } if (command.equalsIgnoreCase("asin")) { double first = Double.parseDouble(display.getDisplay()); double out = Scientific.inverseSin(first, trigMode); if (Double.toString(out) == "NaN") { display.errorDisplay(); } else { Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } } if (command.equalsIgnoreCase("acos")) { double first = Double.parseDouble(display.getDisplay()); double out = Scientific.inverseCosine(first, trigMode); if (Double.toString(out) == "NaN") { display.errorDisplay(); } else { Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } } if (command.equalsIgnoreCase("atan")) { double first = Double.parseDouble(display.getDisplay()); double out = Scientific.inverseTangent(first, trigMode); if (Double.toString(out) == "NaN") { display.errorDisplay(); } else { Console.println(Double.toString(out)); display.setDisplay(Double.toString(out)); } } if (command.equalsIgnoreCase("fact") || command.equalsIgnoreCase("x!")) { double first = Custom.round(Double.parseDouble(display.getDisplay())); BigInteger out = Scientific.factorial(first); Console.println(out.toString()); display.setDisplay(out.toString()); } } } }