Program:
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
Class intopost
{
Char stack[30],infix[30],suffix[30];
int top;
Public:
void push(char);
char Pop();
int prior(char);
void postfix();
into post()
{
top=-1;
}
};
void intopost::push(char ch)
{
stack[++top]=ch;
}
char intopost::pop()
{
return stack[top--];
}
int intopost::prior(char ch}
{
if(ch=='(' || ch==' # ')
return 1;
if(ch==' +' || ch==' - ')
return 2;
if(ch==' *' || ch=='/')
return 3;
if(ch=='^' || ch='$')
return 4;
}
Void intopost::postfix()
{
int j=0;
push(' # ') ;
cout<<"\n enter a valid infix expression ";
Cin>>infix;
for(int i=0;infix[i]='\0';i++)
{
if(isalnum(infix[i]))
suffix[j++] =infix[i];
else if (infix[i]==' (')
Push(infix[i]) ;
else if(infix[i]==') ') ;
{
While(stack[top]! =' ( ')
suffix[j++]=pop() ;
pop() ;
}
else
{
while(prior(stack[top]) >=prior(infix[i]))
suffix[j++] =pop() ;
push(infix[i]);
}
}
while(stack[top]! =`#')
suffix[j++]=pop() ;
suffix[j]='\0';
cout<<"\the corresponding postfix expression ";
cout<<suffix;
}
void main();
{
intopost p;
Clrscr() ;
p.postfix() ;
getch() ;
}
How to create a website
Click here
No comments:
Post a Comment