473,569 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Infix to postfix and evaluate: having problems

1 New Member
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 6532
samido
52 New Member
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
5967
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 occurances of two operands followed by an operator by their infix equivalent"
22
3773
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
11293
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> #include <stdlib.h> #include <string.h> #include <ctype.h>
1
2038
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 putting brackets when pushing operands on the stack is a good idea? 112*20 will yield (112)(20)* as a postfix string.
5
3738
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 guideline as to how to start this would be great. It has to work with assign.cpp which is like this: int main() { Infix infix; while...
30
5444
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”. Likewise, they write “(if test this that)” to mean “if (test) {this} else {that}”. LISP codes are all of the form “(a b c ...)”, where the
0
2517
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 code is given below: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<ctype.h> #include<string.h> #include<process.h>
2
6694
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 ( (, , } ) and if not should produce an error. The program should handle at least the following operators (+, -, *, /, %). Assume the operands are...
2
5567
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 and encountering loop everytime I execute it. Here's my sample code: ******************************************************************** ...
0
7711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7628
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7938
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6317
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5519
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5245
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3679
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1236
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.