Stack Program #4
18. Write a program to convert in-fix notation to pre-fix notation using Stack.
#include<stdio.h>
#include<string.h>
#define N 50
char s[N];
int top=-1;
void push(char);
char pop();
int f(char);
int g(char);
int r(char);
void main()
{
int i=0,k=0,j=0,rank,c=0,l=0,a=0,m=0;
char infix1[50],infix[50],next,temp,x,polish[50],polish1[50];
printf("Enter a infix expression with extra closing bracket:\n");
scanf("%s",infix1);
while(infix1[k]!='\0')
{
l++; // size getting
k++; //string increase
}
for(j=l-1;j>=0;j--)
{
infix[i]=infix1[j];
i++;
}
infix[i]='\0';
i=0,j=0;
while(infix[i]!='\0')
{
if(infix[i]=='(')
{
infix[i]=')';
}
else if(infix[i]==')')
{
infix[i]='(';
}
i++;
}
infix[i]=')';
i++;
infix[i]='\0';
i=1;
top=0;
s[top]='(';
polish[50]='\0';
rank=0;
next=infix[i];
while(next!='\0')
{
if(top<0)
{
printf("Invalid\n");
return;
}
while(g(s[top])>f(next))
{
temp=pop();
polish[j]=temp;
j++;
rank=rank+r(temp);
if(rank<1)
{
printf("Invalid\n");
return;
}
}
if(g(s[top])!=f(next))
{
push(next);
}
else
{
pop();
}
polish[j]='\0';
i++;
next=infix[i];
}
j=0;
while(polish[j]!='\0')
{
a++;
j++;
}
for(c=a-1;c>=0;c--)
{
polish1[m]=polish[c];
m++;
}
if(top!=-1 || rank!=1)
{
printf("Invalid\n");
}
else
{
printf("Valid\n");
polish1[m]='\0';
printf("Prefix of %s is %s \n",infix1,polish1);
}
}
void push(char x)
{
if(top>=N-1)
{
printf("STACK OVERFLOW\n");
}
else
{
top++;
s[top]=x;
}
}
char pop()
{
if(top==-1)
{
printf("STACK UNDERFLOW\n");
return('\0');
}
else
{
top=top-1;
return(s[top+1]);
}
}
int f(char t)
{
if(t=='+' || t=='-')
{
return 1;
}
if(t=='*' || t=='/')
{
return 3;
}
if(t=='^' || t=='$')
{
return 6;
}
if(t>='a' && t<='z' || t>='A' && t<='Z')
{
return 7;
}
if(t=='(')
{
return 9;
}
if(t==')')
{
return 0;
}
}
int g(char t)
{
if(t=='+' || t=='-')
{
return 2;
}
if(t=='*' || t=='/')
{
return 4;
}
if(t=='^' || t=='$')
{
return 5;
}
if(t>='a' && t<='z' || t>='A' && t<='Z')
{
return 8;
}
if(t=='(')
{
return 0;
}
}
int r(char t)
{
if(t=='+' || t=='-')
{
return -1;
}
if(t=='*' || t=='/')
{
return -1;
}
if(t=='^'|| t=='$')
{
return -1;
}
if(t>='a' && t<='z' || t>='A' && t<='Z')
{
return 1;
}
}
#include<string.h>
#define N 50
char s[N];
int top=-1;
void push(char);
char pop();
int f(char);
int g(char);
int r(char);
void main()
{
int i=0,k=0,j=0,rank,c=0,l=0,a=0,m=0;
char infix1[50],infix[50],next,temp,x,polish[50],polish1[50];
printf("Enter a infix expression with extra closing bracket:\n");
scanf("%s",infix1);
while(infix1[k]!='\0')
{
l++; // size getting
k++; //string increase
}
for(j=l-1;j>=0;j--)
{
infix[i]=infix1[j];
i++;
}
infix[i]='\0';
i=0,j=0;
while(infix[i]!='\0')
{
if(infix[i]=='(')
{
infix[i]=')';
}
else if(infix[i]==')')
{
infix[i]='(';
}
i++;
}
infix[i]=')';
i++;
infix[i]='\0';
i=1;
top=0;
s[top]='(';
polish[50]='\0';
rank=0;
next=infix[i];
while(next!='\0')
{
if(top<0)
{
printf("Invalid\n");
return;
}
while(g(s[top])>f(next))
{
temp=pop();
polish[j]=temp;
j++;
rank=rank+r(temp);
if(rank<1)
{
printf("Invalid\n");
return;
}
}
if(g(s[top])!=f(next))
{
push(next);
}
else
{
pop();
}
polish[j]='\0';
i++;
next=infix[i];
}
j=0;
while(polish[j]!='\0')
{
a++;
j++;
}
for(c=a-1;c>=0;c--)
{
polish1[m]=polish[c];
m++;
}
if(top!=-1 || rank!=1)
{
printf("Invalid\n");
}
else
{
printf("Valid\n");
polish1[m]='\0';
printf("Prefix of %s is %s \n",infix1,polish1);
}
}
void push(char x)
{
if(top>=N-1)
{
printf("STACK OVERFLOW\n");
}
else
{
top++;
s[top]=x;
}
}
char pop()
{
if(top==-1)
{
printf("STACK UNDERFLOW\n");
return('\0');
}
else
{
top=top-1;
return(s[top+1]);
}
}
int f(char t)
{
if(t=='+' || t=='-')
{
return 1;
}
if(t=='*' || t=='/')
{
return 3;
}
if(t=='^' || t=='$')
{
return 6;
}
if(t>='a' && t<='z' || t>='A' && t<='Z')
{
return 7;
}
if(t=='(')
{
return 9;
}
if(t==')')
{
return 0;
}
}
int g(char t)
{
if(t=='+' || t=='-')
{
return 2;
}
if(t=='*' || t=='/')
{
return 4;
}
if(t=='^' || t=='$')
{
return 5;
}
if(t>='a' && t<='z' || t>='A' && t<='Z')
{
return 8;
}
if(t=='(')
{
return 0;
}
}
int r(char t)
{
if(t=='+' || t=='-')
{
return -1;
}
if(t=='*' || t=='/')
{
return -1;
}
if(t=='^'|| t=='$')
{
return -1;
}
if(t>='a' && t<='z' || t>='A' && t<='Z')
{
return 1;
}
}
OR
#include<stdio.h>
#include<string.h>
#define N 50
char s[N];
int top=-1;
void push(char);
char pop();
int f(char);
int g(char);
int r(char);
void main()
{
int i=0,k=0,j=0,rank,c=0,l=0,a=0,m=0;
char infix1[50],infix[50],next,temp,x,polish[50],polish1[50];
printf("Enter a infix expression with extra closing bracket:\n");
scanf("%s",infix1);
while(infix1[k]!='\0')
{
l++; // size getting
k++; //string increase
}
for(j=l-1;j>=0;j--)
{
infix[i]=infix1[j];
i++;
}
infix[i]='\0';
printf("%s\n",infix);
i=0,j=0;
while(infix[i]!='\0')
{
if(infix[i]=='(')
{
infix[i]=')';
}
else if(infix[i]==')')
{
infix[i]='(';
}
i++;
}
infix[i]=')';
i++;
infix[i]='\0';
printf("%s\n",infix);
i=1;
top=0;
s[top]='(';
polish[50]='\0';
rank=0;
next=infix[i];
while(next!='\0')
{
if(top<0)
{
printf("Invalid\n");
return;
}
while(g(s[top])>f(next))
{
temp=pop();
polish[j]=temp;
j++;
rank=rank+r(temp);
if(rank<1)
{
printf("Invalid\n");
return;
}
}
if(g(s[top])!=f(next))
{
push(next);
}
else
{
pop();
}
polish[j]='\0';
printf("%c\t%s\t%s\t%d\n",infix[i],s,polish,rank);
i++;
next=infix[i];
}
j=0;
while(polish[j]!='\0')
{
a++;
j++;
}
for(c=a-1;c>=0;c--)
{
polish1[m]=polish[c];
m++;
}
if(top!=-1 || rank!=1)
{
printf("Invalid\n");
}
else
{
printf("Valid\n");
polish1[m]='\0';
printf("Prefix of %s is %s \n",infix1,polish1);
}
}
void push(char x)
{
if(top>=N-1)
{
printf("STACK OVERFLOW\n");
}
else
{
top++;
s[top]=x;
}
}
char pop()
{
if(top==-1)
{
printf("STACK UNDERFLOW\n");
return('\0');
}
else
{
top=top-1;
return(s[top+1]);
}
}
int f(char t)
{
if(t=='+' || t=='-')
{
return 1;
}
if(t=='*' || t=='/')
{
return 3;
}
if(t=='^' || t=='$')
{
return 6;
}
if(t>='a' && t<='z' || t>='A' && t<='Z')
{
return 7;
}
if(t=='(')
{
return 9;
}
if(t==')')
{
return 0;
}
}
int g(char t)
{
if(t=='+' || t=='-')
{
return 2;
}
if(t=='*' || t=='/')
{
return 4;
}
if(t=='^' || t=='$')
{
return 5;
}
if(t>='a' && t<='z' || t>='A' && t<='Z')
{
return 8;
}
if(t=='(')
{
return 0;
}
}
int r(char t)
{
if(t=='+' || t=='-')
{
return -1;
}
if(t=='*' || t=='/')
{
return -1;
}
if(t=='^'|| t=='$')
{
return -1;
}
if(t>='a' && t<='z' || t>='A' && t<='Z')
{
return 1;
}
}
#include<string.h>
#define N 50
char s[N];
int top=-1;
void push(char);
char pop();
int f(char);
int g(char);
int r(char);
void main()
{
int i=0,k=0,j=0,rank,c=0,l=0,a=0,m=0;
char infix1[50],infix[50],next,temp,x,polish[50],polish1[50];
printf("Enter a infix expression with extra closing bracket:\n");
scanf("%s",infix1);
while(infix1[k]!='\0')
{
l++; // size getting
k++; //string increase
}
for(j=l-1;j>=0;j--)
{
infix[i]=infix1[j];
i++;
}
infix[i]='\0';
printf("%s\n",infix);
i=0,j=0;
while(infix[i]!='\0')
{
if(infix[i]=='(')
{
infix[i]=')';
}
else if(infix[i]==')')
{
infix[i]='(';
}
i++;
}
infix[i]=')';
i++;
infix[i]='\0';
printf("%s\n",infix);
i=1;
top=0;
s[top]='(';
polish[50]='\0';
rank=0;
next=infix[i];
while(next!='\0')
{
if(top<0)
{
printf("Invalid\n");
return;
}
while(g(s[top])>f(next))
{
temp=pop();
polish[j]=temp;
j++;
rank=rank+r(temp);
if(rank<1)
{
printf("Invalid\n");
return;
}
}
if(g(s[top])!=f(next))
{
push(next);
}
else
{
pop();
}
polish[j]='\0';
printf("%c\t%s\t%s\t%d\n",infix[i],s,polish,rank);
i++;
next=infix[i];
}
j=0;
while(polish[j]!='\0')
{
a++;
j++;
}
for(c=a-1;c>=0;c--)
{
polish1[m]=polish[c];
m++;
}
if(top!=-1 || rank!=1)
{
printf("Invalid\n");
}
else
{
printf("Valid\n");
polish1[m]='\0';
printf("Prefix of %s is %s \n",infix1,polish1);
}
}
void push(char x)
{
if(top>=N-1)
{
printf("STACK OVERFLOW\n");
}
else
{
top++;
s[top]=x;
}
}
char pop()
{
if(top==-1)
{
printf("STACK UNDERFLOW\n");
return('\0');
}
else
{
top=top-1;
return(s[top+1]);
}
}
int f(char t)
{
if(t=='+' || t=='-')
{
return 1;
}
if(t=='*' || t=='/')
{
return 3;
}
if(t=='^' || t=='$')
{
return 6;
}
if(t>='a' && t<='z' || t>='A' && t<='Z')
{
return 7;
}
if(t=='(')
{
return 9;
}
if(t==')')
{
return 0;
}
}
int g(char t)
{
if(t=='+' || t=='-')
{
return 2;
}
if(t=='*' || t=='/')
{
return 4;
}
if(t=='^' || t=='$')
{
return 5;
}
if(t>='a' && t<='z' || t>='A' && t<='Z')
{
return 8;
}
if(t=='(')
{
return 0;
}
}
int r(char t)
{
if(t=='+' || t=='-')
{
return -1;
}
if(t=='*' || t=='/')
{
return -1;
}
if(t=='^'|| t=='$')
{
return -1;
}
if(t>='a' && t<='z' || t>='A' && t<='Z')
{
return 1;
}
}
Output
- Enter a infix expression with extra closing bracket:
A$B-C*D+E$F/G)
Valid
Prefix of A$B-C*D+E$F/G) is -$AB+*CD/$EFG
OR
Output
- Enter a infix expression with extra closing bracket:
A$B-C*D+E$F/G)
)G/F$E+D*C-B$A
(G/F$E+D*C-B$A)G (G 0Valid
/ (/ G 1
F (/F G 1
$ (/$ GF 2
E (/$E GF 2
+ (+$E GFE$/ 1
D (+DE GFE$/ 1
* (+*E GFE$/D 2
C (+*C GFE$/D 2
- (-*C GFE$/DC*+ 1
B (-BC GFE$/DC*+ 1
$ (-$C GFE$/DC*+B 2
A (-$A GFE$/DC*+B 2
) GFE$/DC*+BA$- 1
Prefix of A$B-C*D+E$F/G) is -$AB+*CD/$EFG
Comments
Post a Comment