Procházet zdrojové kódy

Merge branch 'master' of ElliottStansbury/ZCW-MacroLabs-OOP-ScientificCalculator into master

jonathan-hinds před 6 roky
rodič
revize
fd34e55a0e
5 změnil soubory, kde provedl 165 přidání a 1 odebrání
  1. binární
      .DS_Store
  2. 5
    1
      Calculations.java
  3. 120
    0
      OrderOfOperations.java
  4. 0
    0
      README.TXT
  5. 40
    0
      package.bluej

binární
.DS_Store Zobrazit soubor


+ 5
- 1
Calculations.java Zobrazit soubor

@@ -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);
@@ -98,6 +98,10 @@ public class Calculations
98 98
         //perform the operation
99 99
         //convert to string
100 100
         //return string
101
+        String answer = "";
102
+        
103
+        
104
+        
101 105
         return null;
102 106
     }
103 107
 

+ 120
- 0
OrderOfOperations.java Zobrazit soubor

@@ -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 Zobrazit soubor


+ 40
- 0
package.bluej Zobrazit soubor

@@ -1,8 +1,33 @@
1 1
 #BlueJ package file
2
+<<<<<<< HEAD
3
+<<<<<<< HEAD
4
+editor.fx.0.height=709
5
+editor.fx.0.width=800
6
+editor.fx.0.x=480
7
+editor.fx.0.y=23
8
+objectbench.height=198
9
+objectbench.width=595
10
+package.divider.horizontal=0.6
11
+package.divider.vertical=0.685099846390169
12
+package.editor.height=439
13
+package.editor.width=493
14
+package.editor.x=115
15
+package.editor.y=24
16
+package.frame.height=709
17
+package.frame.width=619
18
+package.numDependencies=0
19
+package.numTargets=2
20
+=======
21
+editor.fx.0.height=0
22
+editor.fx.0.width=0
23
+editor.fx.0.x=0
24
+editor.fx.0.y=0
25
+=======
2 26
 editor.fx.0.height=717
3 27
 editor.fx.0.width=800
4 28
 editor.fx.0.x=240
5 29
 editor.fx.0.y=23
30
+>>>>>>> eac4369901be64afb2073e5d3077b9a43303c3c0
6 31
 objectbench.height=200
7 32
 objectbench.width=595
8 33
 package.divider.horizontal=0.6
@@ -15,6 +40,7 @@ package.frame.height=716
15 40
 package.frame.width=619
16 41
 package.numDependencies=0
17 42
 package.numTargets=3
43
+>>>>>>> 5c782a9d95f3f9208ab5ec62241ab490d899062b
18 44
 package.showExtends=true
19 45
 package.showUses=true
20 46
 project.charset=UTF-8
@@ -24,17 +50,30 @@ readme.width=47
24 50
 readme.x=10
25 51
 readme.y=10
26 52
 target1.height=50
53
+<<<<<<< HEAD
54
+target1.name=OrderOfOperations
55
+target1.showInterface=false
56
+target1.type=ClassTarget
57
+target1.width=140
58
+target1.x=70
59
+target1.y=70
60
+=======
27 61
 target1.name=Calculations
28 62
 target1.showInterface=false
29 63
 target1.type=ClassTarget
30 64
 target1.width=100
31 65
 target1.x=120
32 66
 target1.y=150
67
+>>>>>>> 5c782a9d95f3f9208ab5ec62241ab490d899062b
33 68
 target2.height=50
34 69
 target2.name=Console
35 70
 target2.showInterface=false
36 71
 target2.type=ClassTarget
37 72
 target2.width=80
73
+<<<<<<< HEAD
74
+target2.x=80
75
+target2.y=200
76
+=======
38 77
 target2.x=20
39 78
 target2.y=150
40 79
 target3.height=50
@@ -44,3 +83,4 @@ target3.type=ClassTarget
44 83
 target3.width=120
45 84
 target3.x=70
46 85
 target3.y=70
86
+>>>>>>> 5c782a9d95f3f9208ab5ec62241ab490d899062b