#include<iostream>
#include<cstdlib>
using namespace std;
class stackinpost
{
char infix[30];
char postfix[30];
char stack[30];
int top;
public:
stackinpost()
{
top=-1;
}
void push(char);
char pop();
int precedence(char);
void intopost();
};
void stackinpost::push(char ele)
{
stack[++top]=ele;
}
char stackinpost::pop()
{
return stack[top--];
}
int stackinpost::precedence(char ele)
{
switch(ele)
{
case '#':return -1;
case '(':return 0;
case '+':
case '-':return 1;
case '/':
case '*':return 2;
case '^':return 3;
}
}
void stackinpost::intopost()
{
char ch;
stack[++top]='#';
cout<<"\nEnter the infix expression";
cin>>infix;
int k=0;
for(int i=0;infix[i]!='\0';i++)
{
switch(infix[i])
{
case '(':push(infix[i]);
break;
case ')':
{
ch=pop();
while(ch!='(')
{
postfix[k++]=ch;
ch=pop();
}
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
{
int p;
p=precedence(infix[i]);
while(p<=precedence(stack[top]))
postfix[k++]=pop();
push(infix[i]);
}
break;
default:postfix[k++]=infix[i];
}
}
while(stack[top]!='#')
postfix[k++]=pop();
postfix[k]='\0';
cout<<"\nPostfix expression is \n";
cout<<postfix;
}
int main()
{
stackinpost ob;
ob.intopost();
}
oray the heading is wrong!! this program is infix to postfix!!! change it!
ReplyDelete