Need urgent help! please.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Re: Need urgent help! please.

 
-1
  #11
Sep 18th, 2008
k kinda figured out that i have to add spaces in between my mathematical expression.. it compiles.. my problem now is that it has some code smells.. i have to clean this job.. can you help me? tnx
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need urgent help! please.

 
0
  #12
Sep 18th, 2008
Originally Posted by ai0372 View Post
k kinda figured out that i have to add spaces in between my mathematical expression.. it compiles.. my problem now is that it has some code smells.. i have to clean this job.. can you help me? tnx

How can I make this any clearer? You have two threads. One person has already commented on this thread, probably not knowing about the other. People are donating their time here. Be courteous enough not to waste people's time with duplicate threads.

http://www.daniweb.com/forums/thread146370.html
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Need urgent help! please.

 
0
  #13
Sep 18th, 2008
Originally Posted by ai0372 View Post
it has some code smells, although it compiles.. can you help me again..

Stack is a raw type. References to generic type Stack<E> should be parameterized

Type safety: The method push(Object) belongs to the raw type Stack. References to generic type Stack<E> should be parameterized
Hello again,

Its working fine with me.

Enter the algebraic expression in infix: 4 + 4 * 2
The expression in postfix is: 4 4 2 * +
The answer to the equation is: 12
Only space is required.

If im still missing something. Please be more clear

Regards,
PuneetK
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Re: Need urgent help! please.

 
0
  #14
Sep 18th, 2008
Originally Posted by VernonDozier View Post
How can I make this any clearer? You have two threads. One person has already commented on this thread, probably not knowing about the other. People are donating their time here. Be courteous enough not to waste people's time with duplicate threads.

http://www.daniweb.com/forums/thread146370.html

Hmmmmm.. sorry if ever waste your time sir! how do i marked my first thread SOLVED?!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Re: Need urgent help! please.

 
0
  #15
Sep 18th, 2008
Originally Posted by puneetkay View Post
Hello again,

Its working fine with me.



Only space is required.

If im still missing something. Please be more clear

Regards,
PuneetK


Hello. thank you very much 4 the help you've given. going back to the code, what if i don't want to include spaces in between.. what changes should i make?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Need urgent help! please.

 
0
  #16
Sep 18th, 2008
Originally Posted by ai0372 View Post
Hello. thank you very much 4 the help you've given. going back to the code, what if i don't want to include spaces in between.. what changes should i make?
Ok, Its done!
Enter the algebraic expression in infix: 4+4+3*4/2
The expression in postfix is: 4 4 + 3 4 * 2 / +
The answer to the equation is: 14
  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. String symbol, postfix = "";
  33.  
  34. for (int i = 0; i < infix.length(); i++) {
  35. symbol = infix.charAt(i)+"";
  36. //if it's a number, add it to the string
  37. if (Character.isDigit(symbol.charAt(0)))
  38. postfix = postfix + " " + (Integer.parseInt
  39. (symbol));
  40. else if (symbol.equals("("))
  41. //push (
  42. {
  43. Character operator = new Character('(');
  44. operators.push(operator);
  45. }
  46. else if (symbol.equals(")"))
  47. //push everything back to (
  48. {
  49. while (((Character)operators.peek()).charValue() != '(')
  50. {
  51. postfix = postfix + " " + operators.pop();
  52. }
  53. operators.pop();
  54. }
  55. else
  56. //print operators occurring before it that have greater precedence
  57. {
  58. while (!operators.empty() && !(operators.peek()).equals("(") && prec(symbol.charAt(0)) <= prec(((Character)operators.peek()).charValue()))
  59. postfix = postfix + " " + operators.pop();
  60. Character operator = new Character(symbol.charAt(0));
  61. operators.push(operator);
  62. }
  63. }
  64.  
  65. while (!operators.empty())
  66. postfix = postfix + " " + operators.pop();
  67. return postfix;
  68. }
  69.  
  70. private static int evaluate(String postfix)
  71. {
  72. StringTokenizer s = new StringTokenizer(postfix);
  73. //divides the input into tokens
  74. int value;
  75. String symbol;
  76. while (s.hasMoreTokens())
  77. {
  78. symbol = s.nextToken();
  79. if (Character.isDigit(symbol.charAt(0)))
  80. //if it's a number, push it
  81. {
  82. Integer operand = new Integer(Integer.parseInt(symbol));
  83. operands.push(operand);
  84. }
  85. else //if it's an operator, operate on the previous two operands
  86. {
  87. int op2 = ((Integer)operands.pop()).intValue();
  88. int op1 = ((Integer)operands.pop()).intValue();
  89. int result = 0;
  90. switch(symbol.charAt(0))
  91. {
  92. case '*': {result = op1 * op2; break;}
  93. case '+': {result = op1 + op2; break;}
  94. case '-': {result = op1 - op2; break;}
  95. case '/': {result = op1 / op2; break;}
  96. case '%': {result = op1 % op2; break;}
  97. }
  98. Integer operand = new Integer(result);
  99. operands.push(operand);
  100. }
  101. }
  102. value = ((Integer)operands.pop()).intValue();
  103. return value;
  104. }
  105.  
  106. private static int prec(char x)
  107. {
  108. if (x == '+' || x == '-')
  109. return 1;
  110. if (x == '*' || x == '/' || x == '%')
  111. return 2;
  112. return 0;
  113. }
  114. }

Used forloop instead of StringTokenizer for toPostfix method.

Regards,
PuneetK
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need urgent help! please.

 
0
  #17
Sep 18th, 2008
Originally Posted by ai0372 View Post
Hmmmmm.. sorry if ever waste your time sir! how do i marked my first thread SOLVED?!
Hmmmmmmm.. Sarcastic, aren't we? At the bottom of your thread, you can click "Mark as Solved". Someone has already merged the two threads though, so it's too late. However, when you feel that THIS thread is solved, you can click "Mark as Solved" on it. It'll be in blue letters at the bottom of the thread, right below the last post.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

Re: Need urgent help! please.

 
0
  #18
Sep 20th, 2008
Thank you very much.. = ]
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 1793 | Replies: 17
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC