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