/* IMPLEMENT STACK AS AN ADT USING ARRAY. USE THIS ADT TO
PERFORM EXPRESSION CONVERSION & EVALUATION (INFIX TO
POSTFIX,INFIX TO PREFIX,PREFIX TO INFIX,PREFIX TO POSTFIX,
POSTFIX TO INFIX & POSTFIX TO PREFIX)
*/ .
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define MAX 20
void in2post();
void in2pre();
void post2in();
void pre2in();
void post2pre();
char s[MAX];
char s1[MAX][MAX];
int top=-1;
void main()
{
char e[MAX];
int choice;
char ch;
do
{
clrscr();
printf("\n\n\t================= GIVEN MENU FOR STACK=================");
printf("\n\n\t 1.Infix to Postfix");
printf("\n\n\t 2.Infix to Prefix");
printf("\n\n\t 3.Postfix to Infix");
printf("\n\n\t 4.Prefix to Infix");
printf("\n\n\t 5.Postfix to Prefix");
printf("\n\n\t 6.EXIT");
printf("\n\n\t Enter the Choice(1,2,3,4,5,6): ");
scanf("%d",&choice);
printf("\n\n\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
switch(choice)
{
case 1:
printf("\n\n\t*** INFIX TO POSTFIX ***");
printf("\n\n\t Enter the Infix Exp: ");
scanf("%s",&e);
in2post(e);
break;
case 2:
printf("\n\n\t*** INFIX TO PREFIX ***");
printf("\n\n\t Enter the Infix Exp: ");
scanf("%s",&e);
in2pre(e);
break;
case 3:
printf("\n\n\t*** POSTFIX TO INFIX ***");
printf("\n\n\t Enter the Postfix Exp: ");
scanf("%s",&e);
post2in(e);
break;
case 4:
printf("\n\n\t*** PREFIX TO INFIX ***");
printf("\n\n\t Enter the Prefix Exp: ");
scanf("%s",&e);
pre2in(e);
break;
case 5:
printf("\n\n\t*** POSTFIX TO PREFIX ***");
printf("\n\n\t Enter the Postfix Exp: ");
scanf("%s",&e);
post2pre(e);
break;
case 6:
exit(0);
break;
default:
printf("\n\n\t*** INVALID CHOICE ***");
}
printf("\n\n\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
printf("\n\n\t Do you want to continue(y/n): ");
ch=getche();
}while(ch=='Y'||ch=='y');
getch();
}
void push(char c)
{
if(top>=MAX-1)
{
printf("\n\n\T Stack is Full...");
return ;
}
else
{
top++;
s[top]=c;
}
}
char pop()
{
char c;
if(top<0)
{
return NULL;
}
else
{
c=s[top];
top--;
return(c);
}
}
int prior(char c)
{
if(c=='$'||c=='^')
return 3;
if(c=='*'||c=='/'||c=='%')
return 2 ;
else
{
if(c=='+'||c=='-')
return 1 ;
else
return 0 ;
}
}
void in2post(char e[MAX])
{
int i=0,p=0;
char pe[MAX],c,opr;
while(e[i]!='\0')
{
c=e[i];
if(isalpha(c))
pe[p++]=c;
if(c=='(')
push(c);
if(c=='*'||c=='/'||c=='+'||c=='-'||c=='%'||c=='$'||c=='^')
{
if(top!=-1)
{
opr=pop();
while(prior(opr)>=prior(c))
{
pe[p++]=opr;
opr=pop();
}
push(opr);
push(c);
}
else
push(c);
}
if(c==')')
{
opr=pop();
while(opr!='(')
{
pe[p++]=opr;
opr=pop();
}
}
i++;
}
while(top!=-1)
pe[p++]=pop();;
pe[p++]='\0';
printf("\n\n\t %s",pe);
}
void in2pre(char e[])
{
char pe[MAX],c,opr;
int i=0,p=0;
strrev(e);
while(e[i]!='\0')
{
c=e[i];
if(isalpha(c))
pe[p++]=c;
if(c==')')
push(c);
if(c=='*'||c=='/'||c=='+'||c=='-'||c=='%'||c=='$'||c=='^')
{
if(top!=-1)
{
opr=pop();
while(prior(opr)>prior(c))
{
pe[p++]=opr;
opr=pop();
}
push(opr);
push(c);
}
else
push(c);
}
if(c=='(')
{
opr=pop();
while(opr!=')')
{
pe[p++]=opr;
opr=pop();
}
}
i++;
}
while(top!=-1)
pe[p++]=pop();
pe[p++]='\0';
strrev(pe);
printf("\n\n\t Prefix Exp is: %s",pe);
}
void post2in(char post[])
{
char in[20],p1[20],p2[20],ch,o,t[10];
int i=0;
while(post[i]!='\0')
{
ch=post[i];
if(isalpha(ch))
{
t[0]=ch;
t[1]='\0';
top++;
strcpy(s1[top],t);
}
if(ch=='*'||ch=='^'||ch=='+'||ch=='-'||ch=='/')
{
strcpy(p2,s1[top]);
top--;
strcpy(p1,s1[top]);
top--;
t[0]=ch;
t[1]='\0';
strcat(p1,t);
strcat(p1,p2);
top++;
strcpy(s1[top],p1);
}
i++;
}
strcpy(in,s1[top]);
printf("\n\n\t Infix Exp is: %s",in);
}
void pre2in(char pre[])
{
char in[MAX],p1[MAX],p2[MAX],ch,o,t[10];
int i=0;
strrev(pre);
while(pre[i]!='\0')
{
ch=pre[i];
if(isalpha(ch))
{
t[0]=ch;
t[1]='\0';
top++;
strcpy(s1[top],t);
}
if(ch=='*'||ch=='^'||ch=='+'||ch=='-'||ch=='/')
{
strcpy(p2,s1[top]);
top--;
strcpy(p1,s1[top]);
top--;
t[0]=ch;
t[1]='\0';
strcat(p1,t);
strcat(p1,p2);
top++;
strcpy(s1[top],p1);
}
i++;
}
strcpy(in,s1[top]);
strrev(in);
printf("\n\n\t Infix Exp is: %s",in);
}
void post2pre(char post[])
{
char in[MAX],p1[20],p2[20],ch,o,t[10];
int i=0;
while(post[i]!='\0')
{
ch=post[i];
if(isalpha(ch))
{
t[0]=ch;
t[1]='\0';
top++;
strcpy(s1[top],t);
}
if(ch=='*'||ch=='^'||ch=='+'||ch=='-'||ch=='/')
{
strcpy(p2,s1[top]);
top--;
strcpy(p1,s1[top]);
top--;
t[0]=ch;
t[1]='\0';
strcat(t,p1);
strcat(t,p2);
top++;
strcpy(s1[top],t);
}
i++;
}
strcpy(in,s1[top]);
printf("\n\n\t Prefix Exp is: %s",in);
}
/*
OUTPUT:-
GIVEN MENU FOR STACK
1.Infix to Postfix 2.Infix to Prefix 3.Postfix to Infix 4.Prefi
x to Infix 5.Postfix to Prefix 6.EXIT
Enter the Choice(1,2,3,4,5,6): 1
INFIX TO POSTFIX
Enter the Infix Exp: a+b*c
abc*+
Enter the Choice(1,2,3,4,5,6): 2
INFIX TO PREFIX
Enter the Infix Exp: a+c*b/d
Prefix Exp is: +a/*cbd
Do you want to continue(y/n):y
Enter the Choice(1,2,3,4,5,6): 3
POSTFIX TO INFIX
Enter the Postfix Exp: a+*c/b-d
Infix Exp is: d
Do you want to continue(y/n):y
Enter the Choice(1,2,3,4,5,6): 4
PREFIX TO INFIX
Enter the Prefix Exp: a?b+c*d
Infix Exp is: a
Do you want to continue(y/n):y
Enter the Choice(1,2,3,4,5,6):5
POSTFIX TO PREFIX
Enter the Postfix Exp: a^b+c-d/e
Prefix Exp is: e
Do you want to continue(y/n):y
*/