943,704 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2176
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 18th, 2008
-1

Re: Need urgent help! please.

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
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Sep 18th, 2008
0

Re: Need urgent help! please.

Click to Expand / Collapse  Quote originally posted by ai0372 ...
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
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 18th, 2008
0

Re: Need urgent help! please.

Click to Expand / Collapse  Quote originally posted by ai0372 ...
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.

Quote ...
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
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Sep 18th, 2008
0

Re: Need urgent help! please.

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?!
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Sep 18th, 2008
0

Re: Need urgent help! please.

Click to Expand / Collapse  Quote originally posted by puneetkay ...
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?
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008
Sep 18th, 2008
0

Re: Need urgent help! please.

Click to Expand / Collapse  Quote originally posted by ai0372 ...
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!
Quote ...
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
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Sep 18th, 2008
0

Re: Need urgent help! please.

Click to Expand / Collapse  Quote originally posted by ai0372 ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 20th, 2008
0

Re: Need urgent help! please.

Thank you very much.. = ]
Reputation Points: 9
Solved Threads: 1
Junior Poster
ezkonekgal is offline Offline
109 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: I need help with my program
Next Thread in Java Forum Timeline: dynamic dispatch method





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


Follow us on Twitter


© 2011 DaniWeb® LLC