I\'m having problem debugging my code. I have to convert infix expression to pos
ID: 643577 • Letter: I
Question
I'm having problem debugging my code. I have to convert infix expression to postfix from these functions x=b*c-d/e+f and y=b^c/d+e*f+g-h using these values b=2, c=3, d=4, e=2, f=3, g=4, h=1. In addition I have to compute both expressions using stack and postfix expression. here is my program. Please help ASAP. I got stuck. Thanks
#include
#include
#include
using namespace std;
class stack
{
public:
char stack_array[50];
int top;
stack()
{
top = -1;
}
void push(char symbol)
{
if (full())
{
cout << " Stack overflow: ";
}
else
{
top = top + 1;
stack_array[top] = symbol;
}
}
char pop()
{
if (empty())
return('#'); // Return value '#' indicates stack is empty
else
return(stack_array[top--]);
}
int empty()
{
if (top == -1)
return(1);
else
return(0);
}
int full()
{
if (top == 49)
return(1);
else
return(0);
}
};
class Expression
{
char infix[50];
char postfix[50];
public:
void read()
{
cout << " Enter an infix expression:";
cin >> infix;
cout << "Infix expression: " << infix << endl << endl;
}
int white_space(char symbol)
{
if (symbol == ' ' || symbol == ' ' || symbol == '')
return 1;
else
return 0;
}
void ConvertToPostfix()
{
stack s;
int l, precedence, p;
char entry1, entry2;
p = 0;
for (int i = 0; infix[i] != ''; i++)
{
entry1 = infix[i];
if (!white_space(entry1))
{
switch (entry1)
{
case '(':
s.push(entry1);
break;
case ')':
while ((entry2 = s.pop()) != '(')
postfix[p++] = entry2;
break;
case '+':
case '-':
case '*':
case '/':
if (!s.empty())
{
precedence = prec(entry1);
entry2 = s.pop();
while (precedence <= prec(entry2))
{
postfix[p++] = entry2;
if (!s.empty())
entry2 = s.pop();
else
break;
}
if (precedence>prec(entry2))
s.push(entry2);
}
s.push(entry1);
break;
default:
postfix[p++] = entry1;
break;
}
}
}
while (!s.empty()) //while stack is not empty
postfix[p++] = s.pop();
postfix[p] = '';
cout << "postfix expression : " << postfix << endl << endl;
}
int prec(char symbol)
{
switch (symbol)
{
case '/': return(4); // Precedence of / is 4
case '*': return(3); // Precedence of * is 3
case '+': return(2); // Precedence of + is 2
case '-': return(1); // Precedence of - is 1
case '(': return(0); // Precedence of ( is 0
default: return(-1);
}
}
void calculatePostFix()
{
{
int *stack_array;
char *postfix;
int top = 0, i = 0;
int c = strlen(postfix);
while (i < c)
{
if (!((postfix[i] >= 42) && (postfix[i] <= 47)))
{
top = top + 1;
stack_array[top] = postfix[i];
stack_array[top] = stack_array[top] - 48;
//cout<
}
else
{
int entry1, entry2;
entry1 = stack_array[top];
//cout< top--;
entry2 = stack_array[top];
//cout< top--;
int result;
switch (postfix[i])
{
case '+':
result = entry2 + entry1;
//cout< break;
case '-':
result = entry2 - entry1;
break;
case '*':
result = entry2 * entry1;
break;
case '/':
result = entry2 / entry1;
break;
}
top++;
stack_array[top] = result;
}
i++;
}
top = 1;
cout << stack_array[top] << endl;
}
}
};
int main()
{
char choice = 'y';
Expression expr;
while (choice == 'y')
{
expr.read();
expr.ConvertToPostfix();
expr.calculatePostFix();
cout << " Do you want to continue ? (y/n): ";
cin >> choice;
}
return 0;
}
Explanation / Answer
void ConvertToPostfix()
This function is prefect .
You need to correct :
void calculatePostFix() //this function you need to correct
{
{
int *stack_array; //allocate the memory
char *postfix;
int top = 0, i = 0;
int c = strlen(postfix);
while (i < c)
{
if (!((postfix[i] >= 42) && (postfix[i] <= 47))) //how to extract numbers from postfix expression
{
top = top + 1;
stack_array[top] = postfix[i];
stack_array[top] = stack_array[top] - 48;
//cout<
}
else
{
int entry1, entry2;
entry1 = stack_array[top];
//cout< top--;
entry2 = stack_array[top];
//cout< top--;
int result;
switch (postfix[i])
{
case '+':
result = entry2 + entry1;
//cout< break;
case '-':
result = entry2 - entry1;
break;
case '*':
result = entry2 * entry1;
break;
case '/':
result = entry2 / entry1;
break;
}
top++;
stack_array[top] = result;
}
i++;
}
top = 1;
cout << stack_array[top] << endl;
}
}
rest all is fine ,just correct this function
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.