Facebook
From levent , 4 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 130
  1. /************************************
  2.  *    Data Structures & Algotihms   *
  3.  *    Project 1                     *
  4.  *    Levent Kaya                   *
  5.  *    1800003183                    *  
  6.  ************************************/
  7. #include <iostream>
  8. #include <string>
  9. #include "myStack.h"
  10. #include "stackADT.h"
  11.  
  12. using namespace std;
  13.  
  14. class infixToPostfix{
  15.     public:
  16.         void convertToPostfix();
  17.         bool precedence(char opr1, char opr2);
  18.         void getInfix(string);
  19.         void showInfix();
  20.         void showPostfix();
  21.         infixToPostfix(string);
  22.         string getPfx();
  23.         int evaluatePostfix(char* exp);
  24.  
  25.     private:
  26.         string infx;
  27.         string postfx;
  28. };
  29.  
  30. bool infixToPostfix::precedence(char opr1, char opr2){
  31.    char c1 = opr1, c2 = opr2;
  32.    int flag1 = 0, flag2 = 0;
  33.    
  34.    if(c1 == '(' || c1 == ')'){
  35.        flag1 = 0;
  36.    }
  37.    else if(c2 == '(' || c2 == ')'){
  38.        flag2 = 0;
  39.    }
  40.    else if(c1 == '+' || c1 == '-'){
  41.        flag1 = 1;
  42.    }
  43.    else if(c1 == '*' || c1 == '/'){
  44.        flag1 = 2;
  45.    }
  46.    else if(c2 == '+' || c2 == '-'){
  47.        flag2 = 1;
  48.    }
  49.    else if(c2 == '*' || c2 == '/'){
  50.        flag2 = 2;
  51.    }
  52.  
  53.    if(flag1 >= flag2){
  54.        return true;
  55.    }
  56.    else
  57.     return false;
  58. }
  59.  
  60. string infixToPostfix::getPfx(){
  61.     return postfx;
  62. }
  63.  
  64. void infixToPostfix::showPostfix(){
  65.     cout << getPfx() << endl;
  66. }
  67.  
  68. void infixToPostfix::getInfix(string inputInfix){
  69.     infx = inputInfix;
  70. }
  71.  
  72. void infixToPostfix::showInfix(){
  73.     cout << infx << endl;
  74. }
  75.  
  76. int checkSymOperator(char symbol){
  77.     if(symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'){
  78.         return 1;
  79.     }
  80.     else
  81.     {
  82.         return 0;
  83.     }
  84.    
  85. }
  86. int checkSymOperand(char symbol){
  87.     if(isalpha(symbol) || isdigit(symbol)){
  88.         return 1;
  89.     }
  90.     else{
  91.         return 0;
  92.     }
  93. }
  94.  
  95. void infixToPostfix::convertToPostfix(){
  96.  
  97.     stackType<char> stack;
  98.     postfx = "";
  99.  
  100.     infx.append(")");
  101.     stack.push('(');
  102.  
  103.     for (int i = 0; i < infx.length(); i++){
  104.         if(checkSymOperator(infx[i])){
  105.             if(checkSymOperator(stack.top())){
  106.                 if(precedence(infx[i], stack.top()) == 0){
  107.                     postfx = postfx + stack.top();
  108.                     stack.pop();
  109.                 }
  110.                 else if(precedence(infx[i], stack.top()) == 1){
  111.                     stack.push(infx[i]);
  112.                 }
  113.                 else{
  114.                     postfx = postfx + stack.top();
  115.                     stack.pop();                    
  116.                 }                
  117.             }
  118.             else{
  119.                 stack.push(infx[i]);
  120.             }            
  121.         }
  122.         else{
  123.             if(infx[i] = ')'){
  124.                 while (stack.top() != '('){
  125.                     postfx = postfx + stack.top();
  126.                     stack.pop();
  127.                 }
  128.                 stack.pop();                
  129.             }
  130.             else{
  131.                 postfx = postfx + infx[i];
  132.             }          
  133.            
  134.         }
  135.        
  136.     }
  137.      
  138.    
  139. }
  140.  
  141. infixToPostfix::infixToPostfix(string pref){
  142.     pref = "";
  143. }
  144.  
  145.  
  146. int main(){
  147.     string input;
  148.     infixToPostfix inf2post;
  149.    
  150.     cout << "Infix Expression: ";
  151.     cin >> input;
  152.    
  153.     inf2post.getInfix(input);
  154.     inf2post.convertToPostfix();
  155.  
  156.     cout << "Postfix Expression: ";
  157.     inf2post.showPostfix();
  158.     cout << endl;
  159.  
  160.     return 0;
  161. }