Wednesday 22 August 2012

prefix evaluation | program source code for prefix evaluation

Data structure program | prefix evaluation |  source code
/* Program for prefix evaluation. */

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

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

void push(int 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--]);
}

void main()
{
int a,b,c,d,e,i,op1,op2,l;
char pre[30];
s1.top=-1;
clrscr();
puts("enter 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());
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: