473,416 Members | 1,685 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 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 6516
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

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

Similar topics

5
by: KidLogik | last post by:
Hello! I am converting an infix expression string into a postfix so that I will be able to evaluate it easier -> (5*(((9+8)*(4*6))+7)) == 598+46**7+* I believe the rule is "Replace all...
22
by: Tony Johansson | last post by:
Hello Experts! I'm reading i a book about C++ and they mention infix with telling what it is. I hope you out there can do so. Many thanks! //Tony
19
by: caramel | last post by:
i've been working on this program forever! now i'm stuck and going insane because i keep getting a syntax error msg and i just can't see what the compiler is signaling to! #include <stdio.h>...
1
by: tomerdr | last post by:
Hi, My infix expression never have brackets, So how do i convert it to postfix? For example 112*20 will yield 11220* Which cannot be correctly evaluate(which is it 1*1220 or 11*220...?) Is...
5
by: Thumbski | last post by:
Alright basically I just have one simple question. I can code perfectly fine and don't have any problems with the .cpp of the infix class but with my header I can't get anything to work really, any...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”....
0
by: coolguyjas07 | last post by:
plzzzzzz help me out to run my source code ......... i hav written dis code to convert infix expression to postfix but not getting desired output.... plzzz help me to get correct output.. d source...
2
by: ostorat_elwafa2 | last post by:
program that uses a stack to convert a given infix expression to a postfix expression and then to evaluate it. program should first check if the infix expression entered has balanced brackets ( ...
2
by: zeroeight | last post by:
Hi guys, I'm a newbie here, I just want to seek for help regarding my program. I want to implement an infix to postfix conversion only accepting numbers. Right now, I already debugged the errors...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.