Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Program to perform all types of stack conversions */
- #include<iostream>
- #include<string>
- #define MAX 20
- using namespace std;
- void push(char);
- char pop();
- string in2postfix(string);
- int priority ( char );
- char stk[20];
- int top=-1;
- int main()
- {
- int cont;
- string infix, postfix;
- cout<<"\n enter the infix expression : ";
- //cin>>infix;
- postfix = in2postfix("a+b*c/(d-e)+f");
- return 0;
- }
- // method that pushes the elements onto the character stack
- void push(char oper)
- {
- if(top==MAX-1)
- {
- cout<<"stackfull!!!!";
- }
- else
- {
- top++;
- stk[top]=oper;
- }
- }
- // method that removes character from stack and returns them
- char pop()
- {
- char ch;
- if(top==-1)
- {
- cout<<"stackempty!!!!";
- }
- else
- {
- ch=stk[top];
- stk[top]='\0';
- top--;
- return(ch);
- }
- return 0;
- }
- // method that converts String from infix to postfix
- // all the strings are assumed to be valid expressions
- string in2postfix(string infix)
- {
- int i=0;
- string postfix = "";
- // iterating while end of string is not found
- while(infix[i]!='\0')
- {
- // if an alphabet is found then copy it to the output string
- if(infix[i]>='a' && infix[i]<='z')
- {
- postfix.insert(postfix.end(),infix[i]);
- i++;
- }
- // if an opening bracket is found then put it in stack
- else if(infix[i]=='(' || infix[i]=='{' || infix[i]=='[')
- {
- push(infix[i]);
- i++;
- }
- // if an closing bracket is found then
- // keep removing the operators from the stack and add them to postfix string until you find the corresponding opening bracket
- else if(infix[i]==')' || infix[i]=='}' || infix[i]==']')
- {
- if(infix[i]==')')
- {
- while(stk[top]!='(')
- {
- postfix.insert(postfix.end(),pop());
- }
- pop();
- i++;
- }
- if(infix[i]==']')
- {
- while(stk[top]!='[')
- {
- postfix.insert(postfix.end(),pop());
- }
- pop();
- i++;
- }
- if(infix[i]=='}')
- {
- while(stk[top]!='{')
- {
- postfix.insert(postfix.end(),pop());
- }
- pop();
- i++;
- }
- }
- // if none of the above cases are satisfied then we surely have an operator
- else
- {
- // if the stack if empty then we simply put the operator in stack
- if(top==-1)
- {
- push(infix[i]);
- i++;
- }
- // if the priority of current operator is less than or equal to the stack top then
- // pop the stack top and add it to the postfix string
- else if( priority(infix[i]) <= priority(stk[top])) {
- postfix.insert(postfix.end(),pop());
- // now if you have an operator that has equal priority as of current operator then pop
- while(priority(stk[top]) == priority(infix[i])){
- postfix.insert(postfix.end(),pop());
- if(top < 0) {
- break;
- }
- }
- push(infix[i]);
- i++;
- }
- // if the priority of current operator if high then push it onto the stack
- else if(priority(infix[i]) > priority(stk[top])) {
- push(infix[i]);
- i++;
- }
- }
- }
- // at the end remove all the operators from the stack
- while(top!=-1)
- {
- postfix.insert(postfix.end(),pop());
- }
- cout<<"The converted postfix string is : "<<postfix;
- return postfix;
- }
- // method that returns priority for operators according to their precedence
- int priority ( char alpha )
- {
- if(alpha == '+' || alpha =='-')
- {
- return(1);
- }
- if(alpha == '*' || alpha =='/')
- {
- return(2);
- }
- if(alpha == '$')
- {
- return(3);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment