Mathematics In A Stack

Reply

Join Date: Feb 2005
Posts: 3
Reputation: alsoagirl is an unknown quantity at this point 
Solved Threads: 0
alsoagirl alsoagirl is offline Offline
Newbie Poster

Mathematics In A Stack

 
0
  #1
Feb 22nd, 2005
I have read in problems "INFIX" AND CHANGED THEM TO "POSTFIX" or RPN notation, however i need to evaluate the problems and i can't get this to work. I am not sure where to go from the infix to postifix i have. Please help. My Stack.h is just a basic Stack defined. I am not sure what to do next. Any ideas?

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cassert>
  5. #include <stdlib.h>
  6. #include <cstring>
  7.  
  8. using namespace std;
  9. #include "Stack.h"
  10.  
  11. struct temp1
  12. {
  13. char line[30];
  14. };
  15.  
  16. string RPN(string exp);
  17.  
  18. void main()
  19. {
  20. string exp;
  21. int count= 0;
  22. int result;
  23. temp1 temp[128];
  24.  
  25.  
  26. ifstream File;
  27. File.open("a2.txt");
  28. if (!File.is_open())
  29. {
  30. cout << "File not found!!!!";
  31. exit(1);
  32. }
  33.  
  34. while(File.peek() != EOF)
  35. {
  36. cout << "\nThe Infix Expression is: \n" << endl;
  37. File.getline(temp[count].line, 30);
  38. cout << temp[count].line << endl;
  39. for (;;)
  40. {
  41. cout << "\nThe Postfix Expression is: \n";
  42. exp = temp[count].line;
  43. cout << RPN(exp) << endl;
  44. break;
  45.  
  46. }
  47.  
  48. cout << "\nThe Evaluated Expression is:\n";
  49. cout << RPN(exp) << endl;
  50.  
  51. cout << "\nThe Answer is:\n" << endl;
  52. // result = -exp;
  53. // cout << result << "\n";
  54.  
  55. count++;
  56. }
  57. }
  58.  
  59.  
  60. string RPN(string exp)
  61. {
  62. char token,
  63. topToken;
  64. Stack opStack;
  65. string RPNexp;
  66. const string BLANK = " ";
  67.  
  68. for (int i= 0; i < exp.length(); i++)
  69. {
  70. token = exp[i];
  71. switch(token)
  72. {
  73. case ' ' : break;
  74.  
  75. case '(' : opStack.push(token);
  76. break;
  77.  
  78. case ')' : for (;;)
  79. {
  80. assert (!opStack.empty());
  81. topToken = opStack.top();
  82. opStack.pop();
  83. if (topToken == '(') break;
  84.  
  85. RPNexp.append(BLANK + topToken);
  86. }
  87. break;
  88.  
  89. case '+' : case '-' :
  90. case '*' : case '/' :
  91. for (;;)
  92. {
  93. if (opStack.empty() ||
  94. opStack.top() == '(' ||
  95. (token == '*' || token == '/') &&
  96. (opStack.top() == '+' || opStack.top() == '-')
  97. )
  98.  
  99. {
  100. opStack.push(token);
  101. break;
  102. }
  103. else
  104. {
  105. topToken = opStack.top();
  106. opStack.pop();
  107. RPNexp.append(BLANK + topToken);
  108. }
  109. }
  110. break;
  111.  
  112. default : RPNexp.append(BLANK + token);
  113. }
  114. }
  115.  
  116. for(;;)
  117. {
  118. if (opStack.empty()) break;
  119. topToken = opStack.top();
  120. opStack.pop();
  121. if (topToken != '(')
  122. {
  123. RPNexp.append(BLANK + topToken);
  124. }
  125. else
  126. {
  127. cout << " Error in Infix Expressions\n";
  128. break;
  129. }
  130. }
  131. return RPNexp;
  132.  
  133. }
  134.  
  135. void Stack::push(const StackElement & value)
  136. {
  137. if (myTop < STACK_CAPACITY - 1)
  138. {
  139. ++myTop;
  140. myArray[myTop] = value;
  141. }
  142. }
  143.  
  144. void Stack::display(ostream & out) const
  145. {
  146. for (int i = myTop; i >= 0; i--)
  147. out << myArray[i] << endl;
  148. }
  149.  
  150. StackElement Stack::top() const
  151. {
  152. if (myTop >= 0)
  153. return myArray[myTop];
  154. cout << "--- Stack is Empty----\n";
  155. }
  156.  
  157. void Stack::pop()
  158. {
  159. if (myTop >= 0)
  160. myTop--;
  161. else
  162. cout << "Stack is empty can't remove a value\n";
  163. }
Last edited by alc6379; Feb 23rd, 2005 at 6:22 pm. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 31
Reputation: mcldev is an unknown quantity at this point 
Solved Threads: 2
mcldev mcldev is offline Offline
Light Poster

Re: Mathematics In A Stack

 
0
  #2
Feb 22nd, 2005
Take a look at http://www.calculator.org/rpn.html, I think this will help you fill in the gaps. Good luck!
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 23
Reputation: Khishin is an unknown quantity at this point 
Solved Threads: 0
Khishin Khishin is offline Offline
Newbie Poster

Re: Mathematics In A Stack

 
0
  #3
Feb 23rd, 2005
Also, next time you post code use the [code] tag to make it easier to read and get rid of the smiles.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 3
Reputation: alsoagirl is an unknown quantity at this point 
Solved Threads: 0
alsoagirl alsoagirl is offline Offline
Newbie Poster

Re: Mathematics In A Stack

 
0
  #4
Feb 23rd, 2005
okay so i tried the calculator i know how it works, push's pops etc don't know how to work in actually evaluating the expression. Please help Thanks for everything so far
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 C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC