Guest User

Untitled

a guest
Oct 25th, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import java.util.Stack;
  2. import java.util.Vector;
  3.  
  4. public class OP{
  5. static void printStack(Stack st){
  6. for(int i=0; i<st.size(); i++)
  7. System.out.print("["+ st.get(i) +"]");
  8. System.out.println();
  9. }
  10.  
  11. static double add(double a, double b){
  12. return b+a;
  13. }
  14. static double sub(double a, double b){
  15. return b-a;
  16. }
  17. static double mlt(double a, double b){
  18. return b*a;
  19. }
  20. static double div(double a, double b){
  21. if (a!=0)
  22. return b/a;
  23. else
  24. System.exit(1);
  25. return 0;
  26. }
  27.  
  28. static double getDouble(Object o){
  29. return Double.parseDouble(o.toString());
  30. }
  31.  
  32. static int pri(String s){
  33. char c = s.charAt(0);
  34. switch(c){
  35. case '+':
  36. case '-':
  37. return 1;
  38. case '*':
  39. case '/':
  40. return 2;
  41. }
  42. return 0;
  43. }
  44.  
  45. static double examine(String exp){
  46. Stack stack = new Stack();
  47. String strArr[] = exp.split(" ");
  48.  
  49. for (int i=0; i<strArr.length; i++){
  50. if(strArr[i].equals("+")){
  51. stack.push(add(getDouble(stack.pop()), getDouble(stack.pop())));
  52. }else if(strArr[i].equals("-")){
  53. stack.push(sub(getDouble(stack.pop()), getDouble(stack.pop())));
  54. }else if(strArr[i].equals("*")){
  55. stack.push(mlt(getDouble(stack.pop()), getDouble(stack.pop())));
  56. }else if(strArr[i].equals("/")){
  57. stack.push(div(getDouble(stack.pop()), getDouble(stack.pop())));
  58. }else{
  59. stack.push(strArr[i]);
  60. }
  61. }
  62. return getDouble(stack.pop());
  63. }
  64.  
  65. static String pre(String in){
  66. Stack stack = new Stack();
  67. String inArr[] = in.split(" ");
  68. String out="";
  69.  
  70. for (int i=0; i<inArr.length; i++){
  71. if (inArr[i].equals("(")){
  72. stack.push("(");
  73. }else if (inArr[i].equals(")")){
  74. while(true){
  75. if(stack.empty()){
  76. System.out.println("Parentheses mismatch!");
  77. System.exit(1);
  78. }
  79. String opp = stack.pop().toString();
  80. if(opp.equals("("))
  81. break;
  82. else
  83. out += opp+" ";
  84. }
  85. if(!stack.empty())
  86. out += stack.pop().toString()+" ";
  87. }else if(inArr[i].equals("+")){
  88. while(stack.size() > 0 && pri("+") <= pri(stack.get(stack.size()-1).toString()))
  89. out+=stack.pop().toString()+" ";
  90. stack.push("+");
  91. }else if(inArr[i].equals("-")){
  92. while(stack.size() > 0 && pri("-") <= pri(stack.get(stack.size()-1).toString()))
  93. out+=stack.pop().toString()+" ";
  94. stack.push("-");
  95. }else if(inArr[i].equals("*")){
  96. while(stack.size() > 0 && pri("*") <= pri(stack.get(stack.size()-1).toString()))
  97. out+=stack.pop().toString()+" ";
  98. stack.push("*");
  99. }else if(inArr[i].equals("/")){
  100. while(stack.size() > 0 && pri("/") <= pri(stack.get(stack.size()-1).toString()))
  101. out+=stack.pop().toString()+" ";
  102. stack.push("/");
  103. }else{
  104. out+=inArr[i]+" ";
  105. }
  106. }
  107.  
  108. while(!stack.empty()){
  109. String opp = stack.pop().toString();
  110. if(opp.equals("(")){
  111. System.out.println("Parentheses mismatch!");
  112. System.exit(1);
  113. }
  114. out+=opp+" ";
  115. }
  116. return out;
  117. }
  118.  
  119. public static void main(String args[]){
  120. Stack stack = new Stack();
  121. String exp = "( 56 + 3 ) * 2 / 2 * 42 / ( 2 * ( 2 ) + 3 ) - 194";
  122. String out = pre(exp);
  123. System.out.println(exp);
  124. System.out.println(out);
  125. System.out.println(examine(out));
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment