OrderOfOperations.java 6.2KB

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