Infix to Postfix (HELP NEEDED)

Reply

Join Date: Oct 2009
Posts: 1
Reputation: annunfanwen is an unknown quantity at this point 
Solved Threads: 0
annunfanwen annunfanwen is offline Offline
Newbie Poster

Infix to Postfix (HELP NEEDED)

 
0
  #1
26 Days Ago
Hello,

I am having some difficulty in trying to figure out where I am going wrong in my program. I am trying to convert infix to postfix and from there I am evaluating the postfix. However, it is not compiling correctly. I've tried several things but nothing seems to work correctly for me.

  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class calculator {
  5.  
  6. private String infix;
  7. private String postfix = " ";
  8.  
  9. // public calculator(String in) {
  10. // input = in;
  11. // int SIZE = input.length();
  12.  
  13. // }
  14.  
  15. public String toPostfix(String infix) {
  16.  
  17. String expression;
  18. String postfix = " ";
  19.  
  20. Stack operatorStack = new Stack();
  21.  
  22. StringTokenizer s = new StringTokenizer(infix);
  23.  
  24. while (s.hasMoreTokens()) {
  25.  
  26. expression = s.nextToken();
  27. if (Character.isDigit(expression.charAt(0)))
  28. postfix = postfix + " " + (Integer.parseInt(expression));
  29. else if (expression.equals("("))
  30.  
  31. {
  32. Character operator = new Character('(');
  33. operatorStack.push(operator);
  34. } else if (expression.equals(")"))
  35.  
  36. {
  37. while (((Character) operatorStack.peek()).charValue() != '(') {
  38. postfix = postfix + " " + operatorStack.pop();
  39. }
  40. operatorStack.pop();
  41. } else
  42.  
  43. {
  44. while (!operatorStack.isEmpty()
  45. && !(operatorStack.peek()).equals("(")
  46. && prec(expression.charAt(0)) <= prec(((Character) operatorStack
  47. .peek()).charValue()))
  48. postfix = postfix + " " + operatorStack.pop();
  49. Character operator = new Character(expression.charAt(0));
  50. operatorStack.push(operator);
  51. }
  52. }
  53. while (!operatorStack.isEmpty())
  54. postfix = postfix + " " + operatorStack.pop();
  55. return postfix;
  56. }
  57.  
  58. private static int prec(char x) {
  59. if (x == '+' || x == '-')
  60. return 1;
  61. if (x == '*' || x == '/' || x == '%')
  62. return 2;
  63. return 0;
  64. }
  65.  
  66. public int evaluate(String postfix) {
  67. int a;
  68. int b;
  69. int result = 0;
  70.  
  71. Stack<Integer> myStack = new Stack<Integer>();
  72.  
  73. for (int i = 0; i < postfix.length(); i++) {
  74. char ch = postfix.charAt(i);
  75. if (ch >= '0' && ch <= '9') {
  76. myStack.push((int) (ch - '0'));
  77.  
  78. } else {
  79. switch (ch) {
  80. case '+':
  81. a = myStack.pop();
  82. b = myStack.pop();
  83. result = a + b;
  84. myStack.push(result);
  85. break;
  86.  
  87. case '-':
  88. a = myStack.pop();
  89. b = myStack.pop();
  90. result = a - b;
  91. myStack.push(result);
  92. break;
  93.  
  94. case '*':
  95. a = myStack.pop();
  96. b = myStack.pop();
  97. result = a * b;
  98. myStack.push(result);
  99. break;
  100. case '/':
  101. a = myStack.pop();
  102. b = myStack.pop();
  103. result = a / b;
  104. myStack.push(result);
  105. break;
  106.  
  107. }
  108. myStack.push(result);
  109.  
  110. }
  111. }
  112. myStack.push(result);
  113. return result;
  114. }
  115. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #2
26 Days Ago
properly write constructor
reply , what does mean parameter "String in" in contructor?
What information is transmitted to a class?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 455
Reputation: Grn Xtrm is on a distinguished road 
Solved Threads: 36
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #3
26 Days Ago
Try searching the forum, this question gets posted all the time. In fact my first post ever was regarding converting infix to postfix using stack. Also, I know there is a code snppet for converting infix to postfix in java. Give it a try.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #4
22 Days Ago
I know there is a code snppet for converting infix to postfix in java
here is! , and annunfanwen is an author

method
public String toPostfix(String infix)
adds usefull spaces as separator L28,38,48,54

method
public int evaluate(String postfix)
don't know about it . Also can't collect digits together to form number

In addition little stack tracer
  1. Stack<Integer> myStack = new Stack<Integer>() {
  2.  
  3. public Integer push(Integer i) {
  4. Integer j = super.push(i);
  5. System.out.println("push " + i);
  6. return j;
  7. }
  8.  
  9. public Integer pop() {
  10. Integer i = super.pop();
  11. System.out.println("pop " + i);
  12. return i;
  13. }
  14. };
Last edited by quuba; 22 Days Ago at 6:22 am.
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC