Elliott Stansbury пре 6 година
родитељ
комит
8f582a29e1
5 измењених фајлова са 137 додато и 37 уклоњено
  1. BIN
      .DS_Store
  2. 0
    17
      MainApplication.java
  3. 120
    0
      OrderOfOperations.java
  4. 0
    0
      README.TXT
  5. 17
    20
      package.bluej

+ 0
- 17
MainApplication.java Прегледај датотеку

@@ -1,17 +0,0 @@
1
- 
2
-
3
-/**
4
- * Created by leon on 2/9/18.
5
- */
6
-public class MainApplication {
7
-    public static void main(String[] args) {
8
-        Console.println("Welcome to my calculator!");
9
-        String s = Console.getStringInput("Enter a string");
10
-        Integer i = Console.getIntegerInput("Enter an integer");
11
-        Double d = Console.getDoubleInput("Enter a double.");
12
-
13
-        Console.println("The user input %s as a string", s);
14
-        Console.println("The user input %s as a integer", i);
15
-        Console.println("The user input %s as a d", d);
16
-    }
17
-}

+ 120
- 0
OrderOfOperations.java Прегледај датотеку

@@ -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
+}


+ 17
- 20
package.bluej Прегледај датотеку

@@ -1,22 +1,19 @@
1 1
 #BlueJ package file
2
-dependency1.from=MainApplication
3
-dependency1.to=Console
4
-dependency1.type=UsesDependency
5
-editor.fx.0.height=722
2
+editor.fx.0.height=709
6 3
 editor.fx.0.width=800
7
-editor.fx.0.x=640
4
+editor.fx.0.x=480
8 5
 editor.fx.0.y=23
9
-objectbench.height=214
6
+objectbench.height=198
10 7
 objectbench.width=595
11 8
 package.divider.horizontal=0.6
12
-package.divider.vertical=0.6847360912981455
13
-package.editor.height=473
9
+package.divider.vertical=0.685099846390169
10
+package.editor.height=439
14 11
 package.editor.width=493
15
-package.editor.x=35
16
-package.editor.y=60
17
-package.frame.height=759
12
+package.editor.x=115
13
+package.editor.y=24
14
+package.frame.height=709
18 15
 package.frame.width=619
19
-package.numDependencies=1
16
+package.numDependencies=0
20 17
 package.numTargets=2
21 18
 package.showExtends=true
22 19
 package.showUses=true
@@ -27,16 +24,16 @@ readme.width=47
27 24
 readme.x=10
28 25
 readme.y=10
29 26
 target1.height=50
30
-target1.name=Console
27
+target1.name=OrderOfOperations
31 28
 target1.showInterface=false
32 29
 target1.type=ClassTarget
33
-target1.width=80
34
-target1.x=80
35
-target1.y=200
30
+target1.width=140
31
+target1.x=70
32
+target1.y=70
36 33
 target2.height=50
37
-target2.name=MainApplication
34
+target2.name=Console
38 35
 target2.showInterface=false
39 36
 target2.type=ClassTarget
40
-target2.width=120
41
-target2.x=70
42
-target2.y=70
37
+target2.width=80
38
+target2.x=80
39
+target2.y=200