Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views5 pages

InfixToPostfix (Both)

The document provides a C program that converts infix expressions into postfix notation using two implementations: one with a simple array-based stack and another using a structured stack. It includes functions for stack operations, operator precedence, and the conversion logic. The program prompts the user for an infix expression and outputs the corresponding postfix expression.

Uploaded by

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

InfixToPostfix (Both)

The document provides a C program that converts infix expressions into postfix notation using two implementations: one with a simple array-based stack and another using a structured stack. It includes functions for stack operations, operator precedence, and the conversion logic. The program prompts the user for an infix expression and outputs the corresponding postfix expression.

Uploaded by

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

Write a C program to convert infix expression into postfix.

#include <stdio.h>
#include <ctype.h> // for isalnum()
#include <string.h> // for strlen()

#define MAX 100

char stack[MAX];
int top = -1;

// Push function
void push(char x) {
if (top == (MAX - 1))
printf("Stack overflow\n");
else
stack[++top] = x;
}

// Pop function
char pop() {
if (top == -1)
return -1;
else
return stack[top--];
}

// Function to return precedence of operators


int precedence(char x) {
if (x == '(')
return 0;
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/')
return 2;
if (x == '^')
return 3;
return -1;
}

// Function to convert infix to postfix


void infixToPostfix(char infix[]) {
char postfix[MAX];
int k = 0;
for (int i = 0; i < strlen(infix); i++) {
char ch = infix[i];

if (isalnum(ch)) {
// Operand → directly to output
postfix[k++] = ch;
}
else if (ch == '(') {
push(ch);
}
else if (ch == ')') {
// Pop until '('
while (top != -1 && stack[top] != '(') {
postfix[k++] = pop();
}
pop(); // remove '('
}
else {
// Operator
while (top != -1 && precedence(stack[top]) >= precedence(ch)) {
postfix[k++] = pop();
}
push(ch);
}
}

// Pop remaining operators


while (top != -1) {
postfix[k++] = pop();
}

postfix[k] = '\0';
printf("Postfix Expression: %s\n", postfix);
}

int main() {
char infix[MAX];
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPostfix(infix);
return 0;
}
INPUT:
Enter an infix expression: A*(B+C)/D
OUTPUT:
Postfix Expression: ABC+*D/

INPUT:
Enter an infix expression: a+b*(c^d-e)^(f+g*h)-i
OUTPUT:
Postfix Expression: abcd^e-fgh*+^*+i-

USING STRUCTURE:

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

#define MAX 100

// Stack structure
typedef struct {
char data[MAX];
int top;
} Stack;

// Initialize stack
void init(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 element
void push(Stack *s, char x) {
if (isFull(s)) {
printf("Stack Overflow!\n");
} else {
s->data[++(s->top)] = x;
}
}

// Pop element
char pop(Stack *s) {
if (isEmpty(s)) {
return -1;
} else {
return s->data[(s->top)--];
}
}

// Peek top element


char peek(Stack *s) {
if (isEmpty(s))
return -1;
return s->data[s->top];
}

// Precedence function
int precedence(char x) {
if (x == '(') return 0;
if (x == '+' || x == '-') return 1;
if (x == '*' || x == '/') return 2;
if (x == '^') return 3;
return -1;
}

// Convert infix to postfix


void infixToPostfix(char infix[]) {
Stack s;
init(&s);

char postfix[MAX];
int k = 0;
for (int i = 0; i < strlen(infix); i++) {
char ch = infix[i];

if (isalnum(ch)) {
// Operand goes directly to output
postfix[k++] = ch;
} else if (ch == '(') {
push(&s, ch);
} else if (ch == ')') {
// Pop until '('
while (!isEmpty(&s) && peek(&s) != '(') {
postfix[k++] = pop(&s);
}
pop(&s); // remove '('
} else {
// Operator
while (!isEmpty(&s) && precedence(peek(&s)) >= precedence(ch)) {
postfix[k++] = pop(&s);
}
push(&s, ch);
}
}

// Pop remaining operators


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

postfix[k] = '\0';
printf("Postfix Expression: %s\n", postfix);
}

int main() {
char infix[MAX];

printf("Enter an infix expression: ");


scanf("%s", infix);

infixToPostfix(infix);

return 0;
}

You might also like