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

ds_exp_3

The document contains a C program that evaluates postfix expressions using a stack data structure. It defines functions for stack operations such as push, pop, and initialization, as well as for evaluating the postfix expression. The program prompts the user for input and displays the result of the evaluation.
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)
6 views

ds_exp_3

The document contains a C program that evaluates postfix expressions using a stack data structure. It defines functions for stack operations such as push, pop, and initialization, as well as for evaluating the postfix expression. The program prompts the user for input and displays the result of the evaluation.
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/ 4

‭ ame: Atharva Deepak Manjrekar‬

N
‭Class : Second Year‬
‭Roll no: 66 Div: B‬
‭Course Code: CEC303‬

‭Experiment no: 3‬

‭CODE‬‭:‬

‭ include <stdio.h>‬
#
‭#include <stdlib.h>‬
‭#include <ctype.h>‬
‭#include <string.h>‬

‭#define MAX 100‬

‭typedef struct {‬
‭int top;‬
‭int items[MAX];‬
‭} Stack;‬

‭void initStack(Stack *s) {‬


‭s->top = -1;‬
‭}‬

‭int isEmpty(Stack *s) {‬


‭return s->top == -1;‬
‭}‬

‭void push(Stack *s, int item) {‬


‭if (s->top < MAX - 1) {‬
‭s->items[++(s->top)] = item;‬
‭} else {‬
‭printf("Stack overflow\n");‬
‭exit(EXIT_FAILURE);‬
‭}‬
‭}‬

‭int pop(Stack *s) {‬


‭if (!isEmpty(s)) {‬
‭return s->items[(s->top)--];‬
‭} else {‬
‭printf("Stack underflow\n");‬
‭exit(EXIT_FAILURE);‬
‭}‬
‭}‬

‭int evaluatePostfix(const char *expression) {‬


‭Stack s;‬
‭initStack(&s);‬

‭const char *p = expression;‬

‭ hile (*p) {‬
w
‭if (isspace(*p)) {‬
‭p++;‬
‭continue;‬
‭}‬

i‭f (isdigit(*p)) {‬
‭push(&s, *p - '0');‬
‭} else {‬
‭if (s.top < 1) {‬
‭printf("Error: Not enough operands for operator '%c'\n", *p);‬
‭exit(EXIT_FAILURE);‬
‭}‬

i‭nt op2 = pop(&s);‬


‭int op1 = pop(&s);‬
‭switch (*p) {‬
‭case '+':‬
‭push(&s, op1 + op2);‬
‭break;‬
‭case '-':‬
‭push(&s, op1 - op2);‬
‭ reak;‬
b
‭case '*':‬
‭push(&s, op1 * op2);‬
‭break;‬
‭case '/':‬
‭if (op2 == 0) {‬
‭printf("Error: Division by zero\n");‬
‭exit(EXIT_FAILURE);‬
‭}‬
‭push(&s, op1 / op2);‬
‭break;‬
‭default:‬
‭printf("Invalid operator: %c\n", *p);‬
‭exit(EXIT_FAILURE);‬
}‭ ‬
‭}‬
‭p++;‬
‭}‬

i‭f (isEmpty(&s)) {‬
‭printf("Error: No result on the stack\n");‬
‭exit(EXIT_FAILURE);‬
‭}‬

‭return pop(&s);‬
‭}‬

‭int main() {‬
‭char postfix[MAX];‬

‭ rintf("Enter a postfix expression: ");‬


p
‭fgets(postfix, MAX, stdin);‬

s‭ ize_t len = strlen(postfix);‬


‭if (len > 0 && postfix[len - 1] == '\n') {‬
‭postfix[len - 1] = '\0';‬
‭}‬

i‭nt result = evaluatePostfix(postfix);‬


‭printf("The result of the postfix expression '%s' is: %d\n", postfix, result);‬
‭return 0;‬
‭}‬

‭OUTPUT:‬

You might also like