converting postfix to Infix help

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

converting postfix to Infix help

 
0
  #1
32 Days Ago
I need help trying to convert a postfix expression to Infix. I have tried to search books and internet material to help me take on this task but no luck. Below is what I created, but I know it is far from right which is why I appreciate if someone can help me.


Targeted Function:


  1. void toInfix(string expression)
  2. {
  3.  
  4. stackType<string> stack,here;
  5. string Infix;
  6. string priority;
  7. string number, hold, again;
  8.  
  9. again=expression;
  10.  
  11. ifstream inData("input.txt");
  12. ofstream outData("output.txt");
  13.  
  14. while (getline( inData, expression ))
  15. {
  16.  
  17. cout << expression;
  18.  
  19. }
  20.  
  21. for (int a=0; a < expression.length(); a++)
  22. {
  23.  
  24. number = here = expression[a];
  25. cout << "The expression " << here << endl;
  26. cout << "The Infix " << Infix << endl;
  27.  
  28.  
  29. if ( expression[a] == '+' || expression[a] == '-')
  30. {
  31. if (priority <= 2 )
  32. {
  33. here.pop(); here.push();
  34. }
  35. else
  36. {
  37. here.push();
  38. }
  39. }
  40.  
  41. else if ( expression[a] == '*' || expression[a] == '/' || expression[a] == '%' )
  42. {
  43. if (priority == 1 )
  44. {
  45. here=stack[0]; stack[0]=expression[a]; Infix = Infix + here;
  46. }
  47. else
  48. {
  49. here.push;
  50. }
  51. }
  52.  
  53. else if ( expression[a] == '(' )
  54. {
  55. here.push();
  56. }
  57.  
  58. else if ( expression[a] == ')' )
  59. {
  60. here.pop();
  61. }
  62. else if ( isdigit(again[a]) )
  63. {
  64. if ( isdigit(again[a+1]) )
  65. {
  66. int b=a+1;
  67. while ( isdigit(again[b]) )
  68. {
  69. hold=again[b];
  70. number = number + hold; // number already equals the string here
  71. ++b;
  72. }
  73. infix = infix + ' ' + number;
  74. a=b-1;
  75. }
  76. else
  77. {
  78. infix = infix + ' ' + number;
  79. }
  80.  
  81. } // end of else if
  82. cout << "\nINFIX "<< infix << endl;
  83. } // end of for
  84.  
  85. Infix = Infix + ' ' + stack;
  86. }
  87.  
  88.  
  89. #endif






header file:

  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. private:
  32. int maxStackSize;
  33. int stackTop;
  34. Type *list;
  35. void copyStack(const stackType<Type>& otherStack);
  36. bool isEqual(const stackType<Type>&);
  37.  
  38. };
  39.  
  40. template<class Type>
  41. void stackType<Type>::copyStack(const stackType<Type>& otherStack)
  42. {
  43. delete [] list;
  44. maxStackSize = otherStack.maxStackSize;
  45. stackTop = otherStack.stackTop;
  46.  
  47. list = new Type[maxStackSize];
  48. assert(list != NULL);
  49.  
  50. for(int j = 0; j < stackTop; j++)
  51. list[j] = otherStack.list[j];
  52.  
  53. }
  54.  
  55. template<class Type>
  56. stackType<Type>::stackType(const stackType<Type>& otherStack)
  57. {
  58. list = NULL;
  59.  
  60. copyStack(otherStack);
  61.  
  62. }
  63.  
  64. template<class Type>
  65. const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
  66. {
  67. if (this != &otherStack)
  68. copyStack(otherStack);
  69.  
  70. return *this;
  71. }
  72.  
  73. template<class Type>
  74. void stackType<Type>::initializeStack()
  75. {
  76. stackTop = 0;
  77. }
  78.  
  79. template<class Type>
  80. void stackType<Type>::destroyStack()
  81. {
  82. stackTop = 0;
  83. }
  84.  
  85. template<class Type>
  86. bool stackType<Type>::isEmptyStack()
  87. {
  88. return(stackTop == 0);
  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.  
  156.  
  157. template<class Type>
  158. bool stackType<Type>::isEqual(const stackType<Type>& otherStack) {
  159. bool bRet = false;
  160. if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop){
  161. bRet = true;
  162. for (int j = 0; j < stackTop; ++j) {
  163. if (otherStack.list[j] != list[j]) {
  164. // cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
  165. bRet = false;
  166. break;
  167. }
  168. }
  169. }
  170. return bRet;
  171. }
  172.  
  173. template<class Type>
  174. bool stackType<Type>::operator==(const stackType<Type>& otherStack) {
  175. return isEqual(otherStack);
  176. }
  177.  
  178. template<class Type>
  179. bool stackType<Type>::operator!=(const stackType<Type>& otherStack) {
  180. return !isEqual(otherStack); //!(*this == otherStack);
  181. }
  182.  
  183.  
  184.  
  185. void toInfix(string expression)
  186. {
  187.  
  188. stackType<string> stack,here;
  189. string Infix;
  190. string priority;
  191. string number, hold, again;
  192.  
  193. again=expression;
  194.  
  195. ifstream inData("input.txt"); // declaration of inData
  196. ofstream outData("output.txt"); // declaration of outData
  197.  
  198. while (getline( inData, expression )) // reads words in file using getline to capture all spaces
  199. { /*something to break up line into individual character*/
  200.  
  201. cout << expression;
  202.  
  203. }
  204.  
  205. for (int a=0; a < expression.length(); a++)
  206. {
  207.  
  208. number = here = expression[a];
  209. cout << "The expression " << here << endl;
  210. cout << "The Infix " << Infix << endl;
  211.  
  212.  
  213. if ( expression[a] == '+' || expression[a] == '-')
  214. {
  215. if (priority <= 2 )
  216. {
  217. here.pop(); here.push();
  218. }
  219. else
  220.  
  221. here.push();
  222. }
  223.  
  224. else if ( expression[a] == '*' || expression[a] == '/' || expression[a] == '%' )
  225. {
  226. if (priority == 1 )
  227. {
  228. here=stack[0]; stack[0]=expression[a]; Infix = Infix + here;
  229. }
  230. else
  231.  
  232. here.push;
  233. }
  234. else if ( expression[a] == '(' ) here.push();
  235. else if ( expression[a] == ')' ) here.pop();
  236.  
  237. else if ( isdigit(again[a]) )
  238. { // for dealing with multiple digits and single digit numbers
  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 + hold; // number already equals the string here
  246. ++b;
  247. }
  248. infix = infix + ' ' + number;
  249. a=b-1;
  250. }
  251. else
  252. {
  253. infix = infix + ' ' + number;
  254. }
  255.  
  256. } // end of else if
  257. cout << "\nINFIX "<< infix << endl;
  258. } // end of for
  259.  
  260. Infix = Infix + ' ' + stack;
  261. }
  262.  
  263.  
  264. #endif




main.cpp



#include <iostream>
#include <stdlib.h>
#include "stack.h"

using namespace std;

int main()
{

toInfix(string expression);


system("PAUSE");
return 0;
}
Last edited by NinjaLink; 32 Days Ago at 1:26 am.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC