123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- import java.util.Scanner;
- import java.util.*;
- import java.lang.Math;
-
- public class Main{
-
- static final List<String> button1 = Arrays.asList("(-)", "sq", "sqrt", "cos", "sin", "tan", "-cos", "-sin", "-tan");
- static final List<String> TRIG = Arrays.asList("cos", "sin", "tan", "-cos", "-sin", "-tan");
-
- public static void main(String[] args){
-
- //Calls class Calculator, Memory, Display
- Calculator calc = new Calculator();
- Memory memo = new Memory();
- Display disp = new Display();
-
- boolean isRadians = true;
-
- //User input
- Scanner reader = new Scanner(System.in);
- Console.println("Welcome to Calculator!\n");
- Console.println("Operations:");
- Console.println("|Clear |(-) |OFF |+ |- |* |/ |");
- Console.println("|Cos |Sin |Tan |-Cos |-Sin |-Tan |Sq |SqRt |");
- Console.println("|! |^ |log |-log |ln |M+ |MC |MRC |");
- Console.println("|round |deg |rad |%% | Display Mode |");
- double value = 0;
-
- while(true){
- // conditional ? ifTrue : ifFalse
- String trigMode = isRadians ? "Rad" : "Deg";
- String valueStr = disp.getDisplayString(isRadians ? Math.toRadians(value) : value);
- Console.println("\nDeg/Rad: [%s]; Mode: [%s] Value: [%s]", trigMode, disp.getDisplayMode(), disp.getDisplayString(value), valueStr);
- Console.println("Pick Operation:");
- String button = reader.nextLine().toLowerCase();
-
- if(button.equals("display mode")){
- Console.println("dec | bin | oct | hex | pct");
- String mode = reader.nextLine().toLowerCase();
- switch(mode){
- case "bin":
- disp.setDisplayMode(Display.DisplayMode.BIN);
- break;
- case "oct":
- disp.setDisplayMode(Display.DisplayMode.OCT);
- break;
- case "hex":
- disp.setDisplayMode(Display.DisplayMode.HEX);
- break;
- case "dec":
- disp.setDisplayMode(Display.DisplayMode.DEC);
- break;
- case "pct":
- disp.setDisplayMode(Display.DisplayMode.PCT);
- default:
- break;
- }
- continue; //starts while loop over
- }
-
- if(TRIG.contains(button)){
-
- // value variable will always be stored in DEGREES... convert.
- double radians = Math.toRadians(value);
-
- //result will be in radians
- double result = value;
-
- switch(button) {
- case "cos":
- result = calc.getCosine(value);
- break;
- case "sin":
- result = calc.getSine(radians);
- break;
- case "tan":
- result = calc.getTan(radians);
- break;
- case "-cos":
- result = calc.inverseCos(radians);
- break;
- case "-sin":
- result = calc.inverseSin(radians);
- break;
- case "-tan":
- result = calc.inverseTan(radians);
- break;
- default:
- result = radians;
- Console.println("Invalid trig operator");
- break;
- }
- // result is in radians, value is always saved in degrees
- value = Math.toDegrees(result);
- continue;
- }
-
- if(button.equalsIgnoreCase("OFF")){
- Console.println("Peace.");
- break;
- }else if(button.equalsIgnoreCase("Clear")){
- value = 0;
- continue;
- }else if(button.equalsIgnoreCase("M+")){
- memo.saveNumber(value);
- Console.println("SAVED");
- continue;
- }else if(button.equalsIgnoreCase("MC")){
- memo.clear();
- Console.println("MEMORY CLEARED");
- continue;
- }
-
- if(button1.contains(button)){
- switch(button){
- case "(-)":
- value = calc.invert(value);
- break;
- case "sq":
- value = calc.square(value);
- break;
- case "sqrt":
- value = calc.squareRoot(value);
- break;
- case "!":
- value = calc.factorial(value);
- break;
- case "log":
- value = calc.log(value);
- break;
- case "-log":
- value = calc.inverseLog(value);
- break;
- case "ln":
- value = calc.natLog(value);
- break;
- case "round":
- value = calc.round(value);
- break;
- }
- }else{
- double y = Console.getDoubleInput("Input Number:");
- switch(button){
- case "+":
- value = calc.add(value, y);
- break;
- case "-":
- value = calc.subtract(value, y);
- break;
- case "*":
- value = calc.multiply(value, y);
- break;
- case "/":
- value = calc.divide(value, y);
- break;
- case "^":
- value = calc.getPow(value, y);
- break;
- default:
- Console.print("Err");
- break;
- }
- }
- if(button.equalsIgnoreCase("MRC")){
- value = memo.recall();
- if(memo.recall() > 0){
- double y = value;
- }else{
- continue;
- }
- }
- continue;
- }
- }
- }
-
-
|