Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with my function.. Suppose to convert a postfix to infix expression. I

ID: 3804152 • Letter: N

Question

Need help with my function.. Suppose to convert a postfix to infix expression.

I can give full program if needed. It doesnt seem to handle numbers > 9 very well. Letters its perfect.

2 10 4 * 5 / + 9 3 - -
or (cannont accept spaces)
2104*5/+93--

Is giving me:
((1+((0*4)/5))-(9-3))

should be ((2+((10*4)/5)-(9-3))

if i change 10 to 9 I get correct result

Where am I going wrong? Please add comments if you fix !

string Expression::postToIn(string myExpression){
stack Stack;
string infix = ""; // Initialize postfix as empty string.
string leftParenthesis = "(";
string rightParenthesis = ")";
string leftValue;
string rightValue;
string myOperator;
string currentinfix;
bool leftDone = true; // leftDone if false means left value needs to be initialised.
bool rightDone = false; // rightDone true means rightDone already has a value hence leftvalue should go to stack , right value to leftvalue and new value to right value in case of operand.
leftValue = myExpression[0];
for(int i = 1;i< myExpression.length();i++){

if (isOperand(myExpression[i])){
if(leftDone){
if(rightDone){
Stack.push(leftValue);
leftValue = rightValue;
rightValue = myExpression[i];

}else{
rightValue = myExpression[i];
rightDone = true;
}
}else{
leftDone = myExpression[i];
leftDone = true;
}

}else{
if(rightDone){

leftValue = leftParenthesis + leftValue + myExpression[i] + rightValue + rightParenthesis;
rightDone = false;
}
else{

rightValue = leftValue;
leftValue = Stack.top();
Stack.pop();
leftValue = leftParenthesis + leftValue + myExpression[i] + rightValue + rightParenthesis;
}

}

}
return leftValue;
}

Explanation / Answer

The problem is the way you are using your variable myExpression[]. Since it is an array you are using a loop to get all operands and operators using an index but the numbers greater than 9 will be more than one digit and cannot be accessed using i. This will give each digit as a single number but you should take the operands till space or is there so it will take 10 not 1 and 0.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote