ProgrammingAssignment-Expression

ProgrammingAssignment-Expression - Programming Assignment...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming Assignment #5 Submit - Due Thursday, March 22, 2007 at 10:30 am Hardcopy - Due same day at the beginning of class This assignment gives you more experience implementing recursive algorithms as well as binary trees. Given an infix expression (in the form of a character array), you will build a tree that represents the expression. Once the expression tree is constructed, you will then simplify the resulting tree, if possible. This simplification is similar to a code optimization method used by compilers called constant folding . Constant folding is a technique whereby the compiler can evaluate certain expressions at compile time instead of generating code that evaluates the expression at runtime. For example, the compiler can simplify these expressions: 4 * (2 + 3) 20 A * (2 + 3) A * 5 A * (3 - 4 + 1) + B B A + 2 * 3 A + 6 The client will interact with this “expression simplifier” by constructing an object of the class Expression . This class is a trivial class to implement since all of the work is done by two other support classes. In addition to the constructor/destructor, there are three public methods: a method to construct the tree ( BuildTree ), a method to simplify the tree ( Simplify ), and a simple method to return a pointer to the root of the tree. Part #1 - Construct the expression tree The first task is to construct the expression tree. To construct the tree, you will implement a Parser class. The interesting data structure used is the ExprNode struct, which describes what a node in our expression tree will look like. enum Kind {OPERATOR, LITERAL, IDENTIFIER}; struct ExprNode { ExprNode *left; // the left operand/expression ExprNode *right; // the right operand/expression Kind kind; // the kind of node (OPERATOR/LITERAL/IDENTIFIER) union
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

ProgrammingAssignment-Expression - Programming Assignment...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online