Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Exercise20_23 {
  4. public static void main(String[] args) {
  5. // Create a Scanner
  6. Scanner input = new Scanner(System.in);
  7.  
  8. // Prompt the user to enter an expression
  9. System.out.print("Enter an expression: ");
  10. String expression = input.nextLine();
  11.  
  12. try {
  13. System.out.println(expression + " = " +
  14. evaluateExpression(expression));
  15. }
  16. catch (Exception ex) {
  17. System.out.println("Wrong expression: " + expression);
  18. }
  19. }
  20.  
  21. /** Evaluate an expression */
  22. public static int evaluateExpression(String expression) {
  23. // Create operandStack to store operands
  24. Stack<Integer> operandStack = new Stack<>();
  25.  
  26. // Create operandStack to store operators
  27. Stack<Character> operatorStack = new Stack<>();
  28.  
  29. // Insert blanks around (, ), +, -, /, *, ^, and %
  30. expression = insertBlanks(expression);
  31.  
  32. // Extract operands an operators
  33. String[] tokens = expression.split(" ");
  34.  
  35. // Phase 1: Scan tokens
  36. for (String token: tokens) {
  37. if (token.length() == 0) // Blank space
  38. continue; // Back to the while loop to extract the next token
  39. else if (token.charAt(0) == '+' || token.charAt(0) == '-') {
  40. // Process all +, -, *, /, ^, % in the top of the operator stack
  41. while (!operatorStack.isEmpty() &&
  42. (operatorStack.peek() == '+' ||
  43. operatorStack.peek() == '-' ||
  44. operatorStack.peek() == '*' ||
  45. operatorStack.peek() == '/' ||
  46. operatorStack.peek() == '^' ||
  47. operatorStack.peek() == '%')) {
  48. processAnOperator(operandStack, operatorStack);
  49. }
  50.  
  51. // Push the + or - operator into the operator stack
  52. operatorStack.push(token.charAt(0));
  53. }
  54. else if (token.charAt(0) == '*' ||
  55. token.charAt(0) == '/' || token.charAt(0) == '%') {
  56. // Process all *, /, % in the top of the operator stack
  57. while (!operatorStack.isEmpty() &&
  58. (operatorStack.peek() == '*' ||
  59. operatorStack.peek() == '/' ||
  60. operatorStack.peek() == '%' )) {
  61. processAnOperator(operandStack, operatorStack);
  62. }
  63.  
  64. // Push the *, / or % operator into the operator stack
  65. operatorStack.push(token.charAt(0));
  66. }
  67. else if (token.charAt(0) == '^') {
  68. // Process all '^' operators in the top of the operator stack
  69. while (!operatorStack.isEmpty() &&
  70. operatorStack.peek() == '^') {
  71. processAnOperator(operandStack, operatorStack);
  72. }
  73.  
  74. // Push the ^ operator into the operator stack
  75. operatorStack.push(token.charAt(0));
  76. }
  77. else if (token.trim().charAt(0) == '(') {
  78. operatorStack.push('('); // Push '(' to stack
  79. }
  80. else if (token.trim().charAt(0) == ')') {
  81. //Process all the operators in the stack until seeing '('
  82. while (operatorStack.peek() != '(') {
  83. processAnOperator(operandStack, operatorStack);
  84. }
  85.  
  86. operatorStack.pop(); // Pop the '(' symbol from the stack
  87. }
  88. else { // An operand scanned
  89. // Push an operand to the stack
  90. operandStack.push(new Integer(token));
  91. }
  92. }
  93.  
  94. // Phase 2: Process all the remaining operators in the stack
  95. while (!operatorStack.isEmpty()) {
  96. processAnOperator(operandStack, operatorStack);
  97. }
  98.  
  99. // Return the result
  100. return operandStack.pop();
  101. }
  102.  
  103. /** Process one operator: Take an operator from operatorStack and
  104. * apply it on the operands in the operandStack */
  105. public static void processAnOperator(
  106. Stack<Integer> operandStack, Stack<Character> operatorStack) {
  107. char op = operatorStack.pop();
  108. int op1 = operandStack.pop();
  109. int op2 = operandStack.pop();
  110. if (op == '+')
  111. operandStack.push(op2 + op1);
  112. else if (op == '-')
  113. operandStack.push(op2 - op1);
  114. else if (op == '*')
  115. operandStack.push(op2 * op1);
  116. else if (op == '/')
  117. operandStack.push(op2 / op1);
  118. else if (op == '^')
  119. operandStack.push((int)Math.pow(op2, op1));
  120. else if (op == '%')
  121. operandStack.push(op2 % op1);
  122. }
  123.  
  124. public static String insertBlanks(String s) {
  125. String result = "";
  126.  
  127. for (int i = 0; i < s.length(); i++) {
  128. if (s.charAt(i) == '(' || s.charAt(i) == ')' ||
  129. s.charAt(i) == '+' || s.charAt(i) == '-' ||
  130. s.charAt(i) == '*' || s.charAt(i) == '/' ||
  131. s.charAt(i) == '^' || s.charAt(i) == '%')
  132. result += " " + s.charAt(i) + " ";
  133. else
  134. result += s.charAt(i);
  135. }
  136.  
  137. return result;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement