Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. package Algorithms.stucks;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. /**
  8. * Created by Ruslan Zhdan on 23.05.2016.
  9. */
  10. public class StackInPos
  11. {
  12. private int maxSize;
  13. private long[] stackArray;
  14. private int top;
  15.  
  16. public StackInPos(int maxSize)
  17. {
  18. this.maxSize = maxSize;
  19. stackArray = new long[maxSize];
  20. top = -1;
  21. }
  22.  
  23. public void push (long j){
  24. stackArray[++top] = j;
  25. }
  26.  
  27. public long pop(){
  28. return stackArray[top--];
  29. }
  30.  
  31. public long peek(){
  32. return stackArray[top];
  33. }
  34.  
  35. public boolean isEmpty(){
  36. return (top == -1);
  37. }
  38.  
  39. public boolean isFull(){
  40. return (top == maxSize-1);
  41. }
  42.  
  43. public char peekN(int n){
  44. return (char) stackArray[n];
  45. }
  46.  
  47. public void displayStack(String s){
  48. System.out.print(s);
  49. System.out.print("Stack (bottom --> top): ");
  50. for (int j = 0; j < maxSize; j++){
  51. System.out.print( peekN(j));
  52. System.out.print(' ');
  53. }
  54. System.out.println("");
  55. }
  56. }
  57.  
  58. class InToPos{
  59. private StackInPos theStack;
  60. private String input;
  61. private String output = "";
  62.  
  63. public InToPos(String in)
  64. {
  65. input = in;
  66. int stackSize = input.length();
  67. theStack = new StackInPos(stackSize);
  68. }
  69.  
  70. public String doTrans(){
  71. for (int j = 0; j< input.length(); j++){
  72. char ch = input.charAt(j);
  73. theStack.displayStack("For " + ch + " ");
  74. switch (ch){
  75. case '+':
  76. case '-':
  77. gotOper(ch, 1);
  78. break;
  79. case '*':
  80. case '/':
  81. gotOper(ch, 2);
  82. break;
  83. case '(':
  84. theStack.push(ch);
  85. break;
  86. case ')':
  87. gotParent(ch);
  88. break;
  89. default:
  90. output = output + ch;
  91. break;
  92. }
  93. }
  94. while (!theStack.isEmpty()){
  95. theStack.displayStack("While ");
  96. output = output + theStack.pop();
  97. }
  98. theStack.displayStack("End ");
  99. return output;
  100. }
  101.  
  102. public void gotOper(char opThis, int prec1)
  103. {
  104. while (!theStack.isEmpty()){
  105. char opTop = (char) theStack.pop();
  106. if (opTop == '('){
  107. theStack.push(opTop);
  108. break;
  109. }
  110. else {
  111. int prec2;
  112. if (opTop == '+' || opTop == '-')
  113. prec2 = 1;
  114. else
  115. prec2 = 2;
  116. if (prec2 < prec1){
  117. theStack.push(opTop);
  118. break;
  119. }
  120. else
  121. output = output + opTop;
  122. }
  123. }
  124. theStack.push(opThis);
  125. }
  126.  
  127. public void gotParent(char ch)
  128. {
  129. while (!theStack.isEmpty()){
  130. char chx = (char) theStack.pop();
  131. if (chx == '(')
  132. break;
  133. else
  134. output = output + chx;
  135. }
  136. }
  137. }
  138.  
  139. class InfixApp{
  140. public static void main(String[] args) throws IOException
  141. {
  142. String input, output;
  143. while (true){
  144. System.out.print("Enter infix: ");
  145. System.out.flush();
  146. input = getString();
  147. if (input.equals(""))
  148. break;
  149.  
  150. InToPos theTrans = new InToPos(input);
  151. output = theTrans.doTrans();
  152. System.out.println("Postfix is: " + output + '\n');
  153. }
  154. }
  155.  
  156. public static String getString() throws IOException
  157. {
  158. InputStreamReader isr = new InputStreamReader(System.in);
  159. BufferedReader br = new BufferedReader(isr);
  160. String s = br.readLine();
  161. return s;
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement