Need help with my function.. Suppose to convert a postfix expression to infix ex
ID: 3803885 • Letter: N
Question
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<string> 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
/* Evaluation Of postfix Expression in C++ Input Postfix expression must be in a desired format. Operands must be integers and there should be space in between two operands. Only '+' , '-' , '*' and '/' operators are expected. */ #include #include #include using namespace std; // Function to evaluate Postfix expression and return output int EvaluatePostfix(string expression); // Function to perform an operation and return output. int PerformOperation(char operation, int operand1, int operand2); // Function to verify whether a character is operator symbol or not. bool IsOperator(char C); // Function to verify whether a character is numeric digit. bool IsNumericDigit(char C); int main() { string expression; coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.