Guest User

Untitled

a guest
Jul 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class myStack
  4. {
  5. private static Stack stax = new Stack();
  6.  
  7. public static void main (String [] args)
  8. {
  9. Scanner rice = new Scanner(System.in);
  10. String lawl = " ";
  11. System.out.println("Enter Equation: ");
  12. String werd = rice.nextLine();
  13. if (isGroupingOK(werd))
  14. lawl = infixToPostfix(werd);
  15. else System.out.println("Incorrect use of grouping signs");
  16. evaluator(lawl);
  17. /*System.out.println("Input expression to check if grouping is OK: ");
  18. String dawg = rice.nextLine();
  19. System.out.println(isGroupingOK(dawg));*/
  20.  
  21. }
  22.  
  23. public static String infixToPostfix (String exp)
  24. {
  25. String post = " ";
  26. String val = " ";
  27.  
  28. for (int i = 0; i < exp.length(); i++)
  29. {
  30. char n = exp.charAt(i);
  31.  
  32. if (n == ('+') || n == ('-') || n == ('*') || n == ('/'))
  33. {
  34. while (!stax.empty() && precedence(stax.peek()) >= precedence(n))
  35. post += stax.pop();
  36. stax.push(n);
  37. }
  38. else if (n == ('('))
  39. stax.push(n);
  40.  
  41. else if (n == (')'))
  42. {
  43. while (!stax.peek().equals('('))
  44. post += stax.pop();
  45. stax.pop();
  46. }
  47. else if (n == ('.'))
  48. {
  49. post += n;
  50. int q = 0;
  51. for (q = i+1; q < exp.length() - i; q++)
  52. {
  53. char o = exp.charAt(q);
  54. if (!isOperator(o))
  55. post += o;
  56. else break;
  57. }
  58. i = q-1;
  59. post += ',';
  60.  
  61. }
  62. else
  63. {
  64. post += n;
  65. int z = 0;
  66. for (z = i+1; z < exp.length() - i; z++)
  67. {
  68. char g = exp.charAt(z);
  69. if (!isOperator(g))
  70. post += g;
  71. else break;
  72. }
  73. i = z-1;
  74. post += ',';
  75. }
  76. }
  77.  
  78. while (!stax.empty())
  79. post += stax.pop();
  80. System.out.println("Post fix expression equals: "+post.trim());
  81. return post;
  82. }
  83.  
  84. public static int evaluator (String exp)
  85. {
  86.  
  87. for(int i = 0; i < exp.length(); i++)
  88. {
  89. char c = exp.charAt(i);
  90. String a = "";
  91. int k = 0;
  92.  
  93. if(c =='+')
  94. {
  95. int sum=Integer.parseInt((String)stax.pop())+Integer.parseInt((String)stax.pop());
  96. stax.push(Integer.toString(sum));
  97. }
  98. else if(c =='*')
  99. {
  100. int product=Integer.parseInt((String)stax.pop())*Integer.parseInt((String)stax.pop());
  101. stax.push(Integer.toString(product));
  102. }
  103. else if(c =='/')
  104. {
  105. int denom=Integer.parseInt((String)stax.pop());
  106. int quotient=Integer.parseInt((String)stax.pop())/denom;
  107. stax.push(Integer.toString(quotient));
  108.  
  109. }
  110. else if(c =='-')
  111. {
  112. int subtractor=Integer.parseInt((String)stax.pop());
  113. int difference=Integer.parseInt((String)stax.pop())-subtractor;
  114. stax.push(Integer.toString(difference));
  115. }
  116. else
  117. {
  118. for (k = i+1; k < exp.length() - i; k++)
  119. {
  120. char g = exp.charAt(k);
  121. if (g == ',')
  122. {
  123. a += exp.substring(i, k-1);
  124. i = k+1;
  125. break;
  126. }
  127. }
  128. stax.push(a);
  129. System.out.println(stax.peek());
  130. }
  131. }
  132. System.out.println("Post fix expression evaluates to: "+stax.peek());
  133. return Integer.parseInt((String)stax.pop());
  134. }
  135.  
  136.  
  137. public static int precedence(Object x)
  138. {
  139. if(x.equals('+') || x.equals('-'))
  140. return 1;
  141.  
  142. else if(x.equals('*') || x.equals('/'))
  143. return 2;
  144.  
  145. else return 0;
  146. }
  147.  
  148.  
  149.  
  150. public static boolean isGroupingOK(String infixExp)
  151. {
  152. Stack stax2 = new Stack();
  153. Object output = ' ';
  154.  
  155. for (int i = 0; i < infixExp.length(); i++)
  156. {
  157. char temp = infixExp.charAt(i);
  158. String string = " ";
  159. if (isOpen(temp))
  160. {
  161. stax2.push(temp);
  162. }
  163. else if (isClose(temp))
  164. {
  165. if (stax2.isEmpty())
  166. return false;
  167. else
  168. {
  169. output = stax2.peek();
  170. return (isMatch(output, temp));
  171. }
  172. }
  173.  
  174. }
  175. return true;
  176. }
  177.  
  178.  
  179. public static boolean isOpen(char c)
  180. {
  181. if(c == '(' || c == '[' || c == '{')
  182. return true;
  183. else
  184. return false;
  185. }
  186.  
  187. public static boolean isClose(char c)
  188. {
  189. if(c == ')' || c == ']' || c == '}')
  190. return true;
  191. else
  192. return false;
  193. }
  194. public static boolean isMatch(Object c, char y)
  195. {
  196. if((c.equals('(') && y == ')') || (c.equals('[') && (y == ']')) || (c.equals('{') && y == '}'))
  197. return true;
  198. else
  199. return false;
  200. }
  201. public static boolean isOperator(char x)
  202. {
  203. if (x == ('+') || x == ('-') || x == ('*') || x == ('/') || x == (','))
  204. return true;
  205. else return false;
  206. }
  207.  
  208. }
Add Comment
Please, Sign In to add comment