123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- 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());
- }
- }
- }
- }
|