Guest User

Untitled

a guest
Feb 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. using namespace std;
  6. #include "stack.h"
  7.  
  8. string postFix(string exp);
  9.  
  10. void main()
  11. {
  12.     string expression;
  13.     cout<<"Enter An Infix Expression : ";
  14.    
  15.     getline(cin, expression);      
  16.     expression = postFix(expression);
  17.     cout<<"\nPostfix expression is "<<expression<<"\n";
  18. }
  19.  
  20. string postFix(string exp){
  21.     Stack stack;    
  22.     char token;
  23.     char * tokens;
  24.     string postfixExp="", temp;
  25.     int i = 0, var;
  26.     for (int i=0; i<exp.length(); i++){
  27.             token=exp[i];
  28.             switch(token)
  29.              {
  30.                 case ' ': break;
  31.                 case '(':
  32.                     temp = token;
  33.                     stack.push(temp);
  34.                     break;
  35.                 case ')':
  36.                     temp = stack.pop();
  37.                     token = temp[0];
  38.                     do{
  39.                         postfixExp.append(1,token);
  40.                         temp = stack.pop();
  41.                         token = temp[0];
  42.                     }while(token != '(');
  43.                     break;
  44.                 case '+':
  45.                     temp = token;
  46.                     stack.push(temp);                              
  47.                     break;
  48.                 case '-':
  49.                     temp = token;
  50.                     stack.push(temp);      
  51.                     break;             
  52.                 case '*':
  53.                     temp = token;
  54.                     stack.push(temp);  
  55.                     break;
  56.                 case '/':
  57.                     temp = token;
  58.                     stack.push(temp);  
  59.                     break;
  60.                 default:
  61.                     postfixExp=postfixExp.append(1,token);
  62.                     break;
  63.                 }
  64.             i++;
  65.             }
  66.     stack.display();   
  67.     temp = stack.pop();
  68.     return postfixExp;
  69. }
Add Comment
Please, Sign In to add comment