Quellcode durchsuchen

I found this online to compare to our PEMDAS funciton.

Elliott Stansbury vor 6 Jahren
Ursprung
Commit
ff09987466
3 geänderte Dateien mit 92 neuen und 0 gelöschten Zeilen
  1. BIN
      Main.class
  2. 9
    0
      Main.ctxt
  3. 83
    0
      Main.java

BIN
Main.class Datei anzeigen


+ 9
- 0
Main.ctxt Datei anzeigen

@@ -0,0 +1,9 @@
1
+#BlueJ class context
2
+comment0.target=Main
3
+comment1.params=input
4
+comment1.target=boolean\ pClose(java.lang.String)
5
+comment2.params=input\ num
6
+comment2.target=java.lang.Double\ calc(java.lang.String,\ int)
7
+comment3.params=args
8
+comment3.target=void\ main(java.lang.String[])
9
+numComments=4

+ 83
- 0
Main.java Datei anzeigen

@@ -0,0 +1,83 @@
1
+import java.util.regex.Matcher;
2
+import java.util.regex.Pattern;
3
+import java.util.Scanner;
4
+import java.util.ArrayList;
5
+public class Main{
6
+    public static ArrayList<String> pemdas = new ArrayList<String>();
7
+    public static boolean pClose(String input){
8
+        int tracker = 0;
9
+        for(int i =0; i < input.length(); i++){
10
+            if(input.charAt(i) == '(') tracker++;
11
+            else if (input.charAt(i) == ')') tracker--;
12
+            if(tracker < 0) return false;
13
+        }
14
+        return tracker == 0; 
15
+    }
16
+
17
+    public static Double calc(String input, int num){ // p =0, e = 1, md = 2, as=3
18
+        //System.out.println(input);
19
+        Double result = null;
20
+        String next;
21
+        Pattern filter = Pattern.compile(pemdas.get(num));
22
+        Matcher calcMatch = filter.matcher(input);
23
+        if(!calcMatch.find()){
24
+            if(num < 3) return calc(input, num+1);
25
+            return null;
26
+        }
27
+        if(num == 0){
28
+            Double pSol = calc(calcMatch.group(2), 1);
29
+            if(pSol !=null){ result = pSol;
30
+                next = input.substring(0, calcMatch.start()) + result.toString() + input.substring(calcMatch.end());
31
+            }
32
+            else{
33
+                next = input.substring(0, calcMatch.start()) + input.substring(calcMatch.end());
34
+            }
35
+        }
36
+        else{
37
+            switch(calcMatch.group(2)){
38
+                case "+":
39
+                result = Double.parseDouble(calcMatch.group(1)) + Double.parseDouble(calcMatch.group(3));
40
+                break;
41
+                case "-":
42
+                result = Double.parseDouble(calcMatch.group(1)) - Double.parseDouble(calcMatch.group(3));
43
+                break;
44
+                case "/":
45
+                result = Double.parseDouble(calcMatch.group(1)) / Double.parseDouble(calcMatch.group(3));
46
+                break;
47
+                case "*":
48
+                result = Double.parseDouble(calcMatch.group(1)) * Double.parseDouble(calcMatch.group(3));
49
+                break;
50
+                case "^":
51
+                result = Math.pow(Double.parseDouble(calcMatch.group(1)), Double.parseDouble(calcMatch.group(3)));
52
+                break;
53
+                
54
+            }
55
+            next = input.substring(0, calcMatch.start()) + result.toString() + input.substring(calcMatch.end());
56
+        }
57
+        //for(int i = num; i <= 3; i++){
58
+        //if(calc(next,  i) != null) {return calc(next, i);}
59
+        //}
60
+        if(filter.matcher(next).find()) return calc(next, num);
61
+        if(num < 3){
62
+            Double nextResult = calc(next, num+1);
63
+            if(nextResult !=null) return nextResult;
64
+        }
65
+        return result;
66
+    }
67
+
68
+    public static void main(String args[]){
69
+        System.out.println("PEMDAS calculator! Just write an expression to solve how you normally would, like \"(2+3)*3\"! Supports + - * / ^ % as its operators. \"stop\" will stop the program.");
70
+        Scanner scanner = new Scanner(System.in);
71
+        String input;
72
+        pemdas.add("(\\()([^\\(\\)]*)(\\))");//detects anything in parenthesis
73
+        pemdas.add("(-?\\d+\\.?\\d*)\\D*?(\\^)\\D*?(-?\\d+\\.?\\d*)"); //exponents
74
+        pemdas.add("(-?\\d+\\.?\\d*)\\D*?(\\*|\\/|%)\\D*?(-?\\d+\\.?\\d*)"); //multiplication, division, 
75
+        pemdas.add("(-?\\d+\\.?\\d*)\\D*?(\\+|-)\\D*?(-?\\d+\\.?\\d*)"); // addition, subraction
76
+        while(true){
77
+            input = scanner.nextLine();
78
+            if(input.toLowerCase().equals("stop")) break; 
79
+            if(!pClose(input)) System.out.println("Error: There's a problem with your parenthesis, so the result may be different from what you expect.");
80
+            System.out.println(input + " is " + calc(input, 0));
81
+        }
82
+    }
83
+}