哥哥姐姐们 ,星期四交作业啊。麻烦你们了
Part 2:Design and implement an interactive notation conversion program that is based on an expression tree. The expression tree consists of a series of expression tree nodes (exp_tree_node class). You have to implement two classes: one for the expression tree node and the other for the expression tree.
• expression tree node class
The expression tree node class has the following two nodes:
1. Leaf nodes, which contain a number as their entry;
2. Nonleaf nodes, which contain one of the “+-*/” operators as their entry, and have exactly two children.
For this assignment, you need to design and implement a template expression tree node class (exp_tree_node class) for expression trees, including operations for building expression trees. Also, include a recursive function to evaluate a non-empty expression tree using these rules:
1. If the tree has only one node (which must be a leaf), then the evaluation of the tree returns the number that is the node’s entry;
2. If the tree has more than one node, and the root contains ‘+’, then first evaluates the left subtree, then evaluates the right subtree, and adds the results. Other operators ‘-‘, ‘*’ and ‘/’ work in a similar way.
In addition, include recursive functions (infix, postfix and prefix) to print corresponding infix, postfix and prefix notations of the expression tree. Finally, include a print function to print out the expression tree to the standard output (cout) so that users can see the tree visually.
The exp_tree_node class consists of four private data members:
• op_field: stores one of operators if it is a non-leaf node;
stores “l” if it is a leaf-node.
• data_field: stores a data value.
• left_field: pointer to the left child.
• right_field: pointer to the right child.
Please refer to “exp_tree_node-template.h” for details. Students are required to complete this file and need to write a template implementation of this file.
• expression tree node:
You also need to design and implement a template expression class (expression class) that consists of a series of expression tree nodes. It is a certain type (tree type) of linked list of the exp_tree_node class.
A header file “expression.h” is provided and students are required to implement “expression.template” file.
• The structure of assignment is as follows:
Reading:
Chapter 10 especially, project 1 in programming projects.
File that you must write: exp_tree_node.h: A template header file defining a simple expression tree node.在这个头文件中定义一个简单的算术表达式的tree 的指针
1. exp_tree_node.template: An implementation of the template expression tree node class.这个文件是用于控制指针的
2. expression.template: An implementation of the tree expression class.用于计算的
expression1.rar
(84.33 KB)