0% found this document useful (0 votes)
2 views7 pages

23bcs139_lab_2

The document contains three C programs that utilize stack data structures. The first program reads an expression and prints the precedence of its operators, the second converts an infix expression to postfix, and the third evaluates a given postfix expression. Each program includes relevant functions for stack operations and operator precedence handling.

Uploaded by

chauhanyuvansh26
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)
2 views7 pages

23bcs139_lab_2

The document contains three C programs that utilize stack data structures. The first program reads an expression and prints the precedence of its operators, the second converts an infix expression to postfix, and the third evaluates a given postfix expression. Each program includes relevant functions for stack operations and operator precedence handling.

Uploaded by

chauhanyuvansh26
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/ 7

Lab 2

Name : Yuvansh Chauhan


Roll no : 23bcs139
Date : 25 jan, 2024

1. Write a C program to read an expression and print the precedence of


the operators present in it using the stack data structure.

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

#define size 100

int isEmpty(int top)


{
if (top == -1)
return 1;
else
return 0;
}

int isFull(int top)


{
if (top == size - 1)
return 1;
else
return 0;
}

int push(char stack[], int top, char data)


{
if (isFull(top) == 0)
{
stack[++top] = data;
return top;
}
else
{
printf("Stack Full.\n");
return top;
}
}

char pop(char stack[], int *top)


{
char deleted;
if (isEmpty(*top) == 0)
{
deleted = stack[(*top)--];
return deleted;
}
else
{
printf("Stack Empty.\n");
return '\0'; // '\0' indicates an empty stack
}
}

int precedence(char operator)


{
switch (operator)
{
case '/':
case '*':
case '%':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}

void printprecedence(char expression[])


{
char stack[size];
int top = -1;

printf("Operator precedence: \n");

for (int i = 0; expression[i] != '\0'; i++)


{
if (expression[i] == '+' || expression[i] == '-' || expression[i] == '/' || expression[i] == '*'
|| expression[i] == '%')
{
while (isEmpty(top) == 0 && precedence(expression[i]) <= precedence(stack[top]))
{
printf("%c : %d\n", pop(stack, &top), precedence(stack[top]));
}
top = push(stack, top, expression[i]);
}
else if (expression[i] == '(')
{
top = push(stack, top, expression[i]);
}
else if (expression[i] == ')')
{
while (isEmpty(top) == 0 && stack[top] != '(')
{
printf("%c : %d\n", pop(stack, &top), precedence(stack[top]));
}
pop(stack, &top); // Pop '('
}
}

while (isEmpty(top) == 0)
{
printf("%c : %d\n", pop(stack, &top), precedence(stack[top]));
}
}

int main(void)
{
char expression[size];

printf("Enter an expression : ");


scanf("%s", expression);

printprecedence(expression);

return 0;
}
2. Write a C program to convert a given infix expression to its postfix
equivalent using the stack.

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

char stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;

while(*e != '\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c",pop());
}return 0;
}
3. Using stack data structure, write a C program to evaluate postfix
expression provided by the user.

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

#define MAX_SIZE 100

// Function to initialize the stack


void initialize(int* top) {
*top = -1;
}

// Function to push an element onto the stack


void push(int* stack, int* top, int value) {
if (*top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
exit(EXIT_FAILURE);
}
stack[++(*top)] = value;
}

// Function to pop an element from the stack


int pop(int* stack, int* top) {
if (*top == -1) {
printf("Stack Underflow\n");
exit(EXIT_FAILURE);
}
return stack[(*top)--];
}

// Function to check if a character is an operator


int isOperator(char ch) {
return (ch == '+' || ch == '-' || ch == '*' || ch == '/');
}

// Function to evaluate a postfix expression


int evaluatePostfix(char* expression) {
int stack[MAX_SIZE];
int top;
initialize(&top);
for (int i = 0; expression[i]; ++i) {
if (isdigit(expression[i])) {
push(stack, &top, expression[i] - '0');
} else if (isOperator(expression[i])) {
int operand2 = pop(stack, &top);
int operand1 = pop(stack, &top);

switch (expression[i]) {
case '+':
push(stack, &top, operand1 + operand2);
break;
case '-':
push(stack, &top, operand1 - operand2);
break;
case '*':
push(stack, &top, operand1 * operand2);
break;
case '/':
push(stack, &top, operand1 / operand2);
break;
default:
printf("Invalid operator\n");
exit(EXIT_FAILURE);
}
}
}

return pop(stack, &top);


}

int main() {
char expression[MAX_SIZE];

printf("Enter a postfix expression: ");


scanf("%s", expression);

int result = evaluatePostfix(expression);

printf("Result: %d\n", result);

return 0;
}

You might also like