#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
char stack[60],exp[60],p[60];
int tos=-1,m,n,t=1;
void main()
{
void push(char),display(int);
char ch, pop() ;
clrscr();
cout<<"Enter the Expression : ";
gets(exp);
exp[strlen(exp)]=')';
exp[strlen(exp)+1]=NULL;
// puts(exp);
getch();
push('(');
while(tos>-1)
{
switch(exp[m])
{
case'+':
case'-':
while(stack[tos]=='+'||stack[tos]=='-'||stack[tos]=='*'||stack[tos]=='/'||stack[tos]=='^'||stack[tos]=='%')
{
ch=pop();
p[n]=ch;
n++;
//push(exp[m]);
}
//else
push(exp[m]);
break;
case'*':
case'/':
case'%':
while(stack[tos]=='*'||stack[tos]=='/'||stack[tos]=='^'||stack[tos]=='%')
{
ch=pop();
p[n]=ch;
n++;
}
//else
push(exp[m]);
break;
case'^':
while(stack[tos]=='^')
{
ch=pop();
p[n]=ch;
n++;
}
//else
push(exp[m]);
break;
case ')':
while(stack[tos]!='(')
{
ch=pop();
p[n]=ch;
n++;
}
ch=pop();
break;
case '(':
push('(');
break;
case ' ':
default:
//ch=pop();
p[n]=exp[m];
n++;
}
m++;
//cout<<p[n]<<endl;
display(m-1);
}
}
void push(char c)
{
// cout<<"push";
tos++;
stack[tos]=c;
}
char pop()
{
char ch;
if(tos<0)
return(0);
else
{
ch=stack[tos];
stack[tos]=' ';
tos--;
return(ch);
}
}
void display(int aa)
{
//cout<<"\ncheck" ;
if(t==1)
{
cout<<"\n\t Symbol \t\t Stack \t\t\t Postfix Expression ";
cout<<"\n\t ------ \t\t ------\t\t\t -------------------\n";
t++;
}
if(tos==-1)
cout<<"\n\t "<<exp[aa]<<"\t\t\t Empty"<<"\t\t\t"<<p;
else
cout<<"\n\t "<<exp[aa]<<"\t\t\t "<<stack<<"\t\t\t\t"<<p;
cout<<endl;
getch();
}
No comments:
Post a Comment