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

DS Programs

The document contains C programs for converting infix expressions to postfix, checking for balanced parentheses, and evaluating postfix expressions using a stack. It includes functions for pushing and popping elements from the stack, determining operator precedence, and handling various input cases. The main functions prompt the user for input and display the results of the operations performed.

Uploaded by

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

DS Programs

The document contains C programs for converting infix expressions to postfix, checking for balanced parentheses, and evaluating postfix expressions using a stack. It includes functions for pushing and popping elements from the stack, determining operator precedence, and handling various input cases. The main functions prompt the user for input and display the results of the operations performed.

Uploaded by

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

INFIX TO POSTFIX CONVERSION

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

#define MAX 100

char stack[MAX], infix[MAX], postfix[MAX];


int top = -1;

void push(char ch) { stack[++top] = ch; }


char pop() { return (top >= 0) ? stack[top--] : '#'; }
int precedence(char ch) { return (ch == '+' || ch == '-') ? 1 : (ch == '*' || ch == '/') ? 2 : (ch == '^') ?
3 : 0; }
void infixToPostfix();
void displayPostfix();

int main() {
printf("Enter infix expression: ");
scanf("%s", infix);
infixToPostfix();
displayPostfix();
return 0;
}

void infixToPostfix() {
int i, j = 0;
for (i = 0; infix[i]; i++) {
if (isalnum(infix[i])) postfix[j++] = infix[i];
else if (infix[i] == '(') push(infix[i]);
else if (infix[i] == ')') {
while (top != -1 && stack[top] != '(') postfix[j++] = pop();
top--; // Remove '('
} else {
while (top != -1 && precedence(stack[top]) >= precedence(infix[i]))
postfix[j++] = pop();
push(infix[i]);
}
}
while (top != -1) postfix[j++] = pop();
postfix[j] = '\0';
}

void displayPostfix() {
printf("Postfix expression: %s\n", postfix);
}

Program to implement balanced parathesis check using stack


#include <stdio.h>
#define MAX 100

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

void push(char ch) { stack[++top] = ch; }


char pop() { return (top >= 0) ? stack[top--] : '#'; }
int main() {
char expr[MAX];
int i;

printf("Enter an expression: ");


//scanf("%[^\n]", expr);
scanf("%s", expr);
// Using fgets to read the entire line

for (i = 0; expr[i] != '\0'; i++) {


if (expr[i] == '(' || expr[i] == '{' || expr[i] == '[')
push(expr[i]);
else if (expr[i] == ')' || expr[i] == '}' || expr[i] == ']') {
if (top == -1) { // Unmatched closing bracket
printf("Not Balanced\n");
return 0;
}
char open = pop();
if ((expr[i] == ')' && open != '(') ||
(expr[i] == '}' && open != '{') ||
(expr[i] == ']' && open != '[')) {
printf("Not Balanced\n");
return 0;
}
}
}

// Final check: If stack is empty, it's balanced


if (top == -1)
printf("Balanced\n");
else
printf("Not Balanced\n");

return 0;
}

Post fix expression evaluation using stack


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

#define MAX 100

int stack[MAX], top = -1;


char postfix[MAX];

// Function to push a number onto the stack


void push(int num) {
stack[++top] = num;
}

// Function to pop a number from the stack


int pop() {
return stack[top--];
}

// Function to evaluate the postfix expression


void evaluatePostfix() {
int i, op1, op2, result;

printf("Enter postfix expression (without spaces, e.g., 23*5+): ");


scanf("%s", postfix);

for (i = 0; postfix[i] != '\0'; i++) {


if (isdigit(postfix[i])) {
push(postfix[i] - '0'); // Convert char to int and push
} else {
op2 = pop();
op1 = pop();

switch (postfix[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;
}
push(result);
}
}
}

// Function to display the final result


void displayResult() {
printf("Result: %d\n", stack[top]);
}

int main() {
evaluatePostfix();
displayResult();
return 0;
}

You might also like