Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. import java.util.Stack;
  2.  
  3. public class Parsing{
  4. public static void main (String...args){
  5. String a = "((15/(7-(1+1)))*3)-(2+(1+1))";
  6. Stack<String> b = prepareExpression(a);
  7.  
  8. for (String x : b) System.out.printf("%s ",x);
  9.  
  10. System.out.println();
  11. double k = expression(b);
  12. System.out.println(k);
  13. }
  14. public static Stack<String> prepareExpression(String a){ // seperates operators/digits (Stack already reversed in order to evaluate left to right)
  15. Stack<String> result = new Stack<String>();
  16.  
  17. String numberBuffer = "";
  18.  
  19. for (int i = a.length()-1; i>=0;i--){
  20. char k = a.charAt(i);
  21. if (Character.isDigit(k)) numberBuffer=k+numberBuffer;// digits
  22.  
  23. else if (k=='-' || k =='.' || k =='E'){ // possible digits
  24. if (k=='-') {
  25. if (i==0) numberBuffer=k+numberBuffer;
  26. else if (a.charAt(i-1) =='(' || a.charAt(i-1) =='E') numberBuffer=k+numberBuffer;
  27. //else if (a.charAt(i-1) ==' ') numberBuffer=k+numberBuffer; // OPTIONAL 2+ -1 ('2','+',' ','-','1') will be valid (will convert to -1)
  28. else {
  29. if (!numberBuffer.isEmpty()){
  30. result.push(numberBuffer);
  31. numberBuffer="";
  32. }
  33. result.push(Character.toString(k));
  34. }
  35.  
  36. }
  37. else if (k=='.' || k=='E') numberBuffer=k+numberBuffer;
  38. }
  39.  
  40. else if (k=='+'|| k =='*'|| k =='/'|| k =='^'|| k =='('|| k ==')') { // nondigits
  41. if (!numberBuffer.isEmpty()){
  42. result.push(numberBuffer);
  43. numberBuffer="";
  44. }
  45. result.push(Character.toString(k));
  46. }
  47. else System.out.println("Bad input");
  48. }
  49. if (!numberBuffer.isEmpty()) result.push(numberBuffer);
  50. return result;
  51. }
  52.  
  53. public static double expression(Stack<String> a){
  54. double t=term(a);
  55. if (a.isEmpty()) return t;
  56.  
  57. String operand = a.pop();
  58. if (operand.matches("\\+")){
  59. return t+term(a);
  60. }
  61. else if (operand.matches("-")){
  62. return t-term(a);
  63. }
  64. else {
  65. a.push(operand);
  66. }
  67. return t;
  68. }
  69. public static double term(Stack<String> a){
  70. double t = additionals(a);
  71. if (a.isEmpty()) return t;
  72.  
  73. String operand = a.pop();
  74.  
  75. if (operand.matches("\\*")){
  76. return t*additionals(a);
  77. }
  78. else if (operand.matches("/")){
  79. return t/additionals(a);
  80. }
  81. else {
  82. a.push(operand);
  83. }
  84. return t;
  85. }
  86. public static double additionals(Stack<String> a){
  87. double t = primary(a);
  88. if (a.isEmpty()) return t;
  89.  
  90. String operand = a.pop();
  91.  
  92. if (operand.matches("\\^")){
  93. return Math.pow(t,primary(a));
  94. } else{
  95. a.push(operand);
  96. }
  97. return t;
  98. }
  99. public static double primary(Stack<String> a){
  100. String k = a.pop();
  101. if (k.matches("\\(")){
  102. double d = expression(a);
  103. if (!a.pop().matches("\\)")){
  104. System.out.println("hi");
  105. }
  106. return d;
  107. }
  108. return Double.parseDouble(k);
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement