Sfoglia il codice sorgente

Input accepts numbers or operators

NedRedmond 6 anni fa
parent
commit
923456e5b7
5 ha cambiato i file con 71 aggiunte e 45 eliminazioni
  1. 26
    11
      Console.java
  2. 3
    2
      MainApplication.java
  3. 12
    7
      Trigonometry.java
  4. 11
    6
      opSwitch.java
  5. 19
    19
      simpleOp.java

+ 26
- 11
Console.java Vedi File

@@ -4,6 +4,9 @@ import java.lang.*;
4 4
  * Created by leon on 2/9/18.
5 5
  */
6 6
 public class Console {
7
+    public static double currentNumber = 0;
8
+    public static String regexNum = "\\d+";
9
+    
7 10
     //GIVEN TO US 
8 11
     public static void print(String output, Object... args) {
9 12
         System.out.printf(output, args);
@@ -64,30 +67,42 @@ public class Console {
64 67
         }
65 68
     }
66 69
     */
70
+   public static void getInput() {
71
+       System.out.println(currentNumber); 
72
+       String input = getStringInput("Please enter an operation or number. Enter '?' for a list of commands.");
73
+       quit(input);
74
+       if (input.matches(regexNum) || input.equals("pi") || input.equals("e")) {
75
+           getNumber(input);
76
+        } else {
77
+            getOp(input);
78
+        }
79
+    }
80
+   
67 81
     public static double getNumber(String numberString){
68 82
             if(numberString.equals("quit") || numberString.equals("reset") || numberString.equals("clear")){
69 83
                 quit(numberString);
70
-                simpleOp.currentNumber = 0;
84
+                currentNumber = 0;
71 85
             }else if(numberString.equals("pi")){
72
-                simpleOp.currentNumber = Math.PI;
86
+                currentNumber = Math.PI;
73 87
             }else if(numberString.equals("e")){
74
-                simpleOp.currentNumber = Math.E;
88
+                currentNumber = Math.E;
75 89
             }else{
76
-                simpleOp.currentNumber = Double.parseDouble(numberString);
90
+                currentNumber = Double.parseDouble(numberString);
77 91
             }
78
-            return simpleOp.currentNumber;
92
+            return currentNumber;
79 93
     }
80 94
     
81
-    public static String getOp(){
95
+    public static String getOp(String operation){
82 96
         //GET OPERATION FROM PERSON
83
-            System.out.println("Current Number: " + simpleOp.currentNumber);
84
-            String operation = getStringInput("Please choose your operation.");
97
+            //System.out.println(simpleOp.currentNumber);
98
+            //String operation = getStringInput("Please choose your operation.");
85 99
             if (operation == null) {
86 100
                 System.out.println("You have not provided valid input. Please enter \"?\" for a list of commands.");
87
-            }
88
-            quit(operation);
101
+            } else {
102
+            //quit(operation);
89 103
             opSwitch.choose(operation);
90 104
             //return opSwitch.choose(operation1);
105
+        }
91 106
             return operation;
92 107
     }
93 108
     
@@ -97,7 +112,7 @@ public class Console {
97 112
         } else if(answer.equals("clear")) {
98 113
             System.out.print('\f');
99 114
         } else if(answer.equals("reset")){
100
-            simpleOp.currentNumber = 0;
115
+            currentNumber = 0;
101 116
             //getNumber(Console.getStringInput("Enter Number."));
102 117
         }
103 118
     }

+ 3
- 2
MainApplication.java Vedi File

@@ -6,13 +6,14 @@ public class MainApplication {
6 6
     public static void main(String[] args) {
7 7
         //SWITCH DISPLAY MODE 
8 8
         //display.SwitchDisplayMode();
9
-        System.out.println("Current Number: " + simpleOp.currentNumber);
9
+        //System.out.println("Current Number: " + Console.currentNumber);
10 10
         //simpleOp.currentNumber = Console.getNumber(Console.getStringInput("Enter First Number."));
11 11
         while(true){
12 12
             // if(simpleOp.currentNumber == 0){
13 13
                 // simpleOp.currentNumber = Console.getNumber(Console.getStringInput("Enter First Number."));
14 14
             // }else{
15
-                Console.getOp();
15
+                //System.out.println(Console.currentNumber);
16
+                Console.getInput();
16 17
             //}
17 18
         }
18 19
     }

+ 12
- 7
Trigonometry.java Vedi File

@@ -15,27 +15,32 @@ public class Trigonometry
15 15
     }
16 16
     //sin
17 17
     public static double sin(double x){
18
-        double sin = Math.sin(x);
19
-        return sin;
18
+        Console.currentNumber = Math.sin(x);
19
+        return Console.currentNumber;
20 20
     }
21 21
     //cos
22 22
     public static double cos(double x){
23
-        return null;
23
+        Console.currentNumber = Math.cos(x);
24
+        return Console.currentNumber;
24 25
     }
25 26
     //tan
26 27
     public static double tan(double x){
27
-        return null;
28
+        Console.currentNumber = Math.tan(x);
29
+        return Console.currentNumber;
28 30
     }
29 31
     //inverse of sin
30 32
     public static double sinInverse(double x){
31
-        return null;
33
+        Console.currentNumber = Math.asin(x);
34
+        return Console.currentNumber;
32 35
     }
33 36
     //inverse of cos
34 37
     public static double cosInverse(double x){
35
-        return null;
38
+        Console.currentNumber = Math.acos(x);
39
+        return Console.currentNumber;
36 40
     }
37 41
     //inver of tan
38 42
     public static double tanInverse(double x){
39
-        return null;
43
+        Console.currentNumber = Math.atan(x);
44
+        return Console.currentNumber;
40 45
     }
41 46
 }

+ 11
- 6
opSwitch.java Vedi File

@@ -8,14 +8,14 @@
8 8
 public class opSwitch
9 9
 {
10 10
     public static void choose(String opInput){
11
-        simpleOp op = new simpleOp();
12
-        
11
+        Console op = new Console();
12
+
13 13
         String x;
14 14
         String y;
15 15
         // switchboard for operations
16 16
         switch (opInput) {
17 17
             case "?":
18
-                System.out.println("Commands include:\n"
18
+            System.out.println("Commands include:\n"
19 19
                 +"+"+"\n"
20 20
                 +"-"+"\n"
21 21
                 +"*"+"\n"
@@ -38,7 +38,7 @@ public class opSwitch
38 38
                 +"inverse of sin WIP"+"\n"
39 39
                 +"inver of cos WIP"+"\n"
40 40
                 +"inverse of tan WIP"+"\n");
41
-                break;
41
+            break;
42 42
             case "+": 
43 43
                 //x = Console.getStringInput("Please enter first number.");
44 44
                 y = Console.getStringInput("Please enter number.");
@@ -91,19 +91,24 @@ public class opSwitch
91 91
             case "log (change of base)":
92 92
                 break;
93 93
             case "sin":
94
+                Trigonometry.sin(op.currentNumber);
94 95
                 break;
95 96
             case "cos":
97
+            Trigonometry.cos(op.currentNumber);
96 98
                 break;
97 99
             case "tan":
100
+            Trigonometry.tan(op.currentNumber);
98 101
                 break;
99 102
             case "inverse of sin":
103
+            Trigonometry.sinInverse(op.currentNumber);
100 104
                 break;
101 105
             case "inverse of cos":
106
+            Trigonometry.cosInverse(op.currentNumber);
102 107
                 break;
103 108
             case "inverse of tan":
109
+            Trigonometry.tanInverse(op.currentNumber);
104 110
                 break;
105 111
         }
106 112
     }
107
-    
108
-    
113
+
109 114
 }

+ 19
- 19
simpleOp.java Vedi File

@@ -8,7 +8,7 @@
8 8
 import java.util.*;
9 9
 public class simpleOp
10 10
 {
11
-    public static double currentNumber = 0;
11
+    
12 12
     
13 13
     // public static double NumberOne(){
14 14
         // String getFirst = Console.getStringInput("Please enter number.");
@@ -18,69 +18,69 @@ public class simpleOp
18 18
     
19 19
     public static double add(double x, double y){
20 20
         //CALULATES THE SUM OF TWO NUMBERS
21
-        currentNumber = x + y;
21
+        Console.currentNumber = x + y;
22 22
         //System.out.println(currentNumber);
23
-        return currentNumber;
23
+        return Console.currentNumber;
24 24
     }
25 25
     
26 26
     public static double sub(double x, double y){
27 27
         //CALULATES THE DIFFERENCE OF TWO NUMBERS
28
-        currentNumber = x - y;
28
+        Console.currentNumber = x - y;
29 29
         //System.out.println(currentNumber);
30
-        return currentNumber;
30
+        return Console.currentNumber;
31 31
     }
32 32
     
33 33
     public static double mul(double x, double y){
34 34
         //CALULATES THE PRODUCT OF TWO NUMBERS
35
-        currentNumber = x * y;
35
+        Console.currentNumber = x * y;
36 36
         //System.out.println(currentNumber);
37
-        return currentNumber;
37
+        return Console.currentNumber;
38 38
     }
39 39
     
40 40
     public static double div(double x, double y){
41 41
         //CALULATES THE QUOTIENT OF TWO NUMBERS
42
-        currentNumber = x / y;
42
+        Console.currentNumber = x / y;
43 43
         //System.out.println(currentNumber);
44
-        return currentNumber;
44
+        return Console.currentNumber;
45 45
     }
46 46
     
47 47
     public static double squ(double x){
48 48
         //CALULATES THE SQUARE OF ONE NUMBER
49 49
         //CASTED TO double FOR NOW... IT'S IN ALPHA, YOU DIG?
50
-        currentNumber = Math.pow(x,2);
50
+        Console.currentNumber = Math.pow(x,2);
51 51
         //System.out.println(currentNumber);
52
-        return currentNumber;
52
+        return Console.currentNumber;
53 53
     }
54 54
     
55 55
     public static double sqrt(double x){
56 56
         //CALULATES THE SQUARE ROOT OF ONE NUMBER
57 57
         //CASTED TO double FOR NOW... IT'S IN ALPHA, YOU DIG?
58
-        currentNumber = Math.sqrt(x);
58
+        Console.currentNumber = Math.sqrt(x);
59 59
         //System.out.println(currentNumber);
60
-        return currentNumber;
60
+        return Console.currentNumber;
61 61
     }
62 62
     
63 63
     public static double exp(double x, double y){
64 64
         //CALULATES EXPONENTIATION OF X TO POWER OF Y
65 65
         //CASTED TO double FOR NOW... IT'S IN ALPHA, YOU DIG?
66
-        currentNumber = Math.pow(x,y);
66
+        Console.currentNumber = Math.pow(x,y);
67 67
         //System.out.println(currentNumber);
68
-        return currentNumber;
68
+        return Console.currentNumber;
69 69
     }
70 70
     
71 71
     public static double inv(double x){
72 72
         //CALULATES THE INVERSE OF NUMBER
73 73
         //CASTED TO double FOR NOW... IT'S IN ALPHA, YOU DIG?
74
-        currentNumber = (1/x);
74
+        Console.currentNumber = (1/x);
75 75
         //System.out.println(currentNumber);
76
-        return currentNumber;
76
+        return Console.currentNumber;
77 77
     }
78 78
     
79 79
     public static double invert(double x){
80 80
         //TURNS NEGATIVE TO POSITIVE OR POSITIVE TO NEGATIVE
81
-        currentNumber = -x;
81
+        Console.currentNumber = -x;
82 82
         //System.out.println(currentNumber);
83
-        return currentNumber;
83
+        return Console.currentNumber;
84 84
     }
85 85
     
86 86
 }