postfix to infix help
2 10 4 * 5 / + 9 3 - -
or (cannont accept spaces)
2104*5/+93--
Is giving me:
((1+((0*4)/5))-(9-3))
Where am I going wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
string Expression::postToIn(string myExpression){
stack<string> Stack;
string infix = ""; // Initialize postfix as empty string.
string leftParenthesis = "(";
string rightParenthesis = ")";
string leftValue;
string rightValue;
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;
}
|
Topic archived. No new replies allowed.