DS Programs
DS Programs
#include <stdio.h>
#include <ctype.h>
#include <string.h>
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);
}
char stack[MAX];
int top = -1;
return 0;
}
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);
}
}
}
int main() {
evaluatePostfix();
displayResult();
return 0;
}