Advertisement
carlos1993

Infix to Postfix expression converter.

Oct 1st, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. /*
  2.  *
  3.  * Author: Carlos Alaniz         20244196
  4.  * Date: 10/01/2013
  5.  * Instructor: Zhixiang Chen;
  6.  * Course : CMPE 3333.02
  7.  *
  8.  * Description: Infix to Postfix expression converter.
  9.  **/
  10.  
  11.  
  12.  
  13.  
  14.  
  15. #include <iostream>
  16. #include <string>
  17. #include <cstdlib>
  18. #include <map>
  19.  
  20. using namespace std;
  21.  
  22. class postfix {
  23. private:
  24.     string stack;
  25.     string post;
  26.     string pre;
  27.     map <char, int> ops;
  28. public:
  29.     postfix(string pre) {
  30.         post.clear();
  31.         stack.clear();
  32.         this->pre = pre;
  33.         //Operator herarchy table
  34.         ops['-'] = 1;
  35.         ops['+'] = 1;
  36.         ops['/'] = 2;
  37.         ops['*'] = 2;
  38.         ops['^'] = 4;
  39.         ops['('] = -1;
  40.         ops[')'] = -1;
  41.     }
  42.  
  43.     void checksym(char sym) {
  44.         if (stack.empty() || sym == '(') {
  45.             stack += sym; //push into stack
  46.             return;
  47.         } else {
  48.             char topOp = stack[stack.length() - 1];
  49.             while (!stack.empty() && ops[sym] <= ops[topOp]) {
  50.                 //Pop and discard
  51.                 stack.erase(stack.end() - 1);
  52.                 if (topOp == '(' && sym == ')') {
  53.                     //stack.erase(stack.end() - 1);
  54.                     break;
  55.                 }
  56.                 post += topOp;
  57.                 if (!stack.empty()) {
  58.                     topOp = stack[stack.length() - 1];
  59.                 }
  60.             }
  61.             if (sym != ')')
  62.                 stack += sym;
  63.         }
  64.  
  65.  
  66.     }
  67.  
  68.     void scan() {
  69.         for (unsigned i = 0; i < pre.length(); i++) {
  70.             if (isdigit(pre[i])) {
  71.                 //separate numbers with a "'" ex. 380'5'85'5+++ 580.58.+5.+9.+12541.+
  72.                 while (isdigit(pre[i])) {
  73.                     post += pre[i];
  74.                     i++;
  75.                 }
  76.                 post += ',';
  77.                 i--;
  78.             } else if (isalpha(pre[i])) {
  79.                 if (post.find(pre[i]) >= 0)
  80.                     post += pre[i];
  81.                 else
  82.                     exit(-5);
  83.             } else if (pre[i] == ' ') {
  84.                 //ignore
  85.             } else {
  86.                 checksym(pre[i]);
  87.             }
  88.         }
  89.         for (int j = stack.length() - 1; j >= 0; j--) {
  90.             post += stack[j]; //print the rest of the stack
  91.             stack.erase(stack.end() - 1);
  92.         }
  93.  
  94.     }
  95.    
  96.     string get_postfix(){
  97.         return post;
  98.     }
  99. };
  100.  
  101. int main() {
  102.     //string pre = "(5+8*(8056-7))*8^5-5+(9^8*8/8-5)";
  103.     //string pre = "2*3-4/5";
  104.     string pre = "a+b*c-d";
  105.     postfix post(pre);
  106.     post.scan();
  107.     cout << post.get_postfix() << endl;
  108.     return 0;
  109. }
  110.  
  111.  
  112. /**
  113.  * Sample Output:
  114.  *
  115.  * input : (5+8*(8056-7))*8^5-5+(9^8*8/8-5)
  116.  * output : 5.8.8056.7.-*+8.5.^*5.-9.8.^8.*8./5.-+
  117.  *
  118.  * input : 2*3-4/5
  119.  * output : 2.3.*4.5./-
  120.  *
  121.  * input : a+b*c-d
  122.  * output : abc*+d-
  123.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement