Advertisement
skb50bd

Infix-Posfix-Evaluate

Aug 2nd, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <cstdlib>
  4. #include <cmath>
  5. using namespace std ;
  6.  
  7. string convStr(char ch) {
  8.     string line = "";
  9.     line += ch;
  10.     return line;
  11. }
  12.  
  13. string clrstr(string str) {
  14.     string out = "";
  15.     for (int i = 0, j = -1; str[i]; i++) {
  16.         if (str[i] != ' ') {
  17.             out += convStr(str[i]);
  18.             j++;
  19.         }
  20.         else
  21.             if (out[j] != ' ') {
  22.                 out += " ";
  23.                 j++;
  24.             }
  25.     }
  26.     return out;
  27. }
  28.  
  29. template <class T>
  30. class myStack {
  31. private:
  32.     T data;
  33.     myStack *next;
  34.  
  35. public:
  36.     myStack(T x, myStack *n): data(x), next(n) {}
  37.     myStack(): next(NULL) {}
  38.  
  39.     void push(T x) {
  40.         next = new myStack(x, next);
  41.     }
  42.  
  43.     bool pop() {
  44.         if (next != NULL) {
  45.             myStack *D = next;
  46.             next = next -> next;
  47.             delete D;
  48.             return true;
  49.         }
  50.         else return false;
  51.     }
  52.  
  53.     T top() {
  54.         if (next != NULL)
  55.             return next -> data;
  56.     }
  57.  
  58.     int count() {
  59.         int i = 0;
  60.         for (myStack *cur = next; cur != NULL; cur = cur -> next)
  61.             i++;
  62.         return i;
  63.     }
  64.  
  65.     void display() {
  66.         if (next != NULL)
  67.             for (myStack *cur = next; cur != NULL; cur = cur -> next)
  68.                 cout << cur -> data << " ";
  69.         else
  70.             cout << "< empty >";
  71.  
  72.         cout << endl;
  73.     }
  74.  
  75.     bool empty() {
  76.         return bool(next);
  77.     }
  78.  
  79.     void sort() {
  80.         if (next != NULL)
  81.             for (myStack *prev = next; prev -> next != NULL; prev = prev -> next)
  82.                 for (myStack *cur = prev -> next; cur != NULL; cur = cur -> next)
  83.                     if(prev -> data > cur -> data)
  84.                         swap(prev -> data, cur -> data);
  85.     }
  86. };
  87.  
  88. void pop(double &first, double &second, myStack <double> &S) {
  89.     second = S.top();
  90.     S.pop();
  91.     first = S.top();
  92.     S.pop();
  93. }
  94.  
  95. int main() {
  96.     myStack <char> S;
  97.     char ch;
  98.     string line, str;
  99.     string postfix = "";
  100.  
  101.     cout << "Enter the Infix Expression (with parenthesis): ";
  102.     getline(cin, line);
  103.  
  104.     for (int i = 0; line[i]; i++) {
  105.         if (line[i] == ' ')
  106.             postfix += " ";
  107.         else if (line[i] == '(' || line[i] == '+' || line[i] == '-' || line[i] == '*' || line[i] == '/' || line[i] == '%' || line[i] == '^')
  108.             S.push(line[i]);
  109.         else if (line[i] == ')') {
  110.             while (S.top() != '(') {
  111.                 postfix += " " + convStr(S.top()) + " ";
  112.                 S.pop();
  113.             }
  114.             S.pop();
  115.         }
  116.         else
  117.             postfix += line[i];
  118.     }
  119.  
  120.     postfix = clrstr(postfix);
  121.     cout << "Postfix: " << postfix << endl;
  122.  
  123.     //Evaluate
  124.     istringstream inpstr(postfix);
  125.     myStack <double> Sr;
  126.     double first, second;
  127.  
  128.         while(inpstr >> str) {
  129.             if(str == "+") {
  130.                 pop(first, second, Sr);
  131.                 Sr.push(first + second);
  132.             }
  133.             else if(str == "-") {
  134.                 pop(first, second, Sr);
  135.                 Sr.push(first - second);
  136.             }
  137.             else if(str == "*") {
  138.                 pop(first, second, Sr);
  139.                 Sr.push(first * second);
  140.             }
  141.             else if(str == "/") {
  142.                 pop(first, second, Sr);
  143.                 Sr.push(first / second);
  144.             }
  145.             else if(str == "^") {
  146.                 pop(first, second, Sr);
  147.                 Sr.push(pow(first, second));
  148.             }
  149.             else
  150.                 Sr.push(strtof(str.c_str(), NULL)); //str.c_str() returns a C-String eqv of the String type object
  151.         }
  152.         cout << "Value of the expression is: " << Sr.top() << endl;
  153.  
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement