Facebook
From levent , 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 146
  1. /***** File name: infixToPostfix.h ****/
  2.  
  3. // Header file
  4. #include<string>
  5. using namespace std;
  6.  
  7. // Create a class named infixToPostfix
  8. class infixToPostfix
  9. {
  10. // Declare the variables
  11. string infix;
  12. string postfix;
  13. public:
  14.  
  15. // Declare the methods.
  16. void showPostfix();
  17. bool isOperator(const char c);
  18. int precedence(const char op1, const char op2);
  19. void getInfix(string infixString)
  20. {
  21.   infix = infixString.substr(0, infixString.length() - 1);
  22.   //cout << infix;
  23. }
  24. void showInfix()
  25. {
  26.   cout << "\nThe infix expression is : " << infix;
  27. }
  28. };
  29.  
  30. // The method definitations are as follows:
  31. //a utility function to find a given character is an operator or not
  32. bool infixToPostfix::isOperator(const char c)
  33. {
  34. //     check for the various operators
  35. if ((c == '+') || (c == '-') || (c == '*') ||
  36.   (c == '/') || (c == '^') || (c == '%'))
  37. {
  38.   return true;
  39. }
  40.  
  41. else
  42. {
  43.   return false;
  44. }  
  45. }  
  46.  
  47. int infixToPostfix::precedence(const char op1, const char op2)
  48. {
  49. //     please note that only 5 operators were only
  50. //     considered for precedence.
  51. //     Not considered ^ for comparing the precedence
  52. int pre1, pre2;//declare variables to compare the precedence
  53. if ((op1 == '^') || (op2 == '^'))
  54. {
  55.   cout << "Exponentiation was not considered"
  56.    << "for precedence. \n\tThe program may"
  57.    << "result in abnormal output of Postfix exp.";
  58. }  
  59. if ((op1 == '+') || (op1 == '-'))
  60. {
  61.   //low precedence
  62.   pre1 = 0;
  63. }
  64. else
  65. {
  66.   if ((op1 == '*') || (op1 == '/') || (op1 == '%'))
  67.    pre1 = 1;                  //     high precedence
  68. }
  69.  
  70. if ((op2 == '+') || (op2 == '-')) //low precedence
  71. {
  72.   pre2 = 0;
  73. }
  74.  
  75. else
  76. {
  77.   if ((op2 == '*') || (op2 == '/') || (op2 == '%'))
  78.   {
  79.    pre2 = 1;
  80.   }  
  81. }
  82.  
  83. if (pre1 == pre2)
  84. {
  85.   //     equal precedence of op1 & op2
  86.   return 0;
  87. }
  88. else
  89. {
  90.   if (pre1 > pre2)   //higher precedence of op1 over op2
  91.   {
  92.    return 1;
  93.   }
  94.   else
  95.   {
  96.    //     lower precedence of op1 over op2
  97.    return -1;
  98.   }
  99. }
  100.  
  101. }
  102.  
  103. // Conversion of infix arithmetic exp into postfix exp
  104. void infixToPostfix::showPostfix()
  105. {
  106. myStack< char > myStack;
  107. string pfx = "";
  108.  
  109. //append a right parenthesis ')' to the end of infix
  110. infix.append(")");
  111.  
  112. // push the left parenthesis onto the stack
  113. myStack.push('(');
  114. unsigned int i = 0;
  115.  
  116. do
  117. {
  118.   // when the current element in infix is an operator
  119.   if (isOperator(infix[i]))
  120.   {
  121.    // when the top of the stack is an operator
  122.    if (isOperator(myStack.top()))
  123.    {    
  124.     if (precedence(infix[i], myStack.top()) == 0)
  125.     {    
  126.      pfx = pfx + myStack.top();
  127.      myStack.pop();
  128.     }  
  129.     else
  130.      if (precedence(infix[i],
  131.       myStack.top()) == 1)
  132.      {
  133.       myStack.push(infix[i]);
  134.       i++;
  135.      }
  136.      else
  137.      {    
  138.       pfx = pfx + myStack.top();
  139.       myStack.pop();
  140.      }
  141.    }
  142.    else
  143.    {
  144.     myStack.push(infix[i]);
  145.     i++;
  146.    }
  147.   }
  148.   else
  149.   {
  150.    if (infix[i] == ')')
  151.    {    
  152.     while (myStack.top() != '(')
  153.     {
  154.      pfx = pfx + myStack.top();
  155.      myStack.pop();
  156.     }
  157.     myStack.pop();
  158.     i++;
  159.    }
  160.    else
  161.     if (infix[i] == '(')
  162.     {    
  163.      myStack.push(infix[i]);
  164.      i++;
  165.     }  
  166.     else
  167.     {    
  168.      pfx = pfx + infix[i];
  169.      i++;
  170.     }
  171.   }
  172. } while (i < infix.length());  
  173.  
  174. cout << "\nThe postfix expression is : " << pfx;
  175. }  
  176.  
  177. /***** File name: myStack.h ****/
  178.  
  179. #ifndef H_myStack
  180. #define H_myStack
  181.  
  182. // Header file
  183. #include <iostream>
  184. #include<cassert>
  185. using namespace std;
  186. // definition of the template class myStack
  187. template <class Type>
  188. class myStack
  189. {
  190. // data memebers of the class
  191. private:
  192. int maxStackSize;
  193. int stackTop;
  194. Type *list;
  195.  
  196. // data methods of the class
  197. public:
  198. void initializeStack();
  199. bool isFullStack() const;
  200. bool isEmptyStack() const;
  201. void push(const Type&);
  202. void pop();
  203. Type top() const;
  204. myStack(int = 20);
  205. ~myStack();
  206. };
  207. template <class Type>
  208. void myStack<Type>::initializeStack()
  209. {
  210. stackTop = 0;
  211. }
  212. template <class Type>
  213. bool myStack<Type>::isFullStack() const
  214. {
  215. return (stackTop == maxStackSize);
  216. }
  217. template <class Type>
  218. bool myStack<Type>::isEmptyStack() const
  219. {
  220. return (stackTop == 0);
  221. }
  222.  
  223. template <class Type>
  224. void myStack<Type>::push(const Type& newItem)
  225. {
  226. if (!isFullStack())
  227. {
  228.   list[stackTop] = newItem;
  229.   stackTop++;
  230. }
  231. else
  232. {
  233.   cout << "\n\tCan not add to a full stack";
  234. }  
  235. }  
  236. template <class Type>
  237. void myStack<Type>::pop()
  238. {
  239. if (!isEmptyStack())
  240.   stackTop--;
  241. else
  242.   cout << "\n\tCan not remove from an empty stack";
  243. }  
  244. template <class Type>
  245. Type myStack<Type>::top() const
  246. {
  247. assert(stackTop != 0);
  248. return list[stackTop - 1];
  249. }  
  250. template <class Type>
  251. myStack<Type>::myStack(int stackSize)
  252. {
  253. if (stackSize <= 0)
  254. {
  255.   cout << "Invalid size";
  256.   stackSize = 10;
  257. }    
  258. else
  259.   maxStackSize = stackSize;
  260. stackTop = 0;
  261. list = new Type[maxStackSize];
  262. }  
  263. template <class Type>
  264. myStack<Type>::~myStack()
  265. {
  266. delete[] list;
  267. }  
  268. #endif
  269.  
  270. /***** File name: Main.cpp ****/
  271. #include<fstream>
  272. #include"myStack.h"
  273. #include "infixToPostfix.h"
  274. using namespace std;
  275. //     function main begins program execution
  276. int main()
  277. {
  278. //declaring class variable
  279. infixToPostfix InfixExp;
  280. string infix;
  281. //file stream object
  282. ifstream infile;
  283. //opening a file
  284. infile.open("infixData.txt", ios::in);
  285. //verifying for file existance
  286. if (!infile)
  287. {
  288.   cout << "Cannot open input file. Program terminates!!!" << endl;
  289. //  system("pause");
  290.   return 1;
  291. }
  292. //reading first line
  293. getline(infile, infix);
  294. while (infile)
  295. {
  296.   //calling methods
  297.   InfixExp.getInfix(infix);
  298.   InfixExp.showInfix();
  299.   InfixExp.showPostfix();
  300.   cout << endl;
  301.   //again reading next line
  302.   getline(infile, infix);
  303. }
  304. //closing file
  305. infile.close();
  306. // system("pause");
  307. return 0;
  308. }