944,132 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 602
  • C++ RSS
Oct 26th, 2009
0

C++ postfix to infix help!

Expand Post »
Hello, I need some advice to help me write a program to convert postfix to infix. However, I am having difficulty getting started. I tried to search for more information, but all I got was infix to postfix results. This is what I have so far, but I want to make sure I am on the right track. Any help is appreciated.


main.cpp


C++ Syntax (Toggle Plain Text)
  1. stackType<string>s1;
  2. char letter;
  3.  
  4. ifstream inData("input.txt"); // declaration of inData
  5. ofstream outData("output.txt"); // declaration of outData
  6.  
  7. while (getline( inData, letter ))
  8. {
  9.  
  10. if (letter == 'A'|| letter == 'B' || letter == 'C' || letter == 'D' || letter == 'E')
  11.  
  12. {
  13.  
  14. s1.push();
  15.  
  16. }
  17.  
  18. if (letter == '*'|| letter == '-' || letter == '+' || letter == '/')
  19.  
  20. {
  21.  
  22. s1.pop();
  23.  
  24. }
  25.  
  26.  
  27. inData.close(); // closes inData
  28. outData.close(); // closes outData
  29.  
  30. system("pause");
  31. return 0;
  32.  
  33. }





header file:

C++ Syntax (Toggle Plain Text)
  1. #ifndef H_StackType
  2. #define H_StackType
  3.  
  4. #include <iostream>
  5. #include <cassert>
  6.  
  7. using namespace std;
  8.  
  9. template<class Type>
  10.  
  11. class stackType
  12. {
  13.  
  14. public:
  15. const stackType<Type>& operator=(const stackType<Type>&);
  16. void initializeStack();
  17. bool isEmptyStack();
  18. bool isFullStack();
  19. void destroyStack();
  20. void push(const Type& newItem);
  21. Type top();
  22. void pop();
  23. stackType(int stackSize = 100);
  24. stackType(const stackType<Type>& otherStack);
  25. ~stackType();
  26. bool operator== (const stackType<Type>&);
  27. bool operator!= (const stackType<Type>&);
  28.  
  29.  
  30. private:
  31. int maxStackSize;
  32. int stackTop;
  33. Type *list;
  34. void copyStack(const stackType<Type>& otherStack);
  35. bool isEqual(const stackType<Type>&);
  36.  
  37. };
  38.  
  39. template<class Type>
  40. void stackType<Type>::copyStack(const stackType<Type>& otherStack)
  41. {
  42. delete [] list;
  43. maxStackSize = otherStack.maxStackSize;
  44. stackTop = otherStack.stackTop;
  45.  
  46. list = new Type[maxStackSize];
  47. assert(list != NULL);
  48.  
  49. for(int j = 0; j < stackTop; j++)
  50. list[j] = otherStack.list[j];
  51.  
  52. }
  53.  
  54. template<class Type>
  55. stackType<Type>::stackType(const stackType<Type>& otherStack)
  56. {
  57. list = NULL;
  58.  
  59. copyStack(otherStack);
  60.  
  61. }
  62.  
  63. template<class Type>
  64. const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
  65. {
  66. if (this != &otherStack)
  67. copyStack(otherStack);
  68.  
  69. return *this;
  70. }
  71.  
  72. template<class Type>
  73. void stackType<Type>::initializeStack()
  74. {
  75. stackTop = 0;
  76. }
  77.  
  78. template<class Type>
  79. void stackType<Type>::destroyStack()
  80. {
  81. stackTop = 0;
  82. }
  83.  
  84. template<class Type>
  85. bool stackType<Type>::isEmptyStack()
  86. {
  87. return(stackTop == maxStackSize);
  88. }
  89.  
  90. template<class Type>
  91. bool stackType<Type>::isFullStack()
  92. {
  93. return(stackTop == maxStackSize);
  94. }
  95.  
  96. template<class Type>
  97. void stackType<Type>::push(const Type& newItem)
  98. {
  99. if(!isFullStack())
  100. {
  101. list[stackTop] = newItem;
  102.  
  103. stackTop++;
  104. }
  105.  
  106. else
  107. cerr<<"Cannot add to a full stack."<<endl;
  108. }
  109.  
  110. template<class Type>
  111. Type stackType<Type>::top()
  112. {
  113. assert(stackTop != 0);
  114.  
  115. return list[stackTop - 1];
  116. }
  117.  
  118. template<class Type>
  119. void stackType<Type>::pop()
  120. {
  121. if(!isEmptyStack())
  122. {
  123. stackTop--;
  124. }
  125. else
  126. cerr<<"Cannot remove from an empty stack."<<endl;
  127. }
  128.  
  129. template<class Type>
  130. stackType<Type>::stackType(int stackSize)
  131. {
  132. if(stackSize <= 0)
  133. {
  134. cerr<<"The size of the array to hold the stack must "
  135. <<"be positive."<<endl;
  136. cerr<<"Creating an array of size 100."<<endl;
  137.  
  138. maxStackSize = 100;
  139. }
  140.  
  141. else
  142. maxStackSize = stackSize;
  143.  
  144. stackTop = 0;
  145.  
  146. list = new Type[maxStackSize];
  147. }
  148.  
  149. template<class Type>
  150. stackType<Type>::~stackType()
  151. {
  152. delete [] list;
  153. }
  154.  
  155. //template<class Type>
  156. //bool stackType<Type>::operator== (const stackType<Type>& otherStack)
  157. //{
  158.  
  159. //if (otherStack.isFullStack() != isFullStack())
  160.  
  161. //cerr<<"They are not the same size"<<endl;
  162.  
  163. //for(int j = 0 ; j < isFullStack(); ++j)
  164. //{
  165. //if (otherStack.list[j] != list[j])
  166.  
  167. //return false;
  168.  
  169. //else
  170.  
  171. //return true;
  172. //}
  173.  
  174. //}
  175.  
  176. //template<class Type>
  177. //bool stackType<Type>::operator!= (const stackType<Type>& otherStack)
  178. //{
  179.  
  180. //return !(*this == otherStack);
  181.  
  182. //}
  183.  
  184.  
  185.  
  186. template<class Type>
  187. bool stackType<Type>::isEqual(const stackType<Type>& otherStack) {
  188. bool bRet = false;
  189. if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop){
  190. bRet = true;
  191. for (int j = 0; j < stackTop; ++j) {
  192. if (otherStack.list[j] != list[j]) {
  193. // cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
  194. bRet = false;
  195. break;
  196. }
  197. }
  198. }
  199. return bRet;
  200. }
  201.  
  202. template<class Type>
  203. bool stackType<Type>::operator==(const stackType<Type>& otherStack) {
  204. return isEqual(otherStack);
  205. }
  206.  
  207. template<class Type>
  208. bool stackType<Type>::operator!=(const stackType<Type>& otherStack) {
  209. return !isEqual(otherStack); //!(*this == otherStack);
  210. }
Last edited by NinjaLink; Oct 26th, 2009 at 4:03 am.
Similar Threads
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Oct 26th, 2009
0

Re: C++ postfix to infix help!

C++ Syntax (Toggle Plain Text)
  1. //TO READ POSTFIX POLISH NOTATION FROM FILE
  2. void readFile(ifstream& read)
  3. {
  4. char in = '0';
  5.  
  6. while (in != '\n')
  7. {
  8. read.get(in);
  9.  
  10. if ((in >='0') && (in <='9'))
  11. {
  12. ExprNode<char> EN(in);
  13. LS.Push(EN);
  14. }
  15. else
  16. {
  17. ExprTree<char> ET;
  18. ExprNode<char> EN1 = LS.Pop();
  19. ExprNode<char> EN2 = LS.Pop();
  20. ExprNode<char> EN(in);
  21.  
  22. ET.Insert(EN.getData());
  23. ET.Insert(EN2.getData(), EN2.getLeft(), EN2.getRight());
  24. ET.Insert(EN1.getData(), EN1.getLeft(), EN1.getRight());
  25.  
  26. LS.Push(ET.getRoot());
  27. }
  28. read.get(in);
  29. }
  30. }
  31.  
  32.  
  33.  
  34. //TO PRINT INFIX EXPRESSION
  35. void InOrder(ExprNode<T> *node, ofstream& write)
  36. {
  37. if (node != NULL)
  38. {
  39. if (node->left && node->right)
  40. {
  41. write<<"(";
  42. }
  43. InOrder(node->left, write);
  44. write<<node->data;
  45. InOrder(node->right, write);
  46. if (node->left && node->right)
  47. {
  48. write<<")";
  49. }
  50. }
  51. }
Last edited by tsfaridi; Oct 26th, 2009 at 5:33 am. Reason: was not in code snippet
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tsfaridi is offline Offline
7 posts
since Mar 2009

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: point inside triangle any situations where my code will fail?
Next Thread in C++ Forum Timeline: BigInt addition: Is this correct?





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


Follow us on Twitter


© 2011 DaniWeb® LLC