Below is the code to convert an infix expression to postfix and then evaluate that expression, it seems no error in program but it returns same expression as input by the user and not working fine. Please correct the code and mention errors and make it working. I am a begineer and trying since 1 day. Thanks... Code:
Below is the code to convert an infix expression to postfix and then evaluate that expression, it seems no error in program but it returns same expression as input by the user and not working fine. Please correct the code and mention errors and make it working. I am a begineer and trying since 1 day. Thanks...
Code:
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
#define SIZE 100
class Stack {
private:
char stack[SIZE]; // array to store the stack
int top; // variable to mark the top index of stack.
public:
// constructor
Stack() {
top = -1; // for empty array, set top = -1
}
void push(char symbol) {
if (top >= SIZE - 1) {
printf("Stack Overflow!");
} else {
stack[top++] = symbol;
}
}
char pop() {
char symbol;
if (top < 0) {
printf("Stack underflow!");
getchar();
exit(1);
} else {
symbol = stack[top];
top--;
return (symbol);
}
}
bool topcheck() {
if (top > 0)
return true;
return false;
}
};
class expression {
public:
char infix[SIZE];
char postfix[SIZE];
int isOperator(char symbol) {
if (symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol == '-') {
return 1;
} else {
return 0;
}
}
int precedence(char symbol) {
if (symbol == '^') {
return (3);
} else if (symbol == '*' || symbol == '/') {
return (2);
} else if (symbol == '+' || symbol == '-') {
return (1);
} else {
//printf("Invalid Character Symbol Entered!");
return (0);
}
}
void infixToPostfix(char infix[], char postfix[]) {
int i, j;
char symbol;
char x;
Stack s;
s.push('(');
strcat(infix, ")");
i = 0;
j = 0;
symbol = infix[i];
while (symbol != '\0') {
if (symbol == '(') {
s.push(symbol);
} else if (isdigit(symbol) || isalpha(symbol)) {
postfix[j++] = symbol;
} else if (isOperator(symbol) == 1) {
x = s.pop();
while (isOperator(x) == 1 || precedence(x) >= precedence(symbol)) {
postfix[j++] = x;
x = s.pop();
}
s.push(x);
s.push(symbol);
} else if (symbol == ')') {
x = s.pop();
while (x != '(') {
postfix[j++] = x;
x = s.pop();
} //end while loop
} else {
printf("Invalid Expression!");
getchar();
exit(1);
} //end else if
i++;
symbol = infix[i];
}
if (s.topcheck()) {
printf("Invalid Infix Expression!");
getchar();
exit(1);
}
postfix[j] = '\0';
}
};
int main() {
expression ex;
printf("Enter the expression: ");
scanf("%s", ex.infix); //input expression without any whitespace
int stringLength = strlen(ex.infix); //compute the length
for (int i = 0; i < stringLength; i++) {
//delete the second character if two consecutive characters are operator
if ((ex.isOperator(ex.infix[i]) && ex.isOperator(ex.infix[i + 1])) && i < stringLength - 1) {
for (int j = i + 1; j < stringLength; j++) {
ex.infix[j] = ex.infix[j + 1];
}
stringLength = stringLength - 1; //reduce the length of the string
}
}
printf("\nModified expression: %s", ex.infix); //display the modified string
for (int i = 0; i < strlen(ex.infix); i++) { //for checking multiple ooerators at once!
if (ex.isOperator(ex.infix[i]) && ex.isOperator(ex.infix[i + 1])) {
printf("%c", ex.infix[i]);
while (ex.isOperator(ex.infix[i + 1])) {
i++;
}
} else {
printf("%c", ex.infix[i]);
}
}
for (int i = 0; i < strlen(ex.infix); i++) {
if (ex.infix[i] == '(' && ex.isOperator(ex.infix[i + 1])) { //if operator right after bracker!
printf("Invalid Expression, Please enter infix expression again: \n");
gets(ex.infix);
} else {
break;
}
if (ex.infix[0] == ')' || ex.isOperator(ex.infix[0])) { //checking for ')' or an operator in the beginning!
printf("Expression is Invalid!, Your expression cant start with a ')' or an Operator.");
gets(ex.infix);
} else {
break;
}
}
ex.infixToPostfix(ex.infix, ex.postfix);
printf("\nPostfix Expression: ");
puts(ex.postfix);
Stack s1;
int i = 0;
int value1, value2, answer, element;
while (i < strlen(ex.postfix) - 1) {
element = ex.postfix[i];
if (isdigit(element)) {
s1.push(element - '0');
} else {
value1 = s1.pop();
value2 = s1.pop();
switch (element) {
case '^':
answer = pow(value2, value1);
break;
case '/':
answer = value2 / value1;
break;
case '*':
answer = value2 * value1;
break;
case '+':
answer = value2 + value1;
break;
case '-':
answer = value2 - value1;
break;
}
s1.push(answer);
}
i++;
}
printf("\n Answer: %d", s1.pop());
return 0;
}
Step by step
Solved in 2 steps