...::: Recent Updates :::...

Sunday, March 20, 2011

infix to postfix conversion and evollution

/* Maulin Desai
Infix To postfix conversion and evollution
Any error please cheack and mail me :)
used opeartor are
$,*,/,+,-,%
LICENCE:- GNU/GPL :) :)
MCA II sem
*/
#include
#include
#include
int top=-1;
int cnt;
int power(int a,int b)
{
int i,t=1;
for(i=0 ;i lessthan b ;)
{
t=t*a;
i++;
}
return t;
}
int priority(char t)
{
if(t=='$')
return(3);
else if(t=='^')
return(4);
else if(t=='*' || t=='/'|| t=='%')
return(5);
else if(t=='+'||t=='-')
return(6);
else
return 10;
}
void main()
{
char input[80],stack[80],postfix[80],evo[80],t;
void push(char ,char *);
char pop(char *);
int priority(char);
int i,a,b;
printf("\nEnter Prefix String:-\t");
scanf("%s",&input);
for(i=0;input[i]!='\0';increment)
{
if((priority(input[i]))==10)
{
if(input[i]=='(')
{
push(input[i],stack);
}
else if(input[i]==')')
{
while(stack[top]!='(')
{
postfix[cnt]=stack[top];
cnt++;
top--;
}
top--;
}
else
{
postfix[cnt]=input[i];
cnt++;
}

}
else if(top==-1)
{
push(input[i],stack);
}
else if((priority(input[i]))<(priority(*(stack+top))))
{
push(input[i],stack);
}
else if((priority(input[i]))>=(priority(*(stack+top))))
{
while((priority(input[i]))>=(priority(*(stack+top))))
{
postfix[cnt]=pop(stack);
cnt++;
}
push(input[i],stack);
}
}
while(top!=-1)
{
postfix[cnt]=pop(stack);
cnt++;
}
postfix[cnt]='\0';
printf("\n\nPostfix is:-\n");
puts(postfix);
top=-1;
for(i=0;postfix[i]!='\0';increment)
{
printf("\nvalue of top is\t%d",top);
if(priority(postfix[i])==10)
{
push(postfix[i],evo);
}
else
{
b=pop(evo)-48;
a=pop(evo)-48;
switch(postfix[i])
{
case '+':
push((a+b)+48,evo);
break;
case '-':
push((a-b)+48,evo);
break;
case '^':
break;
case '$':
printf("\npush vale :-\t%d",power(a,b));
push((power(a,b))+48,evo);
break;
case '*' :
push((a*b)+48,evo);
break;
case '/':
push((a/b)+48,evo);
break;
case '%':
push((a%b)+48,evo);
break;
}
}
}
printf("\n\nAns is:-\t%d",pop(evo)-48);
}
void push(char input,char *stack)
{
top=top+1;
stack[top]=input;
}
char pop(char *stack)
{
char k;
k=*(stack+top);
top=top-1;
return k;
}

2 comments:

  1. Hey maulin..
    ur previous coment has deleted bcoz in dat post of debashish.. der was some mistakes...

    ReplyDelete