Bläddra i källkod

Merge branch 'master' of https://git.zipcode.rocks/jonathan-hinds/ZCW-MacroLabs-OOP-ScientificCalculator

Elliott Stansbury 6 år sedan
förälder
incheckning
989c2447a3
3 ändrade filer med 281 tillägg och 5 borttagningar
  1. 2
    1
      Calculations.java
  2. 275
    0
      Pemdas.java
  3. 4
    4
      package.bluej

+ 2
- 1
Calculations.java Visa fil

@@ -7,6 +7,7 @@ public class Calculations
7 7
     public static Console console = new Console();
8 8
     public static ArrayList<String> library = new ArrayList<String>();
9 9
     public static String invalidArgumentsAmountError = "You have entered an invalid amount of arguments. Please only use two arguments, or switch to PEMDAS";
10
+    public static Pemdas pemdas = new Pemdas();
10 11
     
11 12
     public Calculations(){
12 13
    
@@ -100,7 +101,7 @@ public class Calculations
100 101
         while(matcher.find()){
101 102
             String number = matcher.group().trim();
102 103
             Double deci = Double.valueOf(number);
103
-            
104
+
104 105
             results.add(deci);
105 106
         }
106 107
         

+ 275
- 0
Pemdas.java Visa fil

@@ -0,0 +1,275 @@
1
+import java.util.regex.Pattern;
2
+import java.util.regex.Matcher;
3
+import java.util.ArrayList;
4
+
5
+public class Pemdas
6
+{
7
+    //empty constructor function
8
+    public Pemdas()
9
+    {
10
+
11
+    }
12
+    
13
+    //function to perform main calculations using pemdas
14
+    public static String calculate(String s)
15
+    {
16
+        
17
+        System.out.println("Original " + s);
18
+        //String p = null;
19
+        //if(getBetweenBraces(s) != null)
20
+        //{
21
+        //    p = getBetweenBraces(s);
22
+        //}
23
+        //System.out.println(p);
24
+
25
+        String e = null;
26
+        System.out.println("expo");
27
+        //create a duplicate of the method below, this one should have no print statemnts - testing purposes only.
28
+        //instead of the above, remove everyting except for assignment and initiailization
29
+        //this will work because getUpdateString2 cannot return null.
30
+        if(getUpdateString2(s,"^") != null)
31
+        {
32
+            //assign e to this string, after this operator has been calculated, and the original value of the sting has been modified to replace the occurnece with the calculations
33
+            e = getUpdateString2(s,"^");
34
+        }
35
+        
36
+        String m = null;
37
+        System.out.println("mult");
38
+        if(getUpdateString2(e,"*") != null)
39
+        {
40
+            m = getUpdateString2(e,"*");
41
+        }
42
+        
43
+        String d = null;
44
+        System.out.println("div");
45
+        if(getUpdateString2(m,"/") != null)
46
+        {
47
+            d = getUpdateString2(m,"/");
48
+        }
49
+         
50
+        String a = null;
51
+        System.out.println("add");
52
+        if(getUpdateString2(d,"+") != null)
53
+        {
54
+            a = getUpdateString2(d,"+");
55
+        }
56
+            
57
+        String ss = null;
58
+        System.out.println("sub");
59
+       if(getUpdateString2(a,"-") != null)
60
+       {
61
+           ss = getUpdateString2(a,"-");
62
+       }
63
+       
64
+       System.out.println("calculate 1.) Answer: " + ss);
65
+       return ss;
66
+ 
67
+    }
68
+    
69
+    /**
70
+    //search a string (s) and return all occurences of r (regex pattern)
71
+    public static ArrayList<String> searchReturnAll(String s, String r){
72
+            //create a regex pattern
73
+        Pattern pattern = Pattern.compile(r);
74
+        
75
+        //create an arraylist to store all occurences of exponentation
76
+        ArrayList<String> occurences = new ArrayList<String>();
77
+        //create a matcher to match the pattern to the string
78
+        Matcher matcher = pattern.matcher(s);
79
+        //while the last search exists
80
+        while(matcher.find())
81
+        {
82
+            //add the occurences to the list
83
+            occurences.add(matcher.group());
84
+        } 
85
+        
86
+        //if(occurences.size() > 0)
87
+        //{
88
+            return occurences;
89
+        //}
90
+        
91
+    }*/
92
+    
93
+    //search a string ( s ) for the pattern ( r ) and return a string resemblence of the first occurrence 
94
+    public static String searchReturnFirstString(String s, String r){
95
+        
96
+        Pattern pattern = Pattern.compile(r);
97
+        Matcher matcher = pattern.matcher(s);
98
+
99
+        if(matcher.find())
100
+        { 
101
+            System.out.println("searchReturnFirstString 1.) perform on " + s + ": " + matcher.group());
102
+            return matcher.group();
103
+        }
104
+        
105
+        return null;   
106
+    }
107
+    
108
+        
109
+    //search the string (target) and replace the first occrence of (pattern) with replacement.
110
+    public static String searchReplaceFirst(String target, String replacement, String pattern)
111
+    {
112
+        //if nothing is replaced, return null
113
+        if(target.equals(target.replaceFirst(pattern, replacement))){
114
+            return null;
115
+        //if anything is replaced, return an updated string with the appropriate replacements
116
+        } else {
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);
120
+            return result; 
121
+        }
122
+    }
123
+    
124
+    /** 
125
+        - Method Name: PEMDAS
126
+        - The method takes in two strings, one string ( s ) - the string being modified and the operator ( r )
127
+        - Search the string ( s ) for the first occurrence of the operator being performed.
128
+        - If no occurrence is found
129
+            - return the original string
130
+        - If an occurrence of - this operation being performed - exists.
131
+            - store the occurrence as a string ( occ ) 
132
+            - store the digit left of the operator ( left ) as a double
133
+            - store the digit right of the operator ( right ) as a double
134
+            - perform switch case based on the operator ( r ) 
135
+                - case statement for each operator allowed
136
+                    - calculate the product of left and right ( result )
137
+            - replace ( occ ) in ( s ) with ( result ), store this in ( f )
138
+            - if ( f ) and ( s ) do not have the same value
139
+                - run PEMDAS on ( f ) and assign it to ( f )
140
+            - if the original ( s ) has the same value as ( f ) 
141
+                - return ( f )    
142
+                
143
+                
144
+      NOTES
145
+        - The logic is there, and the method behaves properly, the regex is inaccurate and will have to resolved.
146
+        - regex needs to pick up two numbers on either side of a given operation - no matter what the operation is.
147
+        - values need to be matched for both integers and floaring point.
148
+    */
149
+
150
+    public static String getUpdateString2(String s, String operator)
151
+    {
152
+        String f = s;
153
+        System.out.println("getUpdateString2 1.) Input is: " + f);
154
+        String pattern = "\\s?\\.?\\d+\\.?(\\d+)?\\s?\\" + operator + "\\s?\\.?\\d+\\.?(\\d+)?\\s?";
155
+        System.out.println("getUpdateString2 2.) Pattern is " + pattern);
156
+
157
+        String occ = searchReturnFirstString(s, pattern);
158
+        System.out.println("getUpdateString2 3.) the occurance is " + occ);
159
+        //if we recieve a result
160
+        if(occ != null)
161
+        {   
162
+            //store the first and last digits as left and right (based on relationship to operator)
163
+            String leftString = searchReturnFirstString(occ, "\\s?\\.?\\d+\\.?(\\d+)?\\s?");
164
+            String rightStringOpp = searchReturnFirstString(occ, "\\" + operator + "\\s?\\.?\\d+\\.?(\\d+)?\\s?");
165
+            String rightString = searchReturnFirstString(rightStringOpp, "\\s?\\.?\\d+\\.?(\\d+)?\\s?");
166
+            //create a double to store the digit on the left and right, double to store the product of right and left
167
+            Double left = 0.0;
168
+            Double right = 0.0;
169
+            Double doubleResult = 0.0;
170
+            //if the left digit is found as a string, convert to double
171
+            //if the right digit is found as a string, convert to double
172
+            if(leftString != null){left = Double.parseDouble(leftString);}
173
+            if(rightString != null){right = Double.parseDouble(rightString);}
174
+            //switch statement for the operator to conduct proper arithmetics
175
+            switch(operator)
176
+            {
177
+                   case "^":
178
+                   doubleResult = Math.pow(left, right);
179
+                   System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
180
+                   break;
181
+                   
182
+                   case "+":
183
+                   doubleResult = left + right;
184
+                   System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
185
+                   break;
186
+                   
187
+                   case "-":
188
+                   doubleResult = left - right;
189
+                   System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
190
+                   break;
191
+                   
192
+                   case "*":
193
+                   doubleResult = left * right;
194
+                   System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
195
+                   break;
196
+                   
197
+                   case "/":
198
+                   doubleResult = left / right;
199
+                   System.out.println("getUpdateString2 .4) " + left + operator + right + "=" + doubleResult);
200
+                   break;
201
+                   
202
+                   default:
203
+                   doubleResult = 0.0;
204
+                   break;                
205
+            }
206
+            //convert the double result to a string result
207
+            String result = String.valueOf(doubleResult);
208
+            //String result = String.format("%.9f", doubleResult);
209
+            //replace the occurence of occ in s with result
210
+            f = searchReplaceFirst(s, result, pattern);
211
+            System.out.println("getUpdateString2 5.) iteration completed, f: " + f + "\n");
212
+            if(!f.equals(s))
213
+            {
214
+                f = getUpdateString2(f, operator);
215
+            } else {
216
+                return f;
217
+            }
218
+        } else {
219
+            return f;
220
+        }
221
+        return f;
222
+    }
223
+    
224
+    public static String getBetweenBraces(String s){
225
+        //int to hold the opening braces index in the string
226
+        //int to hold the closing brace index in the string
227
+        //set a position counter = 0 to determine if the brace is closed.
228
+        //boolean to determine if the first opening brace has been captured = false
229
+        //convert string to str array
230
+        //for each letter in char array
231
+            //if the char is an opening brace
232
+                //if the boolean varriable to determine of the first opening brace != true
233
+                    //store i from the for loop in the variable to hold the opening index
234
+                //pos --
235
+            //if the char is not an opening brace 
236
+                //pos ++
237
+            
238
+            //if the counter = 0                
239
+                //store i in the varriable to hold the closing brace
240
+                
241
+        int openBrace = 0;
242
+        int closeBrace = 0;
243
+        int position = 0;
244
+        boolean firstBrace = false;
245
+        String[] strArr = s.split("(?!^)");
246
+        
247
+        for(int i = 0; i < strArr.length; i ++)
248
+        {
249
+            String letter = strArr[i];
250
+            if(letter.equals("("))
251
+            {
252
+                if(firstBrace == false)
253
+                {
254
+                    firstBrace = true;
255
+                    openBrace = i + 1;
256
+                }
257
+                position --;
258
+            } else if(letter.equals(")"))
259
+            {
260
+                position ++;
261
+            }
262
+            
263
+            if(position == 0)
264
+            {
265
+                closeBrace = i;
266
+                break;
267
+            } else {
268
+              return null;
269
+            }
270
+        }
271
+        
272
+        String result = s.substring(openBrace, closeBrace);
273
+        return result;
274
+    }
275
+}

+ 4
- 4
package.bluej Visa fil

@@ -8,10 +8,10 @@ dependency2.type=UsesDependency
8 8
 dependency3.from=Calculations
9 9
 dependency3.to=Console
10 10
 dependency3.type=UsesDependency
11
-editor.fx.0.height=709
12
-editor.fx.0.width=800
13
-editor.fx.0.x=240
14
-editor.fx.0.y=23
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 15
 objectbench.height=198
16 16
 objectbench.width=595
17 17
 package.divider.horizontal=0.6