Data sturcture and algorithm week 4
Data sturcture and algorithm week 4
Semester – II Semester
EA2331201010152
1. Write a C program to evaluate infix expressions. An infix expression looks
like the following:
9 * (5 - 4) + 2 / 6
954-*26/+
For this assignment you will need to implement and apply one stack to
convert from infix to postfix, and implement and apply a second stack to
evaluate the postfix expression. Your program must take the infix
expression as a string from the user.
Ans. the C program to evaluate infix expressions, incorporating the best aspects of the
provided responses and addressing noted issues:
C
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char stack[MAX_SIZE];
int top = -1;
int main() {
char infix[MAX_SIZE], postfix[MAX_SIZE];
infixToPostfix(infix, postfix);
return 0;
}
EA2331201010152
char pop() {
if (isEmpty()) {
printf("Stack Underflow\n");
exit(1);
}
return stack[top--];
}
int isEmpty() {
return top == -1;
}
while (!isEmpty()) {
postfix[j++] = pop();
}
postfix[j] = '\0'; // Terminate with null character
}
EA2331201010152
break;
case '*':
result = op1 * op2;
break;
case '/':
result = op1 / op2;
break;
}
push(result + '0'); // Convert integer to character
}
}
return pop() - '0'; // Convert character to integer
}
EA2331201010152