0% found this document useful (0 votes)
5 views2 pages

Dslab Manual

The document contains two C programs: one for evaluating a postfix expression using a stack with single digit operands and basic arithmetic operators, and another for solving the Tower of Hanoi problem with a specified number of disks. The postfix evaluation program reads an expression, processes it using a stack, and outputs the result. The Tower of Hanoi program recursively moves disks between pegs and prints the moves required to solve the puzzle.

Uploaded by

umairabdulbari0
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)
5 views2 pages

Dslab Manual

The document contains two C programs: one for evaluating a postfix expression using a stack with single digit operands and basic arithmetic operators, and another for solving the Tower of Hanoi problem with a specified number of disks. The postfix evaluation program reads an expression, processes it using a stack, and outputs the result. The Tower of Hanoi program recursively moves disks between pegs and prints the moves required to solve the puzzle.

Uploaded by

umairabdulbari0
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/ 2

Program 5: Evaluation of Postfix Expression

Develop a Program in C for the following Stack Applications:


a. Evaluate a Suffix-expression with single digit operands and operators: +, -, *, /, %, ^.
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
char postfix[20];
int result, i, s[20], op1,op2, top=-1;
printf("\nEnter the postfix expression:");
scanf("%s",postfix);
for(i=0;i<strlen(postfix);i++)
{
if(isdigit(postfix[i])) // if operand push on stack

s[++top]=postfix[i]-'0';
else
{
op2=s[top--];
op1=s[top--];
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=op1%op2;
break;
case '^':result=pow(op1,op2);
break;
}
s[++top]=result;

}
}
printf("\nResult=%d",result);
}

Output:
b. Solving Tower of Hanoi problem with n disks.

#include<stdio.h>
tower(int n, char s, char d, char t)
{
if(n==0)
return;

tower(n-1,s,t,d);
printf("\n Move Disc %d from %c to %c", n,s,d);
tower(n-1,t,d,s);

int main()
{
int n;
printf("\nEnter no of discs\n");
scanf("%d",&n);
tower(n,'A','C','B');
}

Output:

You might also like