#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
// Define a stack structure
struct Stack {
int top;
char items[MAX_SIZE];
};
// Function to initialize an empty stack
void initialize(struct Stack *s) {
s->top = -1;
}
// Function to check if the stack is empty
int isEmpty(struct Stack *s) {
return s->top == -1;
}
// Function to push an element onto the stack
void push(struct Stack *s, char item) {
if (s->top == MAX_SIZE - 1) {
}
s->items[++(s->top)] = item;
}
// Function to pop an element from the stack
char pop(struct Stack *s) {
if (isEmpty(s)) {
}
return s->items[(s->top)--];
}
// Function to check precedence of operators
int precedence(char operator) {
if (operator == '+' || operator == '-') {
return 1;
} else if (operator == '*' || operator == '/') {
return 2;
}
return 0;
}
// Function to convert infix expression to postfix notation
void infixToPostfix(char infix[], char postfix[]) {
struct Stack stack;
initialize(&stack);
int i, j;
i = j = 0;
while (infix[i] != '\0') {
char token = infix[i];
if (token >= 'a' && token <= 'z') {
postfix[j++] = token;
} else if (token == '(') {
push(&stack, token);
} else if (token == ')') {
while (!isEmpty(&stack) && stack.items[stack.top] != '(') {
postfix[j++] = pop(&stack);
}
if (!isEmpty(&stack) && stack.items[stack.top] == '(') {
pop(&stack); // Pop the '('
}
} else {
while (!isEmpty(&stack) && precedence(token) <= precedence(stack.items[stack.top])) {
postfix[j++] = pop(&stack);
}
push(&stack, token);
}
i++;
}
while (!isEmpty(&stack)) {
postfix[j++] = pop(&stack);
}
postfix[j] = '\0';
}
int main() {
char infix[MAX_SIZE] = "x+y"; // Predefined infix expression
char postfix[MAX_SIZE];
infixToPostfix(infix, postfix);
printf("Postfix: %s\n", postfix
);
return 0;
}