ソースを参照

merged pemdas

Jonathan Hinds 6 年 前
コミット
6615885989
共有4 個のファイルを変更した80 個の追加47 個の削除を含む
  1. 17
    2
      Calculations.java
  2. 16
    5
      MainApplication.java
  3. 19
    19
      Pemdas.java
  4. 28
    21
      package.bluej

+ 17
- 2
Calculations.java ファイルの表示

@@ -5,6 +5,7 @@ import java.util.regex.Pattern;
5 5
 public class Calculations
6 6
 {
7 7
     public static Console console = new Console();
8
+    public static MainApplication calculator;
8 9
     public static ArrayList<String> library = new ArrayList<String>();
9 10
     public static String invalidArgumentsAmountError = "You have entered an invalid amount of arguments. Please only use two arguments, or switch to PEMDAS";
10 11
     public static Pemdas pemdas = new Pemdas();
@@ -27,10 +28,13 @@ public class Calculations
27 28
     library.add("resetM");
28 29
     library.add("recallM");
29 30
     library.add("displayM");
31
+    library.add("PEMDAS");
32
+    library.add("BASIC");
33
+    
30 34
         
31 35
     }
32 36
 
33
-    public static String getCommand(String userInput)
37
+    public String getCommand(String userInput)
34 38
     {
35 39
         boolean foundCommand = false;
36 40
         String command = "";
@@ -75,8 +79,19 @@ public class Calculations
75 79
             break;   
76 80
             
77 81
             case "clear":
78
-                 Clear();
82
+                Clear();
83
+            break;
84
+            
85
+            case "PEMDAS":   
86
+                System.out.println("You have just entered PEMDAS mode.");
87
+                this.calculator.state = "PEMDAS";
79 88
             break;
89
+            
90
+            case "BASIC":
91
+                System.out.println("You have just entered basic mode");
92
+                this.calculator.state = "basic";
93
+            break;
94
+            
80 95
         }
81 96
         return result;
82 97
     }

+ 16
- 5
MainApplication.java ファイルの表示

@@ -2,11 +2,12 @@ import java.util.Scanner;
2 2
 
3 3
 public class MainApplication {
4 4
     boolean on = false;
5
-    Calculations calculator = new Calculations();
5
+    public static String state = "basic";
6
+    public static Calculations calculator = new Calculations();
6 7
     Console console = calculator.console;
7 8
     
8
-    public static void main(String[] args) {
9
-        
9
+    public void main(String[] args) {
10
+        calculator.calculator = this;
10 11
         //Console.println("Welcome to my calculator!");
11 12
         //String s = Console.getStringInput("Enter a string");
12 13
         //Integer i = Console.getIntegerInput("Enter an integer");
@@ -23,8 +24,18 @@ public class MainApplication {
23 24
         while(this.on == true)
24 25
         {
25 26
             String userInput = scanner.nextLine();
26
-            result = calculator.getCommand(userInput);
27
-            System.out.println(result);
27
+            
28
+            switch(state){
29
+                case "basic":
30
+                    result = calculator.getCommand(userInput);
31
+                    System.out.println(result);
32
+                break;
33
+                
34
+                case "PEMDAS":
35
+                    result = calculator.pemdas.calculate(userInput);
36
+                    System.out.println(result);
37
+                break;    
38
+            }
28 39
         }
29 40
     }
30 41
     

+ 19
- 19
Pemdas.java ファイルの表示

@@ -14,7 +14,7 @@ public class Pemdas
14 14
     public static String calculate(String s)
15 15
     {
16 16
         
17
-        System.out.println("Original " + s);
17
+        //System.out.println("Original " + s);
18 18
         //String p = null;
19 19
         //if(getBetweenBraces(s) != null)
20 20
         //{
@@ -23,7 +23,7 @@ public class Pemdas
23 23
         //System.out.println(p);
24 24
 
25 25
         String e = null;
26
-        System.out.println("expo");
26
+        //System.out.println("expo");
27 27
         //create a duplicate of the method below, this one should have no print statemnts - testing purposes only.
28 28
         //instead of the above, remove everyting except for assignment and initiailization
29 29
         //this will work because getUpdateString2 cannot return null.
@@ -34,34 +34,34 @@ public class Pemdas
34 34
         }
35 35
         
36 36
         String m = null;
37
-        System.out.println("mult");
37
+        //System.out.println("mult");
38 38
         if(getUpdateString2(e,"*") != null)
39 39
         {
40 40
             m = getUpdateString2(e,"*");
41 41
         }
42 42
         
43 43
         String d = null;
44
-        System.out.println("div");
44
+        //System.out.println("div");
45 45
         if(getUpdateString2(m,"/") != null)
46 46
         {
47 47
             d = getUpdateString2(m,"/");
48 48
         }
49 49
          
50 50
         String a = null;
51
-        System.out.println("add");
51
+        //System.out.println("add");
52 52
         if(getUpdateString2(d,"+") != null)
53 53
         {
54 54
             a = getUpdateString2(d,"+");
55 55
         }
56 56
             
57 57
         String ss = null;
58
-        System.out.println("sub");
58
+        //System.out.println("sub");
59 59
        if(getUpdateString2(a,"-") != null)
60 60
        {
61 61
            ss = getUpdateString2(a,"-");
62 62
        }
63 63
        
64
-       System.out.println("calculate 1.) Answer: " + ss);
64
+       //System.out.println("calculate 1.) Answer: " + ss);
65 65
        return ss;
66 66
  
67 67
     }
@@ -98,7 +98,7 @@ public class Pemdas
98 98
 
99 99
         if(matcher.find())
100 100
         { 
101
-            System.out.println("searchReturnFirstString 1.) perform on " + s + ": " + matcher.group());
101
+            //System.out.println("searchReturnFirstString 1.) perform on " + s + ": " + matcher.group());
102 102
             return matcher.group();
103 103
         }
104 104
         
@@ -115,8 +115,8 @@ public class Pemdas
115 115
         //if anything is replaced, return an updated string with the appropriate replacements
116 116
         } else {
117 117
             String result = target.replaceFirst(pattern, replacement);
118
-            System.out.println("searchReplaceFirst 1.) target | pattern | replacement | result");
119
-            System.out.println("searchReplaceFirst 2.) " + target + " | " + pattern + " | " + replacement + " | " + result);
118
+            //System.out.println("searchReplaceFirst 1.) target | pattern | replacement | result");
119
+            //System.out.println("searchReplaceFirst 2.) " + target + " | " + pattern + " | " + replacement + " | " + result);
120 120
             return result; 
121 121
         }
122 122
     }
@@ -150,12 +150,12 @@ public class Pemdas
150 150
     public static String getUpdateString2(String s, String operator)
151 151
     {
152 152
         String f = s;
153
-        System.out.println("getUpdateString2 1.) Input is: " + f);
153
+        //System.out.println("getUpdateString2 1.) Input is: " + f);
154 154
         String pattern = "\\s?\\.?\\d+\\.?(\\d+)?\\s?\\" + operator + "\\s?\\.?\\d+\\.?(\\d+)?\\s?";
155
-        System.out.println("getUpdateString2 2.) Pattern is " + pattern);
155
+        //System.out.println("getUpdateString2 2.) Pattern is " + pattern);
156 156
 
157 157
         String occ = searchReturnFirstString(s, pattern);
158
-        System.out.println("getUpdateString2 3.) the occurance is " + occ);
158
+        //System.out.println("getUpdateString2 3.) the occurance is " + occ);
159 159
         //if we recieve a result
160 160
         if(occ != null)
161 161
         {   
@@ -176,27 +176,27 @@ public class Pemdas
176 176
             {
177 177
                    case "^":
178 178
                    doubleResult = Math.pow(left, right);
179
-                   System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
179
+                   //System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
180 180
                    break;
181 181
                    
182 182
                    case "+":
183 183
                    doubleResult = left + right;
184
-                   System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
184
+                   //System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
185 185
                    break;
186 186
                    
187 187
                    case "-":
188 188
                    doubleResult = left - right;
189
-                   System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
189
+                   //System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
190 190
                    break;
191 191
                    
192 192
                    case "*":
193 193
                    doubleResult = left * right;
194
-                   System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
194
+                   //System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
195 195
                    break;
196 196
                    
197 197
                    case "/":
198 198
                    doubleResult = left / right;
199
-                   System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
199
+                   //System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
200 200
                    break;
201 201
                    
202 202
                    default:
@@ -208,7 +208,7 @@ public class Pemdas
208 208
             //String result = String.format("%.9f", doubleResult);
209 209
             //replace the occurence of occ in s with result
210 210
             f = searchReplaceFirst(s, result, pattern);
211
-            System.out.println("getUpdateString2 5.) iteration completed, f: " + f + "\n");
211
+            //System.out.println("getUpdateString2 5.) iteration completed, f: " + f + "\n");
212 212
             if(!f.equals(s))
213 213
             {
214 214
                 f = getUpdateString2(f, operator);

+ 28
- 21
package.bluej ファイルの表示

@@ -8,22 +8,22 @@ dependency2.type=UsesDependency
8 8
 dependency3.from=Calculations
9 9
 dependency3.to=Console
10 10
 dependency3.type=UsesDependency
11
-editor.fx.0.height=1080
12
-editor.fx.0.width=1920
13
-editor.fx.0.x=-345
14
-editor.fx.0.y=-1080
15
-objectbench.height=198
16
-objectbench.width=595
11
+editor.fx.0.height=722
12
+editor.fx.0.width=1280
13
+editor.fx.0.x=0
14
+editor.fx.0.y=23
15
+objectbench.height=322
16
+objectbench.width=1256
17 17
 package.divider.horizontal=0.6
18
-package.divider.vertical=0.685099846390169
19
-package.editor.height=439
20
-package.editor.width=493
21
-package.editor.x=35
22
-package.editor.y=23
23
-package.frame.height=709
24
-package.frame.width=619
18
+package.divider.vertical=0.569371727748691
19
+package.editor.height=428
20
+package.editor.width=1154
21
+package.editor.x=0
22
+package.editor.y=0
23
+package.frame.height=800
24
+package.frame.width=1280
25 25
 package.numDependencies=3
26
-package.numTargets=3
26
+package.numTargets=4
27 27
 package.showExtends=true
28 28
 package.showUses=true
29 29
 project.charset=UTF-8
@@ -37,19 +37,26 @@ target1.name=Calculations
37 37
 target1.showInterface=false
38 38
 target1.type=ClassTarget
39 39
 target1.width=100
40
-target1.x=240
41
-target1.y=140
40
+target1.x=310
41
+target1.y=260
42 42
 target2.height=50
43 43
 target2.name=Console
44 44
 target2.showInterface=false
45 45
 target2.type=ClassTarget
46 46
 target2.width=80
47
-target2.x=30
48
-target2.y=200
47
+target2.x=190
48
+target2.y=160
49 49
 target3.height=50
50
-target3.name=MainApplication
50
+target3.name=Pemdas
51 51
 target3.showInterface=false
52 52
 target3.type=ClassTarget
53
-target3.width=120
53
+target3.width=80
54 54
 target3.x=70
55
-target3.y=70
55
+target3.y=10
56
+target4.height=120
57
+target4.name=MainApplication
58
+target4.showInterface=false
59
+target4.type=ClassTarget
60
+target4.width=230
61
+target4.x=320
62
+target4.y=20