Question:
Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should support for both parenthesized and free parenthesized
expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric operands.
-
Alphanumeric operands (like
a
,x1
,B3
) -
Operators:
+
,-
,*
,/
,%
,^
-
Parenthesized and free-parenthesized expression.
Features:
-
Handles operator precedence
-
Handles right-associative power operator
^
-
Handles parentheses properly
-
Ignores spaces
Infix to Postfix Conversion (Using Stack):
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#define MAX 100
char stack[MAX];
int top = -1;
// Stack operations
void push(char ch) {
if (top >= MAX - 1) {
printf("Stack Overflow\n");
return;
}
stack[++top] = ch;
}
char pop() {
if (top == -1) return '\0';
return stack[top--];
}
char peek() {
if (top == -1) return '\0';
return stack[top];
}
// Function to check precedence
int precedence(char op) {
switch (op) {
case '^': return 3;
case '*':
case '/':
case '%': return 2;
case '+':
case '-': return 1;
default: return 0;
}
}
// Function to check if the character is operator
int isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == '%' || ch == '^';
}
// Associativity check: returns 1 if right-associative
int isRightAssociative(char op) {
return op == '^';
}
// Main conversion function
void infixToPostfix(const char* infix, char* postfix) {
int i = 0, j = 0;
char ch;
while ((ch = infix[i++]) != '\0') {
if (isspace(ch)) continue;
// If operand (alphanumeric), append to postfix
if (isalnum(ch)) {
postfix[j++] = ch;
}
// If '(', push to stack
else if (ch == '(') {
push(ch);
}
// If ')', pop until '(' is found
else if (ch == ')') {
while (top != -1 && peek() != '(') {
postfix[j++] = pop();
}
if (peek() == '(')
pop(); // Discard '('
}
// If operator
else if (isOperator(ch)) {
while (top != -1 && isOperator(peek()) &&
((precedence(peek()) > precedence(ch)) ||
(precedence(peek()) == precedence(ch) && !isRightAssociative(ch)))) {
postfix[j++] = pop();
}
push(ch);
}
else {
printf("Invalid character: %c\n", ch);
exit(1);
}
}
// Pop remaining operators
while (top != -1) {
if (peek() == '(' || peek() == ')') {
printf("Mismatched parentheses\n");
exit(1);
}
postfix[j++] = pop();
}
postfix[j] = '\0';
}
// Driver Code
int main() {
char infix[MAX], postfix[MAX];
printf("Enter an infix expression:\n");
fgets(infix, MAX, stdin);
infix[strcspn(infix, "\n")] = '\0'; // Remove newline
infixToPostfix(infix, postfix);
printf("Postfix expression:\n%s\n", postfix);
return 0;
}
Input/Output:
Input:
(a+(b-c)*d)
Output:
ceciot@cec-IOT:~$ cd Desktop/
ceciot@cec-IOT:~/Desktop$ gcc postfixconversion.c -o abcd
ceciot@cec-IOT:~/Desktop$ ./abcd
Enter an infix expression:
(a+(b-c)*d)
Postfix expression:
abc-d*+
note:
-
Alphanumeric support is for single characters. You can extend it for full variable names if needed.
-
You can use a
struct
or dynamic arrays to improve variable name handling or expand stack capacity. -
Ensure expressions are valid; this version does not fully validate malformed input.
Let me know if you want:
-
Variable-length operand support (e.g.
num1 + num2
) -
Evaluation of the postfix expression after conversion
-
Support for unary operators like
-a
No comments:
Post a Comment