944,184 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3442
  • C++ RSS
Feb 22nd, 2005
0

Mathematics In A Stack

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alsoagirl is offline Offline
3 posts
since Feb 2005
Feb 22nd, 2005
0

Re: Mathematics In A Stack

Take a look at http://www.calculator.org/rpn.html, I think this will help you fill in the gaps. Good luck!
Reputation Points: 10
Solved Threads: 3
Light Poster
mcldev is offline Offline
31 posts
since Feb 2005
Feb 23rd, 2005
0

Re: Mathematics In A Stack

Also, next time you post code use the [code] tag to make it easier to read and get rid of the smiles.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Khishin is offline Offline
23 posts
since Jan 2005
Feb 23rd, 2005
0

Re: Mathematics In A Stack

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alsoagirl is offline Offline
3 posts
since Feb 2005

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 C++ Forum Timeline: question
Next Thread in C++ Forum Timeline: Beginning: Need Help on an Issue.





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


Follow us on Twitter


© 2011 DaniWeb® LLC