The program should accept input from a file (the filename is entered by the user
ID: 3529059 • Letter: T
Question
The program should accept input from a file (the filename is entered by the user on the console) where each line is a single arithmetic expression in postfix form, and converts each expression into its equivalent (fully parenthesized) infix form and outputs the result to the console. Again, the operands will be single letter variables and the operators will be restricted to +, -, *, and /. The overall algorithm is a two-step process. First convert the postfix expression into an expression tree. Then traverse the expression tree to print out the expression in infix form.Explanation / Answer
#include #include struct node{ char data; node *left; node *right; }; char postfix[35]; int top=-1; node *arr[35]; int r(char inputchar){ //for checking symbol is operand or operatorif(inputchar=='+' || inputchar=='-' || inputchar=='*' || inputchar=='/') return(-1); elseif(inputchar>='a' || inputchar='A' || inputchardata = symbol; newl->left = NULL; newl->right = NULL; push(newl); } else{ //If the symbol is operator//pop two top elements. ptr1=pop(); ptr2=pop(); newl = new node; newl->data = symbol; newl->left = ptr2; newl->right = ptr1; push(newl); } symbol=suffix[i]; } } void preOrder(node *tree){ if( tree!=NULL){ coutleft); preOrder(tree->right); } } void inOrder(node *tree){ if( tree!=NULL){ inOrder( tree->left); coutright); } } void postOrder(node *tree){ if( tree!=NULL){ postOrder( tree->left); postOrder( tree->right); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.