Browse Source

resolved merge conflic with bluej

Jonathan Hinds 6 years ago
parent
commit
b43ca36d1a
5 changed files with 151 additions and 2 deletions
  1. BIN
      .DS_Store
  2. 5
    1
      Calculations.java
  3. 120
    0
      OrderOfOperations.java
  4. 0
    0
      README.TXT
  5. 26
    1
      package.bluej

BIN
.DS_Store View File


+ 5
- 1
Calculations.java View File

@@ -71,7 +71,7 @@ public class Calculations
71 71
             break;   
72 72
             
73 73
             case "clear":
74
-                Clear();
74
+                 Clear();
75 75
             break;
76 76
         }
77 77
         System.out.println(result);
@@ -99,6 +99,10 @@ public class Calculations
99 99
         //perform the operation
100 100
         //convert to string
101 101
         //return string
102
+        String answer = "";
103
+        
104
+        
105
+        
102 106
         return null;
103 107
     }
104 108
 

+ 120
- 0
OrderOfOperations.java View File

@@ -0,0 +1,120 @@
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
+}

+ 0
- 0
README.TXT View File


+ 26
- 1
package.bluej View File

@@ -9,9 +9,26 @@ dependency3.from=Calculations
9 9
 dependency3.to=Console
10 10
 dependency3.type=UsesDependency
11 11
 editor.fx.0.height=716
12
+editor.fx.0.height=709
12 13
 editor.fx.0.width=800
13
-editor.fx.0.x=409
14
+editor.fx.0.x=480
14 15
 editor.fx.0.y=23
16
+objectbench.height=198
17
+objectbench.width=595
18
+package.divider.horizontal=0.6
19
+package.divider.vertical=0.685099846390169
20
+package.editor.height=439
21
+package.editor.width=493
22
+package.editor.x=115
23
+package.editor.y=24
24
+package.frame.height=709
25
+package.frame.width=619
26
+package.numDependencies=0
27
+package.numTargets=2
28
+editor.fx.0.width=0
29
+editor.fx.0.x=0
30
+editor.fx.0.y=0
31
+editor.fx.0.height=717
15 32
 objectbench.height=200
16 33
 objectbench.width=595
17 34
 package.divider.horizontal=0.6
@@ -33,6 +50,12 @@ readme.width=47
33 50
 readme.x=10
34 51
 readme.y=10
35 52
 target1.height=50
53
+target1.name=OrderOfOperations
54
+target1.showInterface=false
55
+target1.type=ClassTarget
56
+target1.width=140
57
+target1.x=70
58
+target1.y=70
36 59
 target1.name=Calculations
37 60
 target1.showInterface=false
38 61
 target1.type=ClassTarget
@@ -44,6 +67,8 @@ target2.name=Console
44 67
 target2.showInterface=false
45 68
 target2.type=ClassTarget
46 69
 target2.width=80
70
+target2.x=80
71
+target2.y=200
47 72
 target2.x=20
48 73
 target2.y=150
49 74
 target3.height=50