kichu's HiDe OUT (JiLs..?)
for every victory of a man der s an any types of pressures.... dese r MinE....
My OwN
Home
My owN page.....
NeW PaGe
Tuesday, March 26, 2013
Prefix Evaluation
Prefix Evaluation.
#include
#include
#include
#include
int top=-1;
int pop(int s[])
{
int a;
a=s[top];
top=top-1;
return a;
}
int push(int c,int s[])
{
top=top+1;
s[top]=c;
return 0;
}
int operation(int a,int b,char ch)
{
switch(ch)
{
case '+':return(a+b);
case '-':return(a-b);
case '*':return(a*b);
case '/':return(a/b);
case '^':return(pow(a,b));
default:return(0);
}
}
void main()
{
int i,len,s[50],a,b,c;
char p[50],ch;
clrscr();
printf("Enter expression:\n");
gets(p);
len=strlen(p);
for(i=len-1;i>=0;i--)
{
ch=p[i];
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^')
{
a=pop(s);
b=pop(s);
c=operation(a,b,ch);
push(c,s);
}
else
{
c=atoi(&ch);
push(c,s);
}
}
printf("Result: %d",pop(s));
getch();
}
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment