944,068 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 966
  • Java RSS
Oct 30th, 2009
0

Infix to Postfix (HELP NEEDED)

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
annunfanwen is offline Offline
3 posts
since Oct 2009
Oct 30th, 2009
0
Re: Infix to Postfix (HELP NEEDED)
properly write constructor
reply , what does mean parameter "String in" in contructor?
What information is transmitted to a class?
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 30th, 2009
0
Re: Infix to Postfix (HELP NEEDED)
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.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Nov 3rd, 2009
0
Re: Infix to Postfix (HELP NEEDED)
Quote ...
I know there is a code snppet for converting infix to postfix in java
here is! , and annunfanwen is an author

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

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

In addition little stack tracer
Java Syntax (Toggle Plain Text)
  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; Nov 3rd, 2009 at 6:22 am.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java applets
Next Thread in Java Forum Timeline: Input an integer number and display its largest factor





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC