Elliott Stansbury vor 6 Jahren
Ursprung
Commit
b41cb0d976
2 geänderte Dateien mit 12 neuen und 122 gelöschten Zeilen
  1. 12
    2
      Calculations.java
  2. 0
    120
      OrderOfOperations.java

+ 12
- 2
Calculations.java Datei anzeigen

@@ -1,4 +1,6 @@
1 1
 import java.util.ArrayList;
2
+import java.util.regex.Matcher;
3
+import java.util.regex.Pattern;
2 4
 
3 5
 public class Calculations
4 6
 {
@@ -89,7 +91,16 @@ public class Calculations
89 91
         //add this to the array list
90 92
         //if the size of the arraylist is 2
91 93
             //return the array list
92
-        //else return null
94
+        //else return null'
95
+        Pattern pattern = Pattern.compile("\\s?\\.?\\d+\\.?(\\d+)?\\s?");
96
+        Matcher matcher = pattern.matcher(userInput);
97
+        
98
+        while(matcher.find()){
99
+            String number = matcher.group().trim();
100
+            Double deci = Double.valueOf(number);
101
+            results.add(deci);
102
+        }
103
+        
93 104
         return results;
94 105
     }
95 106
     
@@ -98,7 +109,6 @@ public class Calculations
98 109
         //perform the operation
99 110
         //convert to string
100 111
         //return string
101
-        String answer = "";
102 112
         
103 113
         
104 114
         

+ 0
- 120
OrderOfOperations.java Datei anzeigen

@@ -1,120 +0,0 @@
1
-public class OrderOfOperations {
2
-    ArrayList<String> contents;
3
-    String item;
4
-    OrderOfOperations check;
5
-
6
-    public static void main (String[] args){
7
-        Scanner input = new Scanner(System.in);
8
-        System.out.println("Enter an operation: ");
9
-        String a = input.nextLine();
10
-        OrderOfOperations go = new OrderOfOperations();
11
-        a = go.brackets(a);
12
-        System.out.println("Result: "+a);
13
-    }
14
-
15
-    public String brackets(String s){             //method which deal with brackets separately
16
-        check = new OrderOfOperations();
17
-        while(s.contains(Character.toString('('))||s.contains(Character.toString(')'))){
18
-            for(int o=0; o<s.length();o++){
19
-                try{                                                        //i there is not sign
20
-                    if((s.charAt(o)==')' || Character.isDigit(s.charAt(o))) //between separate brackets
21
-                    && s.charAt(o+1)=='('){                         //or number and bracket,
22
-                        s=s.substring(0,o+1)+"*"+(s.substring(o+1));        //it treat it as
23
-                    }                                                       //a multiplication
24
-                }catch (Exception ignored){}                                //ignore out of range ex
25
-                if(s.charAt(o)==')'){                                  //search for a closing bracket
26
-                    for(int i=o; i>=0;i--){
27
-                        if(s.charAt(i)=='('){                          //search for a opening bracket
28
-                            String in = s.substring(i+1,o);
29
-                            in = check.recognize(in);
30
-                            s=s.substring(0,i)+in+s.substring(o+1);
31
-                            i=o=0;
32
-                        }
33
-                    }
34
-                }
35
-            }
36
-            if(s.contains(Character.toString('('))||s.contains(Character.toString(')'))||
37
-            s.contains(Character.toString('('))||s.contains(Character.toString(')'))){
38
-                System.out.println("Error: incorrect brackets placement");
39
-                return "Error: incorrect brackets placement";
40
-            }
41
-        }
42
-        s=check.recognize(s);
43
-        return s;
44
-    }
45
-
46
-    public String recognize(String s){              //method divide String on numbers and operators
47
-        PutIt putIt = new PutIt();
48
-        contents = new ArrayList<String>();         //holds numbers and operators
49
-        item = "";
50
-        for(int i=s.length()-1;i>=0;i--){           //is scan String from right to left,
51
-            if(Character.isDigit(s.charAt(i))){     //Strings are added to list, if scan finds
52
-                item=s.charAt(i)+item;              //a operator, or beginning of String
53
-                if(i==0){
54
-                    putIt.put();
55
-                }
56
-            }else{
57
-                if(s.charAt(i)=='.'){
58
-                    item=s.charAt(i)+item;
59
-                }else if(s.charAt(i)=='-' && (i==0 || (!Character.isDigit(s.charAt(i-1))))){
60
-                    item=s.charAt(i)+item;          //this part should recognize
61
-                    putIt.put();                    //negative numbers
62
-                }else{
63
-                    putIt.put();                //it add already formed number and
64
-                    item+=s.charAt(i);          //operators to list
65
-                    putIt.put();                //as separate Strings
66
-                    if(s.charAt(i)=='|'){       //add empty String to list, before "|" sign,
67
-                        item+=" ";          //to avoid removing of any meaningful String
68
-                        putIt.put();        //in last part of result method
69
-                    }
70
-                }
71
-            }
72
-        }
73
-        contents = putIt.result(contents, "^", "|");    //check Strings
74
-        contents = putIt.result(contents, "*", "/");    //for chosen
75
-        contents = putIt.result(contents, "+", "-");    //operators
76
-        return contents.get(0);
77
-    }
78
-    public class PutIt{
79
-        public void put(){
80
-            if(!item.equals("")){
81
-                contents.add(0,item);
82
-                item="";
83
-            }
84
-        }
85
-
86
-public ArrayList<String>result(ArrayList<String> arrayList, String op1, String op2){
87
-            int scale = 10;                              //controls BigDecimal decimal point accuracy
88
-            BigDecimal result = new BigDecimal(0);
89
-            for(int c = 0; c<arrayList.size();c++){
90
-                if(arrayList.get(c).equals(op1)|| arrayList.get(c).equals(op2)){
91
-                    if(arrayList.get(c).equals("^")){
92
-                        result = new BigDecimal(arrayList.get(c-1)).pow(Integer.parseInt(arrayList.get(c+1)));
93
-                    }else if(arrayList.get(c).equals("|")){
94
-                        result = new BigDecimal(Math.sqrt(Double.parseDouble(arrayList.get(c+1))));
95
-                    }else if(arrayList.get(c).equals("*")){
96
-                        result = new BigDecimal(arrayList.get(c-1)).multiply
97
-                                (new BigDecimal(arrayList.get(c+1)));
98
-                    }else if(arrayList.get(c).equals("/")){
99
-                        result = new BigDecimal(arrayList.get(c-1)).divide
100
-                                (new BigDecimal(arrayList.get(c+1)),scale,BigDecimal.ROUND_DOWN);
101
-                    }else if(arrayList.get(c).equals("+")){
102
-                        result = new BigDecimal(arrayList.get(c-1)).add(new BigDecimal(arrayList.get(c+1)));
103
-                    }else if(arrayList.get(c).equals("-")){
104
-                        result = new BigDecimal(arrayList.get(c-1)).subtract(new BigDecimal(arrayList.get(c+1)));
105
-                    }
106
-                    try{       //in a case of to "out of range" ex
107
-                        arrayList.set(c, (result.setScale(scale, RoundingMode.HALF_DOWN).
108
-                                stripTrailingZeros().toPlainString()));
109
-                        arrayList.remove(c + 1);            //it replace the operator with result
110
-                        arrayList.remove(c-1);              //and remove used numbers from list
111
-                    }catch (Exception ignored){}
112
-                }else{
113
-                    continue;            
114
-                }
115
-                c=0;                     //loop reset, as arrayList changed size
116
-            }
117
-            return arrayList;
118
-        }
119
-    }
120
-}