stack/queue program for long math operation - need help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 17
Reputation: JAGgededgeOB172 is an unknown quantity at this point 
Solved Threads: 0
JAGgededgeOB172 JAGgededgeOB172 is offline Offline
Newbie Poster

stack/queue program for long math operation - need help

 
0
  #1
Feb 8th, 2009
Hi all,
The program I'm making needs to basically interpret long math operations (see the attached text file) and compute them. Simple right?

I have to use stacks and queues to reorder the operation into the desired output (the output will compile properly if you run the program as it is now). I'm obviously trying to compute a final answer but the final answer is not correct, I'm thinking I'm having issues converting my char variables into float variables. For better convenience my problem I'm sure lies in my EVALUATE_POSTFIX function.

A side note: I have to read in multiple lines of math operations from the text file ultimately, but for now I'm only having the program interpret one line, just for better troubleshooting.

The purpose of all the reordering is for faster computation - as instructed from my college class.

Thank you for your time!

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <stack>
  5. #include <string>
  6. #include <queue>
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <cstring>
  10.  
  11. using namespace std;
  12.  
  13. queue<char> INFIX_TO_POSTFIX(ifstream &);
  14. void EVALUATE_POSTFIX(queue<char> &);
  15. int precedence(char);
  16.  
  17. int main()
  18. {
  19. ifstream inFile;
  20. inFile.open("a2.txt"); //attempts to open read file, and tests for existance
  21. if(inFile.is_open())
  22. cout << "File Exists." << endl << endl;
  23. else
  24. cout << "File does not exist." << endl << endl;
  25.  
  26. queue<char> bigqueue;
  27.  
  28. //while(!inFile.eof())
  29. // {
  30. bigqueue = INFIX_TO_POSTFIX(inFile); //Function call
  31. // }
  32. EVALUATE_POSTFIX(bigqueue);
  33.  
  34. // float tool = 7 + (4 - 6) * (8 + 6) / 3;
  35. // cout << endl << tool << endl;
  36.  
  37. system("PAUSE");
  38. return EXIT_SUCCESS;
  39. }
  40.  
  41. queue<char> INFIX_TO_POSTFIX(ifstream &Filein) //function, Filein is just the name of my text file read stream
  42. {
  43.  
  44. stack<char> thestack;
  45. queue<char> thequeue;
  46.  
  47. string data; //string created to store the input read line
  48. getline(Filein, data); //program reads file, stores read line as the string, data
  49. cout << "The original infix expression is: " << data << endl; //output read string
  50. char dataarray[25] = {0}; //char array created
  51. int arrayLength = data.length(); //set a counter
  52. char temp; //temporary variable
  53.  
  54. for(int i = 0; i < arrayLength; i++) //for loop set up to step through the character array, one character at a time
  55. {
  56. dataarray[i] = data[i]; //Here's the key line, I equate each position in the string to equate to the character array
  57.  
  58. if(isdigit(dataarray[i])) //Here on out is where the processing of the character array begins....
  59. thequeue.push (dataarray[i]);//output content(Next) to Queue;
  60.  
  61. else if(dataarray[i] == '(')
  62. thestack.push (dataarray[i]);
  63.  
  64. else if(dataarray[i] == ')')
  65. {
  66. if(thestack.empty() == false)
  67. {
  68. while(thestack.top() != '(')
  69. {
  70. temp = thestack.top();
  71. thequeue.push(temp);
  72. thestack.pop();
  73. if(thestack.empty() == true)
  74. {
  75. break;
  76. } //POP the content in Stack to Queue until “(“ is reached,
  77. }
  78. }
  79. thestack.pop(); //POP it but not to Queue; break;
  80. }
  81.  
  82. else if(dataarray[i] == '+' || '-' || '*' || '/' || '^')
  83. {
  84. if(thestack.empty() == false)
  85. {
  86. while(precedence(thestack.top()) >= precedence(dataarray[i])) //while(PRE(TOP) >= PRE(Next))
  87. {
  88. temp = thestack.top();
  89. thequeue.push(temp);
  90. thestack.pop();
  91. if(thestack.empty() == true)
  92. {
  93. break;
  94. }
  95. }
  96. }
  97. thestack.push(dataarray[i]);
  98. }
  99.  
  100. else
  101. cout << "There was an error in the operation, please restart." << endl;
  102. }
  103.  
  104. while(!thestack.empty()) //while(PRE(TOP) >= PRE(Next))
  105. {
  106. temp = thestack.top();
  107. thequeue.push(temp);
  108. thestack.pop();
  109. }
  110. return(thequeue);
  111. }
  112.  
  113. int precedence(char thing)
  114. {
  115. int compare;
  116. switch(thing)
  117. {
  118. case '^': compare = 3;
  119. break;
  120. case '/':
  121. case '*': compare = 2;
  122. break;
  123. case '+':
  124. case '-': compare = 1;
  125. break;
  126. case '(':
  127. case ')': compare = 0;
  128. break;
  129. }
  130. return (compare);
  131. }
  132.  
  133. void EVALUATE_POSTFIX(queue<char> &postqueue)
  134. {
  135. stack<float> poststack;
  136. int queueLength;
  137. float final;
  138. char var, temp, temp1, temp2;
  139. queueLength = postqueue.size();
  140.  
  141. for(int j = 0; j < queueLength; j++)
  142. {
  143. cout << postqueue.front();
  144. if(isdigit(postqueue.front()))
  145. {
  146. var = postqueue.front();
  147. poststack.push(var);
  148. }
  149.  
  150. else if(postqueue.front() == '+' || '-' || '*' || '/' || '^')
  151. {
  152. temp2 = poststack.top();
  153. poststack.pop();
  154. temp1 = poststack.top();
  155. poststack.pop();
  156. switch(postqueue.front())
  157. {
  158. case '+': temp = temp1 + temp2;
  159. break;
  160. case '-': temp = temp1 - temp2;
  161. break;
  162. case '*': temp = temp1 * temp2;
  163. break;
  164. case '/': temp = temp1 / temp2;
  165. break;
  166. case '^': temp = temp1 ^ temp2;
  167. break;
  168. default: cout << "There was a glitch in the operation, press any key to terminate.";
  169. }
  170. poststack.push(temp);
  171. }
  172. postqueue.pop();
  173. }
  174. final = poststack.top();
  175. cout << endl << "The postfix operation answer is: " << poststack.top() << endl;
  176. return;
  177. }
Attached Files
File Type: txt a2.txt (69 Bytes, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 332
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 64
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: stack/queue program for long math operation - need help

 
0
  #2
Feb 8th, 2009
1+2*3 and (1+2)*3. What's the output..
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 17
Reputation: JAGgededgeOB172 is an unknown quantity at this point 
Solved Threads: 0
JAGgededgeOB172 JAGgededgeOB172 is offline Offline
Newbie Poster

Re: stack/queue program for long math operation - need help

 
0
  #3
Feb 8th, 2009
Originally Posted by cikara21 View Post
1+2*3 and (1+2)*3. What's the output..
Well that would be 7 and 9, but how is this helping me?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 17
Reputation: JAGgededgeOB172 is an unknown quantity at this point 
Solved Threads: 0
JAGgededgeOB172 JAGgededgeOB172 is offline Offline
Newbie Poster

Re: stack/queue program for long math operation - need help

 
0
  #4
Feb 8th, 2009
Ok, I've figured out that my variables are equating to ASCII equivalents, I'm having trouble converting them, I don't understand why they're not converting when I convert them...

  1. void EVALUATE_POSTFIX(queue<char> &postqueue)
  2. {
  3. stack<float> poststack;
  4. int queueLength;
  5. char var;
  6. float var1, temp, temp1, temp2, final;
  7. queueLength = postqueue.size();
  8.  
  9. for(int j = 0; j < queueLength; j++)
  10. {
  11. cout << postqueue.front();
  12. if(isdigit(postqueue.front()))
  13. {
  14. var = postqueue.front();
  15. var1 = static_cast<float>(var);
  16. poststack.push(var1);
  17. }
  18.  
  19. else if(postqueue.front() == '+' || '-' || '*' || '/' || '^')
  20. {
  21. temp2 = poststack.top();
  22. poststack.pop();
  23. temp1 = poststack.top();
  24. poststack.pop();
  25. switch(postqueue.front())
  26. {
  27. case '+': temp = temp1 + temp2;
  28. break;
  29. case '-': temp = temp1 - temp2;
  30. break;
  31. case '*': temp = temp1 * temp2;
  32. break;
  33. case '/': temp = temp1 / temp2;
  34. break;
  35. case '^': temp = pow(temp1, temp2);
  36. break;
  37. default: cout << "There was a glitch in the operation, press any key to terminate.";
  38. }
  39.  
  40. poststack.push(temp);
  41. }
  42. postqueue.pop();
  43. }
  44. final = poststack.top();
  45. cout << endl << "The postfix operation answer is: " << final << endl;
  46. return;
  47. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 332
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 64
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: stack/queue program for long math operation - need help

 
0
  #5
Feb 9th, 2009
->#3
That's correct..Where's the problem..
convert to char* then use atof to convert to float..
Last edited by cikara21; Feb 9th, 2009 at 1:34 am.
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC