#include<iostream.h>
#include<conio.h>
#include<string.h>
char a[20];
int top=-1;
void push(char sym)
{
++top;
a[top]=sym;
}
char pop()
{
char r=a[top];
--top;
return r;
}
int precedence(char sym)
{
if(sym=='*' || sym=='/')
return 2;
if(sym=='+' || sym=='-')
return 1;
else
return 0;
}
void main()
{
int l;
char e[80];
clrscr();
cout<<"Enter any infix notation : ";
cin>>e;
l=strlen(e);
e[l]='#';
push('#');
cout<<endl
<<"Postfix Notation : ";
for(int i=0;i<=l;i++)
{
if(e[i]>=48 && e[i]<=57)
cout<<e[i];
if(e[i]=='*'|| e[i]=='/'|| e[i]=='+'|| e[i]=='-')
{
while(precedence(a[top]) >= precedence(e[i]))
{
char s=pop();
cout<<s;
}
push(e[i]);
}
if(e[i]=='#')
{
while(a[top]!='#')
cout<<pop();
}
}
getch();
}
No comments:
Post a Comment