Untitled

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

#define MAX_SIZE 100

typedef struct {
    char data[MAX_SIZE];
    int top;
} Stack;

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

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

int is_full(Stack *stack) {
    return stack->top == MAX_SIZE - 1;
}

void push(Stack *stack, char element) {
    if (is_full(stack)) {
        printf("Stack overflow!\n");
    } else {
        stack->data[++stack->top] = element;
    }
}

char pop(Stack *stack) {
    if (is_empty(stack)) {
        printf("Stack underflow!\n");
        return '\0';
    } else {
        return stack->data[stack->top--];
    }
}

char peek(Stack *stack) {
    if (is_empty(stack)) {
        return '\0';
    } else {
        return stack->data[stack->top];
    }
}

int is_operator(char c) {
    return c == '+' || c == '-' || c == '*' || c == '/' || c == '^';
}

int precedence(char c) {
    switch (c) {
        case '+':
        case '-':
            return 1;
        case '*':
        case '/':
            return 2;
        case '^':
            return 3;
        default:
            return 0;
    }
}

void infix_to_postfix(char infix[], char postfix[]) {
    Stack operator_stack;
    initialize(&operator_stack);
    int i = 0, j = 0;

    while (infix[i] != '\0') {
        char ch = infix[i];
        if (isalnum(ch)) {
            postfix[j++] = ch;
        } else if (ch == '(') {
            push(&operator_stack, ch);
        } else if (ch == ')') {
            while (!is_empty(&operator_stack) && peek(&operator_stack) != '(') {
                postfix[j++] = pop(&operator_stack);
            }
            pop(&operator_stack); // Pop '(' from the stack
        } else if (is_operator(ch)) {
            while (!is_empty(&operator_stack) && precedence(peek(&operator_stack)) >= precedence(ch)) {
                postfix[j++] = pop(&operator_stack);
            }
            push(&operator_stack, ch);
        }
        i++;
    }

    while (!is_empty(&operator_stack)) {
        postfix[j++] = pop(&operator_stack);
    }

    postfix[j] = '\0';
}

void reverse_string(char str[]) {
    int start = 0;
    int end = strlen(str) - 1;

    while (start < end) {
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
}

void infix_to_prefix(char infix[], char prefix[]) {
    reverse_string(infix);
    for (int i = 0; infix[i] != '\0'; i++) {
        if (infix[i] == '(') {
            infix[i] = ')';
        } else if (infix[i] == ')') {
            infix[i] = '(';
        }
    }

    char postfix[MAX_SIZE];
    infix_to_postfix(infix, postfix);

    reverse_string(postfix);
    strcpy(prefix, postfix);
}

void print_box_title(const char *title) {
    int title_length = strlen(title);
    int width = title_length + 4;

    printf("\n");

    // Print the top border
    for (int i = 0; i < width; i++) {
        printf("*");
    }
    printf("\n");

    // Print the title and left/right borders
    printf("* %s *\n", title);

    // Print the bottom border
    for (int i = 0; i < width; i++) {
        printf("*");
    }
    printf("\n");
}

int main() {
    char infix[MAX_SIZE], postfix[MAX_SIZE], prefix[MAX_SIZE];
    int choice;

    while (1) {
        print_box_title("Expression Conversion");
        printf("Menu:\n");
        printf("1. Convert infix to postfix\n");
        printf("2. Convert infix to prefix\n");
        printf("3. Exit\n");
        printf("Enter your choice (1/2/3): ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter the infix expression: ");
                scanf(" %[^\n]", infix);
                infix_to_postfix(infix, postfix);
                print_box_title("Postfix Expression");
                printf("%s\n", postfix);
                break;
            case 2:
                printf("Enter the infix expression: ");
                scanf(" %[^\n]", infix);
                infix_to_prefix(infix, prefix);
                print_box_title("Prefix Expression");
                printf("%s\n", prefix);
                break;
            case 3:
                printf("Exiting the program.\n");
                exit(0);
            default:
                printf("Invalid choice. Please enter a valid option.\n");
        }
    }

    return 0;
}