DSA Postfix To Evaluate
DSA Postfix To Evaluate
MIS -112415033
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
typedef struct {
int top;
int items[MAX];
} Stack;
switch (token[0]) {
case '+':
push(&s, operand1 + operand2);
break;
case '-':
push(&s, operand1 - operand2);
break;
case '*':
push(&s, operand1 * operand2);
break;
case '/':
if (operand2 == 0) {
printf("Division by zero error\n");
exit(1);
}
push(&s, operand1 / operand2);
break;
default:
printf("Invalid operator encountered: %s\n", token);
exit(1);
}
}
token = strtok(NULL, " ");
}
return pop(&s);
}
int main() {
char expression[MAX];
printf("Enter a postfix expression ");
fgets(expression, MAX, stdin);
return 0;
}
OUTPUT:-
Enter a postfix expression 2 3 4 * +
Result: 14