Friday 15 May 2015

Cercular queue insert delete display

/* Cercular queue insert delete display */
#include<stdio.h>
#include<ctype.h>
#include<string.h>
double power(double n1,double n2)
{
double n=n1;
while(n2>1)
{
n1*=n;
n2--;
}
return n1;
}
double op(char symbol,double op1,double op2)
{
double res;
switch(symbol)
{
case '+' : return(op1+op2);
case '-' : return(op1-op2);
case '*' : return(op1*op2);
case '/' : return(op1/op2);
case '$' :
case '^' : res=power(op1,op2);
  return res;
default : printf("Enter proper operation");
}
}
void push(double data,int *top,double s[])
{
(*top)++;
s[*top]=data;
}
double pop(int *top,double s[])
{
double t;
t=s[*top];
(*top)--;
return t;
}
int main()
{
double s[20],res,op1,op2;
int top=-1,i;
char post[20],symbol;
printf("Enter the postfix expression");
scanf("%s",post);
for(i=0;i<strlen(post);i++)
{
symbol=post[i];
if(isdigit(symbol))
push(symbol-'0',&top,s);
else
{
op2=pop(&top,s);
op1=pop(&top,s);
res=op(symbol,op1,op2);
push(res,&top,s);
}
}
res=pop(&top,s);
printf("The result = %f",res);
}

No comments:

Post a Comment