Need help with a function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Need help with a function

 
0
  #1
27 Days Ago
I need help with my function that is in my stack.h file which converts postfix expression to infix. My main.cpp reads the postfix expression from a file and outputs the expression to an output file. My main.cpp is currently working. I just need someone to help me with my toInfix function at the bottom of my stack.h. I tried to look for more information, but all I can find is Infix to Postfix. I appreciate if someone can lead me to the right direction.


main.cpp

  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4. #include "stack.h"
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.  
  12. stringstream ss;
  13. stackType<string> stack;
  14. string Infix;
  15. string line;
  16. string word;
  17.  
  18.  
  19. ifstream inData("input.txt");
  20. ofstream outData("output.txt");
  21.  
  22. while (getline( inData, line ))
  23. {
  24. ss << line;
  25.  
  26. }
  27. while ( ss >> word)
  28. {
  29. stack.push(word);
  30. }
  31.  
  32. outData<<line;
  33.  
  34.  
  35.  
  36. inData.close();
  37. outData.close();
  38.  
  39. system("PAUSE");
  40. return 0;
  41. }






stack.h

  1. #ifndef H_StackType
  2. #define H_StackType
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <cassert>
  7.  
  8. using namespace std;
  9.  
  10. template<class Type>
  11.  
  12. class stackType
  13. {
  14.  
  15. public:
  16. const stackType<Type>& operator=(const stackType<Type>&);
  17. void initializeStack();
  18. bool isEmptyStack();
  19. bool isFullStack();
  20. void destroyStack();
  21. void push(const Type& newItem);
  22. Type top();
  23. void pop();
  24. stackType(int stackSize = 100);
  25. stackType(const stackType<Type>& otherStack);
  26. ~stackType();
  27. bool operator== (const stackType<Type>&);
  28. bool operator!= (const stackType<Type>&);
  29.  
  30.  
  31.  
  32. private:
  33. int maxStackSize;
  34. int stackTop;
  35. Type *list;
  36. void copyStack(const stackType<Type>& otherStack);
  37. bool isEqual(const stackType<Type>&);
  38. void toInfix(const stackType<Type>&);
  39.  
  40. };
  41.  
  42. template<class Type>
  43. void stackType<Type>::copyStack(const stackType<Type>& otherStack)
  44. {
  45. delete [] list;
  46. maxStackSize = otherStack.maxStackSize;
  47. stackTop = otherStack.stackTop;
  48.  
  49. list = new Type[maxStackSize];
  50. assert(list != NULL);
  51.  
  52. for(int j = 0; j < stackTop; j++)
  53. list[j] = otherStack.list[j];
  54.  
  55. }
  56.  
  57. template<class Type>
  58. stackType<Type>::stackType(const stackType<Type>& otherStack)
  59. {
  60. list = NULL;
  61.  
  62. copyStack(otherStack);
  63.  
  64. }
  65.  
  66. template<class Type>
  67. const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
  68. {
  69. if (this != &otherStack)
  70. copyStack(otherStack);
  71.  
  72. return *this;
  73. }
  74.  
  75. template<class Type>
  76. void stackType<Type>::initializeStack()
  77. {
  78. stackTop = 0;
  79. }
  80.  
  81. template<class Type>
  82. void stackType<Type>::destroyStack()
  83. {
  84. stackTop = 0;
  85. }
  86.  
  87. template<class Type>
  88. bool stackType<Type>::isEmptyStack()
  89. {
  90. return(stackTop == 0);
  91. }
  92. template<class Type>
  93. bool stackType<Type>::isFullStack()
  94. {
  95. return(stackTop == maxStackSize);
  96. }
  97.  
  98. template<class Type>
  99. void stackType<Type>::push(const Type& newItem)
  100. {
  101. if(!isFullStack())
  102. {
  103. list[stackTop] = newItem;
  104.  
  105. stackTop++;
  106. }
  107.  
  108. else
  109. cerr<<"Cannot add to a full stack."<<endl;
  110. }
  111.  
  112. template<class Type>
  113. Type stackType<Type>::top()
  114. {
  115. assert(stackTop != 0);
  116.  
  117. return list[stackTop - 1];
  118. }
  119.  
  120. template<class Type>
  121. void stackType<Type>::pop()
  122. {
  123. if(!isEmptyStack())
  124. {
  125. stackTop--;
  126. }
  127. else
  128. cerr<<"Cannot remove from an empty stack."<<endl;
  129. }
  130.  
  131. template<class Type>
  132. stackType<Type>::stackType(int stackSize)
  133. {
  134. if(stackSize <= 0)
  135. {
  136. cerr<<"The size of the array to hold the stack must "
  137. <<"be positive."<<endl;
  138. cerr<<"Creating an array of size 100."<<endl;
  139.  
  140. maxStackSize = 100;
  141. }
  142.  
  143. else
  144. maxStackSize = stackSize;
  145.  
  146. stackTop = 0;
  147.  
  148. list = new Type[maxStackSize];
  149. }
  150.  
  151. template<class Type>
  152. stackType<Type>::~stackType()
  153. {
  154. delete [] list;
  155. }
  156.  
  157.  
  158.  
  159. template<class Type>
  160. bool stackType<Type>::isEqual(const stackType<Type>& otherStack) {
  161. bool bRet = false;
  162. if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop){
  163. bRet = true;
  164. for (int j = 0; j < stackTop; ++j) {
  165. if (otherStack.list[j] != list[j]) {
  166. // cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
  167. bRet = false;
  168. break;
  169. }
  170. }
  171. }
  172. return bRet;
  173. }
  174.  
  175. template<class Type>
  176. bool stackType<Type>::operator==(const stackType<Type>& otherStack) {
  177. return isEqual(otherStack);
  178. }
  179.  
  180. template<class Type>
  181. bool stackType<Type>::operator!=(const stackType<Type>& otherStack) {
  182. return !isEqual(otherStack); //!(*this == otherStack);
  183. }
  184.  
  185.  
  186.  
  187. void stackType<Type>::toInfix(const stackType<Type>& otherStack)
  188. {
  189.  
  190. string Infix;
  191. string isDigit;
  192. string again;
  193. int number;
  194. string word;
  195. string line;
  196. string here;
  197.  
  198. for (int a=0; a < line.length(); a++)
  199. {
  200.  
  201. number = word = expression[a];
  202.  
  203.  
  204. if ( line[a] == '+' || line[a] == '-')
  205. {
  206. if (priority <= 2 )
  207. {
  208. word.pop(); word.push();
  209. }
  210. else
  211. {
  212. word.push();
  213. }
  214. }
  215.  
  216. else if ( line[a] == '*' || line[a] == '/' || line[a] == '%' )
  217. {
  218. if (priority == 1 )
  219. {
  220. word=stack[0]; word[0]=line[a]; Infix = Infix + word;
  221. }
  222. else
  223. {
  224. word.push;
  225. }
  226. }
  227.  
  228. else if ( line[a] == '(' )
  229. {
  230. word.push();
  231. }
  232.  
  233. else if ( line[a] == ')' )
  234. {
  235. here.pop();
  236. }
  237. else if ( isdigit(again[a]) )
  238. {
  239. if ( isdigit(again[a+1]) )
  240. {
  241. int b=a+1;
  242. while ( isdigit(again[b]) )
  243. {
  244. hold=again[b];
  245. number = number + word;
  246. ++b;
  247. }
  248. infix = infix + ' ' + number;
  249. a=b-1;
  250. }
  251. else
  252. {
  253. infix = infix + ' ' + number;
  254. }
  255.  
  256. }
  257. }
  258.  
  259. Infix = Infix + ' ' + stack;
  260.  
  261. }
  262.  
  263. #endif
Reply With Quote Quick reply to this message  
Reply

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