Advertisement
henryw

assignment 5

May 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. public class Evaluation2SK {
  2.  
  3. static String[] Statement = {"(1+2)*3+1","1+3-2/1","(1+2)-2*1","3+2*2-1","1+2*(1+1)"};
  4. static String post="";
  5. static int value;
  6.  
  7.  
  8. public static void main(String[] args){
  9.  
  10. for (int i = 0; i < Statement.length; i++){ // no of statements to be evaluated
  11. post="";
  12. // infix -> postfix
  13. System.out.println("infix = " +Statement[i]);
  14. Stack211<Character> st = new Stack211();
  15.  
  16.  
  17. for (int j = 0; j < Statement[i].length(); j++) {
  18. char c = Statement[i].charAt(j);
  19.  
  20. if (c >= '0' && c <= '9') {
  21. post += c;
  22. } // find out the number
  23.  
  24. // If the stack is empty or contains a left parenthesis on top, push the incoming operator onto the stack.
  25. else if (st.isEmpty() || (char)st.myStack[st.stackTop] == '(') {
  26. st.push(c);
  27.  
  28. } else if (c == '(') {//If the incoming symbol is a left parenthesis, push it on the stack.
  29. st.push(c);
  30.  
  31. } else if (c == ')') {//If the incoming symbol is a right parenthesis, pop the stack and print the operators until you see a left parenthesis. Discard the pair of parentheses.
  32. while ((char) st.myStack[st.stackTop] != '(') {
  33. post+=(st.pop());
  34. }
  35. st.pop();
  36.  
  37. } else if (checkprecedence(c) > (char) st.myStack[st.stackTop]) {//If the incoming symbol has higher precedence than the top of the stack, push it on the stack.
  38. st.push(c);
  39.  
  40. }else if (checkprecedence(c)== (char)st.myStack[st.stackTop]){//If the incoming symbol has equal precedence with the top of the stack, use association. If the association is left to right, pop and print the top of the stack and then push the incoming operator.
  41. post+=(st.pop());
  42. if(c=='*'||c=='+'){
  43. st.push(c);
  44. }
  45.  
  46. }else if (checkprecedence(c)< (char)st.myStack[st.stackTop]){//If the incoming symbol has lower precedence than the symbol on the top of the stack, pop the stack and print the top operator. Then test the incoming operator against the new top of stack
  47. post+=(st.pop());
  48. j--;
  49.  
  50. }else if(c=='*'||c=='+'){
  51. post+=(st.pop());
  52. }
  53.  
  54. }
  55. while(!st.isEmpty()) {
  56. post += st.pop();// end of first statement(1 + 2 ) * 3. postfix is now produced
  57. }
  58. System.out.println("postfix = " +post);
  59. //postfix = 1+2*3
  60.  
  61.  
  62.  
  63. Stack211<Integer> intSt = new Stack211();// postfix evaluation
  64. for (int k = 0; k < post.length(); k++){
  65. char c= post.charAt(k);
  66.  
  67. if (c >='0' && c<='9'){
  68. int v = c-'0';
  69. intSt.push(v);
  70. value+=v; //just test
  71.  
  72. } else {
  73. int x = intSt.pop();
  74. int y = intSt.pop();
  75. int value= calculator(x,y,c);
  76. intSt.push(value);//push the value back to stack top
  77. }
  78. }
  79. value=intSt.pop();//pop the value out
  80. System.out.println("value= " + value);
  81. System.out.println();
  82. }
  83.  
  84. }
  85. public static int checkprecedence(char c){// method for precedence
  86. int num;
  87. if (c=='*'||c=='/'){
  88. num=2;
  89. return num;
  90. }else if(c== '+'||c=='-'||c=='('){
  91. num= 1;
  92. return num;
  93. }
  94. return 0;
  95. }
  96. static int calculator(int x,int y, char c){
  97. if (c == '+') {// + pop calcu then push
  98.  
  99. return (y+x);
  100.  
  101. } else if (c == '-') {
  102.  
  103. return(y-x);
  104.  
  105. } else if (c == '*') {
  106.  
  107. return(y*x);
  108.  
  109. } else {
  110.  
  111. return(y/x);
  112. }
  113.  
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement