Wednesday 22 August 2012

Conversion from infix to postfix and prefix expression | priority method and evaluation of postfix and prefix expression | Output

conversion from infix to postfix and prefix expression | priority method and evaluation of
postfix and prefix expression | Output
/* How to  convert from infix to postfix and prefix expression
by priority method and evaluation of postfix and prefix expression */

#include<stdio.h>
#include<conio.h>
#define max 50

typedef struct stack
{
int top;
char item[max];
}stack;
stack s1;

void push(char n)
{
if(s1.top==max-1)
puts("\nstack is full");
else
s1.item[++s1.top]=n;
}

int pop()
{
if(s1.top==-1)
return s1.top;
else
return(s1.item[s1.top--]);
}

int prio(char ch)
{
int sh;
if(ch=='^' || ch=='$')
sh=3;
else if(ch=='*' || ch=='/')
sh=2;
else if(ch=='+' || ch=='-')
sh=1;
else if(ch=='{' || ch=='(' || ch=='[')
sh=0;
return sh;
}

void in_post()
{
int i,j=0;
char inf[20],post[20],ch1;
printf("\nenter infix expression:- ");
scanf("%s",&inf);
for(i=0;inf[i]!='\0';i++)
{
if(inf[i]=='{' || inf[i]=='[' || inf[i]=='(')
push(inf[i]);
else if((inf[i]>=65 && inf[i]<91) || (inf[i]>=97 && inf[i]<123))
post[j++]=inf[i];
else if(inf[i]=='^' || inf[i]=='$' || inf[i]=='+' || inf[i]=='-' || inf[i]=='*' || inf[i]=='/')
{
if(s1.top==-1)
push(inf[i]);
else
{
if(prio(inf[i])>prio(s1.item[s1.top]))
push(inf[i]);
else
{
while(prio(inf[i])<=prio(s1.item[s1.top]))
{
post[j++]=pop();
if(s1.top==-1)
break;
}
push(inf[i]);
}
}
}
else if(inf[i]==')'||inf[i]=='}'||inf[i]==']')
{
while(s1.item[s1.top]!='(' && s1.item[s1.top]!='[' && s1.item[s1.top]!='{')
post[j++]=pop();
pop();
}
}
if(s1.top!=-1)
{
while(s1.top!=-1)
post[j++]=pop();
}
post[j]='\0';
printf("\npostfix expression is:- %s",post);
}

int prio1(char ch)
{
int sh;
if(ch=='^' || ch=='$')
sh=3;
if(ch=='*' || ch=='/')
sh=2;
if(ch=='+' || ch=='-')
sh=1;
if(ch=='}' || ch==')' || ch==']')
sh=0;
return sh;
}

void in_pre()
{
int i,j=0;
char inf[30],pre[30],ch1;
printf("\nenter infix expression:- ");
scanf("%s",&inf);
for(i=strlen(inf);i>=0;i--)
{
if(inf[i]=='}' || inf[i]==']' || inf[i]==')')
push(inf[i]);
else if((inf[i]>=65 && inf[i]<91) || (inf[i]>=97 && inf[i]<123))
pre[j++]=inf[i];
else if(inf[i]=='^' || inf[i]=='$' || inf[i]=='+' || inf[i]=='-' || inf[i]=='*' || inf[i]=='/')
{
if(s1.top==-1)
push(inf[i]);
else
{
if(prio1(inf[i])>=prio1(s1.item[s1.top]))
push(inf[i]);
else
{
while(prio1(inf[i])<prio1(s1.item[s1.top]))
{
pre[j++]=pop();
if(s1.top==-1)
break;
}
push(inf[i]);
}
}
}
else if(inf[i]=='{'||inf[i]=='('||inf[i]=='[')
{
while(s1.item[s1.top]!=')' && s1.item[s1.top]!=']' && s1.item[s1.top]!='}')
pre[j++]=pop();
pop();
}
}
if(s1.top!=-1)
{
while(s1.top!=-1)
pre[j++]=pop();
}
pre[j]='\0';
printf("\nprefix expression is:- %s",strrev(pre));
}

void eval_post()
{
int a,b,c,d,e,i,op1,op2;
char pos[30];
printf("\nenter the value of a,b,c,d,e:- ");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("\nenter postfix expression:- ");
scanf("%s",&pos);
for(i=0;pos[i]!='\0';i++)
{
switch(pos[i])
{
case 'a':
push(a);
break;
case 'b':
push(b);
break;
case 'c':
push(c);
break;
case 'd':
push(d);
break;
case 'e':
push(e);
break;
default:
op2=pop();
op1=pop();
}
switch(pos[i])
{
case '+':
push(op1+op2);
break;
case '-':
push(op1-op2);
break;
case '*':
push(op1*op2);
break;
case '/':
push(op1/op2);
break;
case '%':
push(op1%op2);
break;
}
}
printf("\nvalue of postfix expression is:- %d",pop());
}

void eval_pre()
{
int a,b,c,d,e,i,op1,op2,l;
char pre[30];
printf("\nenter the value of a,b,c,d,e:- ");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("\nenter prefix expression:- ");
scanf("%s",&pre);
l=strlen(pre);
for(i=l;i>=0;i--)
{
switch(pre[i])
{
case 'a':
push(a);
break;
case 'b':
push(b);
break;
case 'c':
push(c);
break;
case 'd':
push(d);
break;
case 'e':
push(e);
break;
default:
op1=pop();
op2=pop();
break;
}
switch(pre[i])
{
case '+':
push(op1+op2);
break;
case '-':
push(op1-op2);
break;
case '*':
push(op1*op2);
break;
case '/':
push(op1/op2);
break;
case '%':
push(op1%op2);
break;
}
}
printf("\nvalue of prefix expression is:- %d",pop());
}

int menu()
{
int ch;
s1.top=-1;
printf("\n\n1:for conversion from infix to postfix expression");
printf("\n2:for conversion from infix to prefix expression");
printf("\n3:for evaluation of postfix expression");
printf("\n4:for evaluation of prefix expression");
printf("\n5:for exit");
printf("\nenter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
in_post();
menu();
break;
case 2:
in_pre();
menu();
break;
case 3:
eval_post();
menu();
break;
case 4:
eval_pre();
menu();
break;
case 5:
exit(0);
default:
printf("\nyou entered wrong choice");
menu();
}
return 0;
}

void main()
{
clrscr();
menu();
getch();
}
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: