Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.9 kB
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define MAX_STACK_SIZE 100

// Stack implementation
struct Stack {
    int top;
    int items[MAX_STACK_SIZE];
};

void initializeStack(struct Stack* stack) {
    stack->top = -1;
}

int isEmpty(struct Stack* stack) {
    return stack->top == -1;
}

int isFull(struct Stack* stack) {
    return stack->top == MAX_STACK_SIZE - 1;
}

void push(struct Stack* stack, int item) {
    if (isFull(stack)) {
        printf("Stack Overflow\n");
    } else {
        stack->items[++stack->top] = item;
    }
}

int pop(struct Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack Underflow\n");
        return -1;
    } else {
        return stack->items[stack->top--];
    }
}

int peek(struct Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack Underflow\n");
        return -1;
    } else {
        return stack->items[stack->top];
    }
}

// Function to evaluate a postfix expression
int evaluatePostfix(char* expression) {
    struct Stack stack;
    initializeStack(&stack);

    int i, op1, op2, result;
    for (i = 0; expression[i] != '\0'; i++) {
        if (isdigit(expression[i])) {
            push(&stack, expression[i] - '0');
        } else {
            op2 = pop(&stack);
            op1 = pop(&stack);

            switch (expression[i]) {
                case '+':
                    result = op1 + op2;
                    break;
                case '-':
                    result = op1 - op2;
                    break;
                case '*':
                    result = op1 * op2;
                    break;
                case '/':
                    result = op1 / op2;
                    break;
                default:
                    printf("Invalid Operator\n");
                    return -1;
            }

            push(&stack, result);
        }
    }

    return pop(&stack);
}

// Function to evaluate a prefix expression
int evaluatePrefix(char* expression) {
    struct Stack stack;
    initializeStack(&stack);

    int i, op1, op2, result;
    int len = strlen(expression);

    // Read the expression from right to left
    for (i = len - 1; i >= 0; i--) {
        if (isdigit(expression[i])) {
            push(&stack, expression[i] - '0');
        } else {
            op1 = pop(&stack);
            op2 = pop(&stack);

            switch (expression[i]) {
                case '+':
                    result = op1 + op2;
                    break;
                case '-':
                    result = op1 - op2;
                    break;
                case '*':
                    result = op1 * op2;
                    break;
                case '/':
                    result = op1 / op2;
                    break;
                default:
                    printf("Invalid Operator\n");
                    return -1;
            }

            push(&stack, result);
        }
    }

    return pop(&stack);
}

int main() {
    char expression[100];
    int choice, result;
    loop:
    printf("Menu:\n");
    printf("1. Evaluate Postfix Expression\n");
    printf("2. Evaluate Prefix Expression\n");
    printf("3. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("Enter the Postfix Expression: ");
            scanf("%s", expression);
            result = evaluatePostfix(expression);
            printf("Result: %d\n", result);
            goto loop;
            break;
        case 2:
            printf("Enter the Prefix Expression: ");
            scanf("%s", expression);
            result = evaluatePrefix(expression);
            printf("Result: %d\n", result);
            goto loop;
            break;
        case 3:
            exit(0);
            break;
        default:
            printf("Invalid Choice\n");
            return 1;
    }

    
     
    return 0;
}