Lab 5
Lab 5
Lab (BCSL404)
4th Sem – AI-ML
By,
Vandana U
Asst. Professor
Dept. of AI-DS
Develop 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.
This can be the part of the
presentation where you introduce
yourself, write your email…
This can be the part of the
presentation where you introduce
yourself, write your email…
#include <stdio.h>
Function to calculate the value of
#include<ctype.h> postfix expression
#include<math.h>
#include<string.h>
float compute(char symbol,float op1,float op2)
{ switch(symbol)
{
case '+': return op1+op2;
break;
case '-': return op1-op2;
break;
case '*': return op1*op2;
break; Power is denoted as ^
case '/': return op1/op2;
break;
case ‘^': return pow(op1,op2);
break;
default: return 0; }}
void main() {
float s[20],res,op1,op2;
int top,i;
char postfix[20],symbol;
printf("Enter the postfix expression:\n");
scanf("%s",postfix);
Take one value from postfix and assign it to symbol
top=-1;
for(i=0;i<strlen(postfix);i++)
Check whether the entered symbol is a digit. If so, take the
{ symbol=postfix[i];
symbol and find its equivalent ASCII value and assign it to
if(isdigit(symbol)) the top of the stack.
s[++top]=symbol-'0'; Ex: ASCII value of 5 is 53 and 0 is 48 ,
}
Once, we finish the calculation for
res=s[top--]; all the values, pop out the element
printf("The result is %f",res); left in the stack and display that as
the result.
}
B) Towers of Hanoi :
Solving Tower of Hanoi problem with n disks. The Tower of Hanoi is a
mathematical game or puzzle. It consists of three rods, and a number of disks of
different sizes which can slide onto any rod. The puzzle starts with the disks in a
neat stack in ascending order of size on one rod, the smallest at the top, thus
making a conical shape.
The objective of the puzzle is to move the entire stack to another rod,
obeying the following simple rules:
- Only one disk can be moved at a time.
- Each move consists of taking the upper disk from one of the stacks and
placing it on top of another stack i.e. a disk can only be moved if it is the
uppermost disk on a stack.
- No disk may be placed on top of a smaller disk. With three disks, the
puzzle can be solved in seven moves. The minimum number of moves
required to solve a Tower of Hanoi puzzle is 2 n - 1, where n is the number
of disks
ALGORITHM:
Step 1: Start.
Step 2: Read N number of discs.
Step 3: Move all the discs from source to destination by using temp rod.
Step 4: Stop.
#include <stdio.h> If number of disc
tower(1,beg,aux,end); Once the above step is carried out, you will be left with only 1 disc at
source. Now move it from source to destination
tower(n-1,aux,beg,end);
Now move the remaining discs present in intermediate to the
}} destination.
void main()
{
int n; Call the tower of Hanoi
function
char a,b,c;
printf("Enter the number of discs:\n");
scanf("%d",&n);
printf("Tower of Hanoi for %d discs has the following steps:\n",n);
tower(n,'a','b','c');
printf("\n\nTotal Number of moves are: %d", (int)pow(2,n)-1);
}