DS Lab Program5
DS Lab Program5
Design, develop and implement a Program in C for the following Stack Applications.
*, /, %, ^.
Program:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<math.h>
float stack[30];
int top=-1;
void main()
{
int ch,n;
char postfix[30];
while(1)
{
printf("Stack Application\n 1. Evaluation of postfix\n
2.Tower of Hanoi\n 3.exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter the postfix expression\n");
scanf("%s",postfix);
eval(postfix);
break;
case 3:exit(0);
else
{
op2=pop();
op1=pop();
switch(postfix[i])
{
case '+': result=op1+op2;
break;
case '-': result=op1-op2;
break;
case '*': result=op1*op2;
break;
case '/': result=op1/op2;
break;
case '%': result=fmod(op1,op2);
break;
case '^': result=pow(op1,op2);
break;
default : printf("Enter a valid operation\n");
}
push(result);
}
i++;
}
if(top==0)
printf("result=%f\n",result);
else
{
printf("invalid expression\n");
exit(0);
}
}
void push(float x)
{
stack[++top]=x;
}
float pop()
{
if(top==-1)
{
printf("Invalid expression\n");
exit(0);
}
return(stack[top--]);
}
Output1:
Output2:
Output3:
Output4:
Output5:
Output6: