Program:
#include<stdio.h>
int F(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 1;
case '*':
case '/':return 3;
case '^':
case '$':return 6;
case ')':return 0;
case '#':return -1;
default:return 8;
}
}
int G(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '^':
case '$':return 5;
case '(':return 0;
case ')':return 9;
default:return 7;
}
}
void infix_postfix(char infi[],char postfi[])
{
char s[30];
int top=-1;
char symbol;
int i,j;
s[++top]='#';
j=0;
for(i=0;i<strlen(infi);i++)
{
symbol=infi[i];
while(F(s[top])>G(symbol))
{
postfi[j++]=s[top--];
}
if(F(s[top])<G(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top]!='#')
{
postfi[j++]=s[top--];
}
postfi[j]='\0';
}
void main()
{
char infix[30];
char postfix[30];
printf("enter the infix expression \n");
scanf("%s",infix);
infix_postfix(infix,postfix);
printf("Postfix expression is \n");
printf("%s \n",postfix);
}
#include<stdio.h>
int F(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 1;
case '*':
case '/':return 3;
case '^':
case '$':return 6;
case ')':return 0;
case '#':return -1;
default:return 8;
}
}
int G(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '^':
case '$':return 5;
case '(':return 0;
case ')':return 9;
default:return 7;
}
}
void infix_postfix(char infi[],char postfi[])
{
char s[30];
int top=-1;
char symbol;
int i,j;
s[++top]='#';
j=0;
for(i=0;i<strlen(infi);i++)
{
symbol=infi[i];
while(F(s[top])>G(symbol))
{
postfi[j++]=s[top--];
}
if(F(s[top])<G(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top]!='#')
{
postfi[j++]=s[top--];
}
postfi[j]='\0';
}
void main()
{
char infix[30];
char postfix[30];
printf("enter the infix expression \n");
scanf("%s",infix);
infix_postfix(infix,postfix);
printf("Postfix expression is \n");
printf("%s \n",postfix);
}
No comments:
Post a Comment