#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100 // Maximum size of stack
// Global stack and top index
int stack[MAX];
int top = -1;
// Function to push an element onto the
stack
void push(int value) {
if (top < MAX - 1) {
top++;
stack[top] = value;
} else {
printf("Stack overflow\n");
}
}
// Function to pop an element from the
stack
int pop() {
if (top >= 0) {
int value = stack[top];
top--;
return value;
} else {
printf("Stack underflow\n");
return -1; // Return -1 for underflow
}
}
// Function to perform the operation
int performOperation(int operand1, int
operand2, char operation) {
switch (operation) {
case '+':
return operand1 + operand2;
case '-':
return operand1 - operand2;
case '*':
return operand1 * operand2;
case '/':
if (operand2 != 0) {
return operand1 / operand2; //
Check for division by zero
} else {
printf("Error: Division by
zero\n");
return 0; // Return 0 for
division by zero
}
default:
return 0; // Return 0 for unknown
operation
}
}
// Function to evaluate the prefix
expression
int evaluatePrefixExpression(char*
expression) {
int length = strlen(expression);
// Iterate from right to left (reverse)
for (int i = length - 1; i >= 0; i--) {
char c = expression[i];
if (isdigit(c)) {
// Convert char digit to int and
push onto the stack
push(c - '0');
} else {
// Pop the top two elements for the
operation
int operand1 = pop();
int operand2 = pop();
// Perform the operation and push
the result back onto the stack
int result =
performOperation(operand1, operand2, c);
push(result);
}
}
// The final result should be the only item left in the stack
return pop();
}
int main() {
char expression[MAX];
// Ask the user for input
printf("Enter a prefix expression: ");
scanf("%s", expression);
int result = evaluatePrefixExpression(expression);
printf("Result of Prefix Expression: %d\n", result);
return 0;
}
Method2
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100 // Maximum size of stack
// Stack structure
struct Stack {
int data[MAX];
int top;
};
// Function to initialize the stack
void initializeStack(struct Stack* stack) {
stack->top = -1;
}
// Function to push an element onto the stack
void push(struct Stack* stack, int value) {
if (stack->top < MAX - 1) {
stack->top++;
stack->data[stack->top] = value;
} else {
printf("Stack overflow\n");
}
}
// Function to pop an element from the stack
int pop(struct Stack* stack) {
if (stack->top >= 0) {
int value = stack->data[stack->top];
stack->top--;
return value;
} else {
printf("Stack underflow\n");
return -1;
}
}
// Function to perform the operation
int performOperation(int operand1, int operand2, char operation) {
switch (operation) {
case '+':
return operand1 + operand2;
case '-':
return operand1 - operand2;
case '*':
return operand1 * operand2;
case '/':
return operand1 / operand2;
default:
return 0;
}
}
// Function to evaluate the prefix expression
int evaluatePrefixExpression(char* expression) {
struct Stack stack;
initializeStack(&stack);
int length = strlen(expression);
// Iterate from right to left (reverse)
for (int i = length - 1; i >= 0; i--) {
char c = expression[i];
if (isdigit(c)) {
// Convert char digit to int and push onto the stack
push(&stack, c - '0');
} else {
// Pop the top two elements for the operation
int operand1 = pop(&stack);
int operand2 = pop(&stack);
// Perform the operation and push the result back onto the stack
int result = performOperation(operand1, operand2, c);
push(&stack, result);
}
}
// The final result should be the only item left in the stack
return pop(&stack);
}
int main() {
char expression[MAX];
// Ask the user for input
printf("Enter a prefix expression: ");
scanf("%s", expression);
int result = evaluatePrefixExpression(expression);
printf("Result of Prefix Expression: %d\n", result);
return 0;
}