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
30 Days Ago
Hi. What I want to do is convert a postfix expression into a infix expression. I am reading the postfix expression from an input file and I want to print the infix expression to an output file. I basically just need help setting up my function, so it will work. Please help if you can. Thank you


Input file:

ABC+-D-E-



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.  
  10. bool isOperand(string expr);
  11. bool isOperator(string expr);
  12. string PostfixToInfix(string expr, stackType<string> stack);
  13.  
  14.  
  15.  
  16. int main()
  17. {
  18.  
  19. stringstream ss;
  20. stackType<string> stack;
  21. string Infix;
  22. string line;
  23. string word;
  24. string expr;
  25.  
  26.  
  27. ifstream inData("input.txt");
  28. ofstream outData("output.txt");
  29.  
  30. while (getline( inData, line ))
  31. {
  32. ss << line;
  33.  
  34. }
  35. while ( ss >> word)
  36. {
  37. stack.push(word);
  38. }
  39.  
  40. outData<<line;
  41.  
  42.  
  43.  
  44. inData.close();
  45. outData.close();
  46.  
  47. system("PAUSE");
  48. return 0;
  49. }
  50.  
  51.  
  52.  
  53. bool isOperand(string expr)
  54. {
  55. return(!isOperator(expr)? true : false);
  56. }
  57.  
  58. bool isOperator(string expr)
  59. {
  60. return((expr=="+" || expr=="-" || expr=="*" || expr=="/" || expr=="(" || expr==")")? true : false);
  61. }
  62.  
  63. string PostfixToInfix(string expr,stackType<string> stack)
  64. {
  65.  
  66. for(int i=0; i<expr.length(); i++)
  67. {
  68. if(isOperand(expr[i]))
  69. {
  70. stack.push(expr.length()[i]);
  71. }
  72. else
  73. {
  74. string temp = stack.top();
  75. stack.pop();
  76. int stack.push()=stack.top()+expr.length()[i]+temp;
  77. stack.pop();
  78. stack.push();
  79. }
  80. }
  81. return(stack.top());
  82. }




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.  
  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. #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