Please Help Stuck Need Help Asap!!!

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

Join Date: Nov 2006
Posts: 47
Reputation: nnobakht is an unknown quantity at this point 
Solved Threads: 1
nnobakht nnobakht is offline Offline
Light Poster

Please Help Stuck Need Help Asap!!!

 
0
  #1
Mar 26th, 2007
hey guys im trying to write a simple caclculator. i have written the code and the functions for it but the the main when i try to use the Scanner function to get user input and to put it all together it does not work. the code is below i have to get the user input and if they call functions parse, convert and eval to do it. if they use p then the numbers then it it is in postfix and if it is a number to jsut evaluated and q exits it. can some1 help me with this. the code is posted below and the explanation is atached.
  1. import java.util.*;
  2. import java.math.*;
  3. import java.io.*;
  4. import java.lang.*;
  5.  
  6.  
  7. public class Tokens{
  8.  
  9. private int position=0; //Postion to put in array. Increases as the String is tokenized.
  10. private String [] Tokenized= new String[500]; //Tokenized version of array
  11. private Stack operand = new Stack(); //Operand Stack for conversion
  12. private static Stack output=new Stack(); //Output Stack for conversion. (Everything will be in here at end of conversion)
  13. private Stack reverse = new Stack(); //reverses the output into here
  14.  
  15. //public Tokens()
  16. /********************************
  17.   *Start of tokenize class. Once called it will use the Parse function
  18.   *to tokenize the given String (ie. user input)*/
  19. //{
  20.  
  21.  
  22. //}
  23.  
  24.  
  25. public void Parse(String s){
  26. /****************************
  27.   *Parse Function. Takes Input as string and tokenizes it to be able to use as a Token.
  28.   ****************************/
  29. String temp=""; //Digits and operands are temporarily stored, seperatly in here.
  30. for (int i=0; i<s.length();i++){
  31. if(Character.isDigit(s.charAt(i))){
  32. temp+=s.substring(i,i+1);
  33. }
  34. else
  35. {
  36. if(temp != "")
  37. {
  38. Tokenized[position]=temp;
  39. position++;
  40. }
  41. temp="";
  42. switch (s.charAt(i)){
  43. case '+':
  44. temp="+";
  45. break;
  46. case '-':
  47. temp="-";
  48. break;
  49. case '(':
  50. temp="(";
  51. break;
  52. case ')':
  53. temp=")";
  54. break;
  55. case '/':
  56. temp="/";
  57. break;
  58. case '*':
  59. if (s.charAt(i+1) == '*')
  60. {
  61. temp="^";
  62. i++;
  63. }
  64. else{
  65. temp="*";
  66. }
  67. break;
  68. case ' ':
  69. break;
  70. default:
  71. System.out.println("Incorrect format.");
  72. }
  73. if(temp != "")
  74. {
  75. Tokenized[position]=temp;
  76. position++;
  77. }
  78. temp = "";
  79. }
  80. }
  81. }
  82.  
  83. /*WORKS TO HERE NO PROBLEM!!!! */
  84.  
  85. public int evaluate(String x){
  86. /*************************
  87.   Evaluates the given sign.
  88.   * +, - have same value:1
  89.   * *, / have same value:2
  90.   * ^ has value of: 3
  91.   * otherwise it is value: 0
  92.   *************************/
  93. if(x.equals("+") )
  94. {
  95. return 1;
  96. }
  97. else if(x.equals("-"))
  98. {
  99. return 1;
  100. }
  101. else if(x.equals("*"))
  102. {
  103. return 2;
  104. }
  105. else if (x.equals("/"))
  106. {
  107. return 2;
  108. }
  109. else if(x.equals("^"))
  110. {
  111. return 3;
  112. }
  113. else
  114. {
  115. return 4;
  116. }
  117.  
  118. }
  119. public void Bracket()
  120. {
  121. String temp=((String)(operand.peek()));
  122. while(!((String)(operand.peek())).equals("("))
  123. {
  124. output.push(operand.pop());
  125. }
  126. operand.pop();
  127. }
  128. public void convert(String[] Tokenized){
  129. /****************************************************
  130.   *takes the token created in Parse function and
  131.   *converts it from Infix notation to postfix notion
  132.   *Uses evaluate function to see what to do with signs
  133.   * and puts it in 2 stacks operand and output
  134.   *untill the end. at the end everything is in
  135.   *stack output which will be returned
  136.   ****************************************************/
  137. for (int j=0; j<position; j++)
  138. {
  139. if (Character.isDigit(Tokenized[j].charAt(0)))
  140. {
  141. output.push(Tokenized[j]);
  142. }
  143. else
  144. {
  145.  
  146. if (!Tokenized[j].equals("("))
  147. {
  148. if (operand.empty())
  149. {
  150. operand.push(Tokenized[j]);
  151. }
  152. else if (((String)(operand.peek())).equals("("))
  153. {
  154. operand.push(Tokenized[j]);
  155. }
  156. else if (Tokenized[j].equals(")"))
  157. {
  158. Bracket();
  159. }
  160. else if (Tokenized[j].equals("^"))
  161. {
  162. if (((String)(operand.peek())).equals("^"))
  163. {
  164. operand.push(Tokenized[j]);
  165. }
  166. else
  167. {
  168. int second=evaluate((String)(operand.peek()));
  169. int first=evaluate(Tokenized[j]);
  170. if (first > second)
  171. {
  172. operand.push(Tokenized[j]);
  173. }
  174. else if (first == second)
  175. {
  176. output.push(operand.pop());
  177. operand.push(Tokenized[j]);
  178. }
  179. else if (first < second)
  180. {
  181. output.push(operand.pop());
  182. operand.push(Tokenized[j]);
  183. }
  184. }
  185. }
  186. else
  187. {
  188. int second=evaluate((String)(operand.peek()));
  189. int first=evaluate(Tokenized[j]);
  190. if (first > second)
  191. {
  192. operand.push(Tokenized[j]);
  193. }
  194. else if (first == second)
  195. {
  196. output.push(operand.pop());
  197. operand.push(Tokenized[j]);
  198. }
  199. else if (first < second)
  200. {
  201. output.push(operand.pop());
  202. operand.push(Tokenized[j]);
  203. }
  204. }
  205. }
  206. else if (Tokenized[j].equals("("))
  207. {
  208. operand.push(Tokenized[j]);
  209. }
  210. }
  211. }
  212. while(!operand.empty())
  213. {
  214. output.push(operand.pop());
  215. }
  216. while (!output.empty())
  217. {
  218. reverse.push(output.pop());
  219. }
  220.  
  221. }
  222.  
  223. public double eval()
  224. /********************************
  225.   *takes token output which is in postfix notation
  226.   *Reads it and caluates it using the Switch/Case.
  227.   ********************************/
  228. {
  229. Stack Tmp = new Stack();
  230. while ( !reverse.empty())
  231. {
  232. System.out.println("&&&&&&& "+ Tmp.peek());
  233. String x=(String)(reverse.peek());
  234. System.out.println(x);
  235.  
  236. if (Character.isDigit(x.charAt(0)))
  237. {
  238. Tmp.push(reverse.pop());
  239. }
  240. else
  241. {
  242. String sign=(String)(reverse.pop());
  243. if (sign.equals("+"))
  244. {
  245. double j=Double.parseDouble(((String)(Tmp.pop()+"")));
  246. double y=Double.parseDouble(((String)(Tmp.pop()+"")));
  247. double z;
  248. z= (y + j);
  249. Tmp.push(z);
  250. }
  251. else if ( sign.equals("-"))
  252. {
  253. double a=Double.parseDouble(((String)(Tmp.pop()+"")));
  254. double b=Double.parseDouble(((String)(Tmp.pop()+"")));
  255. double c;
  256. c= (b - a);
  257. Tmp.push(c);
  258. }
  259. else if (sign.equals("*"))
  260. {
  261. double first=Double.parseDouble(((String)(Tmp.pop()+"")));
  262. double two=Double.parseDouble(((String)(Tmp.pop()+"")));
  263. double three;
  264. three= (two * first);
  265. Tmp.push(three);
  266. }
  267. else if (sign.equals("/"))
  268. {
  269. double d=Double.parseDouble(((String)(Tmp.pop()+"")));
  270. double e=Double.parseDouble(((String)(Tmp.pop()+"")));
  271. double f;
  272. f= (e /d);
  273. Tmp.push(f);
  274. }
  275. else if (sign.equals("^"))
  276. {
  277. double g=Double.parseDouble(((String)(Tmp.pop()+"")));
  278. double h=Double.parseDouble(((String)(Tmp.pop()+"")));
  279. double i;
  280. i= Math.pow(h,g);
  281. Tmp.push(i);
  282. }
  283. }
  284. }
  285. Double o;
  286. o=Double.parseDouble(((String)(Tmp.pop()+"")));
  287. System.out.println("&&&&& " + o);
  288. return o;
  289. }
  290. public static void main(String[] args)
  291. {
  292. double result;
  293. String store = " ";
  294. Tokens app = new Tokens();
  295. String temp;
  296. int k=0;
  297. System.out.print("c> ");
  298. Scanner scan=new Scanner(System.in);
  299. temp = scan.next();
  300. if (temp.equals("p"))
  301. {
  302. while (scan.hasNext())
  303. {
  304. store+=scan.next();
  305. }
  306. }
  307. else if (temp.equals("parse"))
  308. {
  309. while (scan.hasNext() && !(scan.next().equals("q")))
  310. {
  311. store+=scan.next();
  312. }
  313. app.Parse(store);
  314. }
  315. else if (temp.equals("convert"))
  316. {
  317. app.convert(app.Tokenized);
  318. }
  319. }
  320. }
Attached Files
File Type: pdf a2.pdf (41.5 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Please Help Stuck Need Help Asap!!!

 
0
  #2
Mar 26th, 2007
>Please Help Stuck Need Help Asap!!!

1)Just because you are in a hurry makes no difference to us. You should have thought about that then perhaps you wouldn't have missed your deadline (23 March).:eek:

2)What I also found amusing is how your school cites wiki as a reference. I mean come on, use something more reliable for pity sake!:lol:

3) Have you even attempted to debug it. I.e add watches to find out where it is going wrong?

4) A complete answer albeit in c++ was provided earlier by me. Why you decided to start another thread, I don't know. :rolleyes:
http://www.daniweb.com/techtalkforums/thread73200.html

If you have managed to separate your expression into tokens successfully then all you need to convert that expression into postfix is the following:-

  1. bool Equation::TakesPrecedence(char OperatorA, char OperatorB)
  2. {
  3. if (OperatorA == '(')
  4. return false;
  5. else if (OperatorB == '(')
  6. return false;
  7. else if (OperatorB == ')')
  8. return true;
  9. else if ((OperatorA == '^') && (OperatorB == '^'))
  10. return false;
  11. else if (OperatorA == '^')
  12. return true;
  13. else if (OperatorB == '^')
  14. return false;
  15. else if ((OperatorA == '*') || (OperatorA == '/'))
  16. return true;
  17. else if ((OperatorB == '*') || (OperatorB == '/'))
  18. return false;
  19. else
  20. return true;
  21. }
  22.  
  23. void Equation::Convert(const string & Infix, string & Postfix)
  24. {
  25. stack <char> OperatorStack;
  26. char TopSymbol, Symbol;
  27. int k;
  28.  
  29. for (k = 0; k < Infix.size(); k++)
  30. {
  31. Symbol = Infix[k];
  32. if (IsOperand(Symbol))
  33. Postfix = Postfix + Symbol;
  34. else
  35. {
  36. while ((! OperatorStack.empty()) &&
  37. (TakesPrecedence(OperatorStack.top(), Symbol)))
  38. {
  39. TopSymbol = OperatorStack.top();
  40. OperatorStack.pop();
  41. Postfix = Postfix + TopSymbol;
  42. }
  43. if ((! OperatorStack.empty()) && (Symbol == ')'))
  44. OperatorStack.pop(); // discard matching (
  45. else
  46. OperatorStack.push(Symbol);
  47. }
  48. }
  49.  
  50. while (! OperatorStack.empty())
  51. {
  52. TopSymbol = OperatorStack.top();
  53. OperatorStack.pop();
  54. Postfix = Postfix + TopSymbol;
  55. }
  56. }

Your teacher has kindly omitted the need for unary expressions. If you're smart you'll look again at my code.

Evaluating the postfix notation is a piece of cake.
Last edited by iamthwee; Mar 26th, 2007 at 5:03 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 47
Reputation: nnobakht is an unknown quantity at this point 
Solved Threads: 1
nnobakht nnobakht is offline Offline
Light Poster

Re: Please Help Stuck Need Help Asap!!!

 
0
  #3
Mar 26th, 2007
the deadline is not really march 23rd i didnt miss it. i have already done from infix to postfix that is the convert function created. what i am having problem with is constructing my main. i cannot use the debugger since i cant construct the main properly so it runs.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Please Help Stuck Need Help Asap!!!

 
0
  #4
Mar 26th, 2007
Well surely, if you have done infix to postfix, the actual evaluation is a walk in the park.

See example

http://www.daniweb.com/techtalkforums/thread73200.html
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Please Help Stuck Need Help Asap!!!

 
0
  #5
Mar 26th, 2007
>i cannot use the debugger since i cant construct the main properly so it runs.

That's basic class design. You should have notes about creating a constructor etc... Anyway, I'm off to bed.
Last edited by iamthwee; Mar 26th, 2007 at 5:26 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC