Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Stack;
  3. import java.lang.*;
  4. public class Infix {
  5. public static double calculations(String here){
  6. char[]chars = here.toCharArray();
  7. MyStack<Character> operatorstack = new MyStack<Character>();
  8. MyStack<Double> valuestack = new MyStack<Double>();
  9. String operator;
  10. char topOperator;
  11. char nextCharacter;
  12. double operandTwo;
  13. double operandOne;
  14. double result;
  15. double next;
  16. for(char c: chars)
  17. {
  18. //System.out.println("c is:" + c);
  19. if(Character.isDigit(c)){
  20. System.out.println("is a digit: " + c);
  21. Double.parseDouble(Character.toString(c));
  22. }
  23. else {
  24. operator = String.valueOf(c);
  25. nextCharacter = c;
  26. switch(nextCharacter) {
  27.  
  28. case '^':
  29. operatorstack.push(nextCharacter);
  30. break;
  31.  
  32.  
  33. case '+':
  34. case '-':
  35. case '*':
  36. case '/':
  37.  
  38. while(!operatorstack.isEmpty()&& getPriority(nextCharacter) <= getPriority(operatorstack.peek()))
  39. {
  40. // Calculate operator at the top of the stack.
  41. topOperator = operatorstack.pop();
  42. operandTwo = valuestack.pop();
  43. operandOne = valuestack.pop();
  44.  
  45. if (topOperator == '*')
  46. result = operandOne * operandTwo;
  47. else if (topOperator == '/')
  48. result = operandOne / operandTwo;
  49. else if (topOperator == '+')
  50. result = operandOne + operandTwo;
  51. else if (topOperator == '-')
  52. result = operandOne - operandTwo;
  53. else
  54. throw new RuntimeException("You can't do that, illegal operator: " + topOperator);
  55.  
  56. valuestack.push(result);
  57. }
  58. operatorstack.push(nextCharacter);
  59. break;
  60. default:
  61. break;
  62. }
  63.  
  64.  
  65. }
  66. while(!operatorstack.isEmpty()){
  67. topOperator = operatorstack.pop();
  68.  
  69. operandTwo = valuestack.pop();
  70.  
  71. operandOne = valuestack.pop();
  72.  
  73.  
  74. if (topOperator == '*')
  75. result = operandOne * operandTwo;
  76. else if (topOperator == '/')
  77. result = operandOne / operandTwo;
  78. else if (topOperator == '+')
  79. result = operandOne + operandTwo;
  80. else if (topOperator == '-')
  81. result = operandOne - operandTwo;
  82. else
  83. throw new RuntimeException("You can't do that, illegal operator: " + topOperator);
  84. valuestack.push(result);
  85.  
  86. }
  87. }
  88. double answer = valuestack.peek();
  89. return answer;
  90. }
  91.  
  92. public static int getPriority(char nextCharacter) {
  93. if (nextCharacter == '*' || nextCharacter == '/')
  94. return 1;
  95. else if (nextCharacter == '+' || nextCharacter == '-')
  96. return 2;
  97. else if (nextCharacter == '=')
  98. return 3;
  99. else
  100. return Integer.MIN_VALUE;
  101.  
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement