0% found this document useful (0 votes)
30 views3 pages

6

This C program converts an infix expression to postfix notation using a stack data structure. It includes functions for stack operations, checking precedence of operators, and the main logic for the conversion process. The user is prompted to enter an infix expression, which is then processed and the resulting postfix expression is displayed.

Uploaded by

swetha.v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

6

This C program converts an infix expression to postfix notation using a stack data structure. It includes functions for stack operations, checking precedence of operators, and the main logic for the conversion process. The user is prompted to enter an infix expression, which is then processed and the resulting postfix expression is displayed.

Uploaded by

swetha.v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>
#include <ctype.h>

#define MAX 100

typedef struct {
int top;
char items[MAX];
} Stack;

void initStack(Stack *s);


int isEmpty(Stack *s);
int isFull(Stack *s);
void push(Stack *s, char item);
char pop(Stack *s);
char peek(Stack *s);
int precedence(char op);
void infixToPostfix(char *infix, char *postfix);

int main() {
char infix[MAX], postfix[MAX];

printf("Enter an infix expression (e.g., A+B*C): ");


// Read input from keyboard
fgets(infix, MAX, stdin);

// Remove newline character if present


size_t length = strlen(infix);
if (length > 0 && infix[length - 1] == '\n') {
infix[length - 1] = '\0';
}

infixToPostfix(infix, postfix);

printf("Postfix expression: %s\n", postfix);

return 0;
}

// Initialize stack
void initStack(Stack *s) {
s->top = -1;
}

// Check if stack is empty


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

// Check if stack is full


int isFull(Stack *s) {
return s->top == (MAX - 1);
}

// Push item to stack


void push(Stack *s, char item) {
if (isFull(s)) {
printf("Stack is full\n");
exit(1);
}
s->items[++(s->top)] = item;
}

// Pop item from stack


char pop(Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty\n");
exit(1);
}
return s->items[(s->top)--];
}

char peek(Stack *s) {


if (isEmpty(s)) {
printf("Stack is empty\n");
exit(1);
}
return s->items[s->top];
}

int precedence(char op) {


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

void infixToPostfix(char *infix, char *postfix) {


Stack s;
initStack(&s);

int i = 0, k = 0;
char ch;

while ((ch = infix[i++]) != '\0') {


if (isalnum(ch)) {
postfix[k++] = ch;
} else if (ch == '(') {
push(&s, ch);
} else if (ch == ')') {
while (!isEmpty(&s) && peek(&s) != '(') {
postfix[k++] = pop(&s);
}
pop(&s); // Remove '(' from stack
} else {
while (!isEmpty(&s) && precedence(peek(&s)) >= precedence(ch)) {
postfix[k++] = pop(&s);
}
push(&s, ch);
}
}

while (!isEmpty(&s)) {
postfix[k++] = pop(&s);
}
postfix[k] = '\0';
}

You might also like