471,854 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,854 software developers and data experts.

Infix to postfix and evaluate: having problems

1
this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help?

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Postfix
  5. {
  6. private static Stack operators = new Stack();
  7. private static Stack operands = new Stack();
  8.  
  9. public static void main(String argv[]) throws IOException
  10. {
  11. String infix;
  12.  
  13. //create an input stream object
  14. BufferedReader keyboard = new BufferedReader (new
  15. InputStreamReader(System.in));
  16.  
  17. //get input from user
  18. System.out.print("\nEnter the algebraic expression in infix: ");
  19. infix = keyboard.readLine();
  20.  
  21. //output as postfix
  22. System.out.println("The expression in postfix is:" + toPostfix(infix));
  23.  
  24. //get answer
  25. System.out.println("The answer to the equation is: " + evaluate
  26. (toPostfix(infix)) + "\n");
  27. }
  28.  
  29. private static String toPostfix(String infix)
  30. //converts an infix expression to postfix
  31. {
  32. StringTokenizer s = new StringTokenizer(infix);
  33. //divides the input into tokens
  34. String symbol, postfix = "";
  35. while (s.hasMoreTokens())
  36. //while there is input to be read
  37. {
  38. symbol = s.nextToken();
  39. //if it's a number, add it to the string
  40. if (Character.isDigit(symbol.charAt(0)))
  41. postfix = postfix + " " + (Integer.parseInt
  42. (symbol));
  43. else if (symbol.equals("("))
  44. //push (
  45. {
  46. Character operator = new Character('(');
  47. operators.push(operator);
  48. }
  49. else if (symbol.equals(")"))
  50. //push everything back to (
  51. {
  52. while (((Character)operators.peek()).charValue() != '(')
  53. {
  54. postfix = postfix + " " + operators.pop();
  55. }
  56. operators.pop();
  57. }
  58. else
  59. //print operators occurring before it that have greater precedence
  60. {
  61. while (!operators.empty() && !(operators.peek()).equals("(") && prec(symbol.charAt(0)) <= prec(((Character)operators.peek()).charValue()))
  62. postfix = postfix + " " + operators.pop();
  63. Character operator = new Character(symbol.charAt(0));
  64. operators.push(operator);
  65. }
  66. }
  67. while (!operators.empty())
  68. postfix = postfix + " " + operators.pop();
  69. return postfix;
  70. }
  71.  
  72. private static int evaluate(String postfix)
  73. {
  74. StringTokenizer s = new StringTokenizer(postfix);
  75. //divides the input into tokens
  76. int value;
  77. String symbol;
  78. while (s.hasMoreTokens())
  79. {
  80. symbol = s.nextToken();
  81. if (Character.isDigit(symbol.charAt(0)))
  82. //if it's a number, push it
  83. {
  84. Integer operand = new Integer(Integer.parseInt(symbol));
  85. operands.push(operand);
  86. }
  87. else //if it's an operator, operate on the previous two operands
  88. {
  89. int op2 = ((Integer)operands.pop()).intValue();
  90. int op1 = ((Integer)operands.pop()).intValue();
  91. int result = 0;
  92. switch(symbol.charAt(0))
  93. {
  94. case '*': {result = op1 * op2; break;}
  95. case '+': {result = op1 + op2; break;}
  96. case '-': {result = op1 - op2; break;}
  97. case '/': {result = op1 / op2; break;}
  98. case '%': {result = op1 % op2; break;}
  99. }
  100. Integer operand = new Integer(result);
  101. operands.push(operand);
  102. }
  103. }
  104. value = ((Integer)operands.pop()).intValue();
  105. return value;
  106. }
  107.  
  108. private static int prec(char x)
  109. {
  110. if (x == '+' || x == '-')
  111. return 1;
  112. if (x == '*' || x == '/' || x == '%')
  113. return 2;
  114. return 0;
  115. }
  116. }
Sep 18 '08 #1
1 6371
samido
52
what is the matter with this code ...? what it was suppose to do and not happenign yet, what do you pass as inputs and do you expect as out put...? you didn't ask any questionon this thread and I wonder what is it you want us to do.

Sam Rabophala
Sep 18 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

22 posts views Thread by Tony Johansson | last post: by
19 posts views Thread by caramel | last post: by
2 posts views Thread by ostorat_elwafa2 | last post: by
2 posts views Thread by zeroeight | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.