Question: Need help with my function.. Suppose to convert a postfix expression to infix expression. 2 10 4 * 5 / + 9 3 - -

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

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))

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; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!