0% found this document useful (0 votes)
40 views

DS Lab Program5

The document describes a C program that implements two stack applications: 1) evaluating postfix expressions with single digit operands and basic math operators, and 2) solving the Tower of Hanoi problem for a given number of disks. The program uses functions to push and pop values onto a stack, evaluate postfix expressions by popping operands and pushing results, and recursively move disks in the Tower of Hanoi problem. It provides a menu for the user to choose which application to run and inputs like the postfix expression or number of disks.

Uploaded by

rtf47507
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

DS Lab Program5

The document describes a C program that implements two stack applications: 1) evaluating postfix expressions with single digit operands and basic math operators, and 2) solving the Tower of Hanoi problem for a given number of disks. The program uses functions to push and pop values onto a stack, evaluate postfix expressions by popping operands and pushing results, and recursively move disks in the Tower of Hanoi problem. It provides a menu for the user to choose which application to run and inputs like the postfix expression or number of disks.

Uploaded by

rtf47507
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab Program 5

Design, develop and implement a Program in C for the following Stack Applications.

a) Evaluation of Suffix expression with single digit operands and operators: +, -,

*, /, %, ^.

b) Solving Tower of Hanoi problem with n disks

Program:

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<math.h>

void towers(int, char, char, char);


void eval(char postfix[30]);
void push(float op);
float pop();

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 2:printf("Enter the number of disk\n");


scanf("%d",&n);
towers(n,'A','B','C');
break;

case 3:exit(0);

default: printf("Enter the valid choice\n");


}
}
}

void towers(int n, char A, char B, char C)


{
if(n==1)
{
printf("disk 1 is moved from peg %c to peg %c\n", A, C);
return;
}
towers(n-1, A, C, B);
printf("Disk %d is moved from peg %c to peg %c\n",n, A, C);
towers(n-1, B, A, C);
}
void eval(char postfix[30])
{
float result,op1,op2;
int i=0;
while(postfix[i]!='\0')
{
if(isdigit(postfix[i]))
push(postfix[i]-'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:

You might also like