Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <sstream>
- #include <string>
- using namespace std;
- #include "stack.h"
- string postFix(string exp);
- void main()
- {
- string expression;
- cout<<"Enter An Infix Expression : ";
- getline(cin, expression);
- expression = postFix(expression);
- cout<<"\nPostfix expression is "<<expression<<"\n";
- }
- string postFix(string exp){
- Stack stack;
- char token;
- char * tokens;
- string postfixExp="", temp;
- int i = 0, var;
- for (int i=0; i<exp.length(); i++){
- token=exp[i];
- switch(token)
- {
- case ' ': break;
- case '(':
- temp = token;
- stack.push(temp);
- break;
- case ')':
- temp = stack.pop();
- token = temp[0];
- do{
- postfixExp.append(1,token);
- temp = stack.pop();
- token = temp[0];
- }while(token != '(');
- break;
- case '+':
- temp = token;
- stack.push(temp);
- break;
- case '-':
- temp = token;
- stack.push(temp);
- break;
- case '*':
- temp = token;
- stack.push(temp);
- break;
- case '/':
- temp = token;
- stack.push(temp);
- break;
- default:
- postfixExp=postfixExp.append(1,token);
- break;
- }
- i++;
- }
- stack.display();
- temp = stack.pop();
- return postfixExp;
- }
Add Comment
Please, Sign In to add comment