MUST HAVE A HEADER FILE Create a program which will read a s

*MUST HAVE A HEADER FILE*

Create a program, which will read a series of numbers and operators. Using those values atree will be built representing the expression. The tree will then be evaluated and deleted.

STATEMENTS:
Valid statements are of one of the following three forms:

1) ‘C’ followed by a whole number: this will cause the number (up to three digits with a possible minus sign) to be inserted into the tree as a constant.

2) ‘V’: The letter V followed by two digits will cause a variable to be placed into the tree(the digits will be which variable 00 through 99).

3) ‘v’: The letter v followed by two digits, a space, and up to three digits with possible
minus sign will set the value of a variable (the set of two digits is which variable) tobe set to the value (the set of up to three digits).

4) ‘O’ followed by an operator (‘+’, ‘-‘, ‘*’, ‘/’) followed by a single digit number: this
will insert the operator into the tree using the following digit as the precedence of theoperator (this equates to the number of parentheses enclosing the operator - the higherthe value of the number, the higher the precedence of the operator).

5) ‘E’: The letter E will cause the tree to be evaluated and the resulting value printed.

6) ‘D’: The letter D will cause the tree to be deleted.

7) ‘X’: Terminate the program.For test purposes some way of printing the delete operation as it proceeds should be provided to insure that all nodes are indeed deleted.After deleting the tree, the process may begin again and will result in the creation of anew tree.

Solution

#include \"stdafx.h\" #include #include #include #include #include using namespace std; // An expression tree node struct et { char value; et* left, *right; }; // A utility function to check if \'c\' // is an operator bool isOperator(char c) { if (c == \'+\' || c == \'-\' || c == \'*\' || c == \'/\' || c == \'^\') return true; return false; } // Utility function to do inorder traversal void inorder(et *t) { if (t) { inorder(t->left); printf(\"%c \", t->value); inorder(t->right); } } // A utility function to create a new node et* newNode(int v) { et *temp = new et; temp->left = temp->right = NULL; temp->value = v; return temp; }; // Returns root of constructed tree for given // postfix expression et* constructTree(char postfix[]) { stack st; et *t, *t1, *t2; // Traverse through every character of // input expression for (int i = 0; iright = t1; t->left = t2; // Add this subexpression to stack st.push(t); } } // only element will be root of expression // tree t = st.top(); st.pop(); return t; } void deleteTree(struct node* node) { if (node == NULL) return; /* first delete both subtrees */ deleteTree(node->left); deleteTree(node->right); /* then delete the node */ cout << endl; cout << \"Deleting node\" << endl; free(node); } // Driver program to test above int main() { char postfix[] = \"ab+ef*g*-\"; et* r = constructTree(postfix); printf(\"infix expression is \ \"); inorder(r); return 0; }
*MUST HAVE A HEADER FILE* Create a program, which will read a series of numbers and operators. Using those values atree will be built representing the expressio

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site