Expression Tree Project Doc is interesting

Source: Internet
Author: User




Problem description
Problem Analysis
Program instructions
Program Structure

Problem description
The original problem is as follows: (from Chapter 3, additional exercises 10)

Programming project:

I am sorry I can't find where the project is! I just hear it from one of my classmate

Enter a string of expression, you must consturct a tree devide from the string

And calculate the value of this expression

Back to Top
Problem Analysis

1:

The string stream that inputs an expression identifies various arithmetic elements such as the "+" "-" variable and integer floating point number.

2: Build an expression tree using recursive Methods

3:

Tree numeric operations to find the unknown variable, get its value, and traverse the calculated value in descending order

Back to Top

Program instructions

Button description

Enter: Read the expression in the edit boxProgram, Generate Expression Tree

Postorder: Post-order traversal

Inorder

Preorder pre-order traversal

Insert Space: determines whether to insert spaces in the generated traversal formula, because the variable may be multiple characters.

Calculation: calculates the expression value.

Procedure

1: Enter an expression in the editor.

2: Press enter.

3: Expression Processing is completed. If the expression is correct, a tree is generated but not expanded.

4: Click the root node to view the entire tree.

5: view the result of three traversal types by preorder, inorder, or posorder.

6: Click calculation to start the calculation of the expression value.

7. A dialog box is displayed when the program encounters an unknown variable.

8: After the variable value is input, the program will automatically record it and the next operation will automatically call

9: to change the value of a variable in the Variable list, double-click the variable with the mouse.

Program Structure

The core data structure is as follows:

Typedef struct tree_struct

{

Cstring node;

Tree_struct * left;

Tree_struct * right;

} Treestruct;

The Core Algorithm functions are as follows:

Bool constructtree (treestruct * leaf, cstring expression)

Double calculate (treestruct * leaf)

Cstring preorder (treestruct * leaf)

Cstring postorder (treestruct * leaf)

Cstring inorder (treestruct * leaf)

Bool testwholeexpression (cstring Str)

Bool ctreedlg: constructtree (treestruct * leaf, cstring expression)

// Tree construction function. Leaf is the expression string stream of the Expression Tree's current worker node.

{(Go Back To function List)

Cstring STR;

Int length; // expression Length

Cstring rightexpression; // truncates the right substring

Cstring leftexpression; // extract the left substring

Treestruct * rightleaf; // right subtree Node

Treestruct * leftleaf; // left subtree Node

Length = expression. getlength ();

If (length = 0) return false; // The input is invalid if the string length is zero.

// This setting is used to prevent "A + B -".

Int operator; // obtain the location where the symbol can be used as a node

// Return rule demonstration

/*

(A + B) * (C + D) operator = 3

A + (B-d) operator = 2

A operator =-1;

(A + B) operator =-1;

*/

If (! Testwholeexpression (expression) return false; // judge whether the brackets in the expression match

// If it is of the "(A + B)" type, parentheses are removed to prevent infinite recursion or as a variable

If (expression [length-1] = ')')

{

Left = findleft (expression, length-1 );

// Findleft () is a core function that returns the right brace matching the current right brace.

If (Left =-1) return false;

If (Left = 0) // The description is of the (A + B) type.

Expression = expression. mid (1, length-2 );

Length = expression. getlength ();

If (length = 0) return false;

}

// Find '+' Operator

Operator = find (expression, '+ ');

/* Find () is another important function that returns the leftmost position of the character to be searched.

However, you must skip the brackets.

If no result is found,-1 is returned.

If a syntax error is found in the expression,-2 */is returned */

If (operator =-2) return false; // if the expression has an error, false is returned.

If (Operator! =-1)

{

// Determine the Left and Right subexpressions

Cstring temp;

Leftexpression. Format (lefting (temp, operator ));

Rightexpression. Format (righting (temp, length-operator-1 ));

// Build a tree

Leaf-> node = "+ ";

Rightleaf = new treestruct;

Leftleaf = new treestruct;

Leaf-> left = leftleaf;

Leaf-> right = rightleaf;

If (! Constructtree (rightleaf, rightexpression) return false;

If (! Constructtree (leftleaf, leftexpression) return false;

Return true;

}

// Find "-" Operator

.................................

// Find "*" Operator

......................................

// Find "/" Operator

.....................................

// The appearance of a () B is not allowed.

If (findchar (expression ,'(')! =-1) return false;

If (findchar (expression ,')')! =-1) return false;

// Determine the leaf node

Leaf-> node = expression;

Leaf-> right = NULL;

Leaf-> left = NULL;

Return true;

}

// Numeric calculation of the expression

Double ctreedlg: Calculate (treestruct * leaf)

{(Go Back To function List)

Cdatadlg da;

// M_error is the type of error returned by the global variable record. It is related to the error list.

If (isoperator (leaf-> node) // isoperator () determines whether this node is an operator

{

Double DL = calculate (leaf-> left );

If (m_error) return 0;

Double DR = calculate (leaf-> right );

If (m_error) return 0;

Return operation (leaf-> node, DL, Dr );

}

Else

{

Int Index = findinvariablelist (leaf-> node); // search

If (index! =-1)

Return m_variable [Index]. Data;

Else

{

Variablesp ++; // The top pointer of the variable stack moves up

M_variable [variablesp]. Variable = leaf-> node;

M_variable [variablesp]. Data = (double) (atof (leaf-> node ));

If (m_variable [variablesp]. Data = 0 &&

M_variable [variablesp]. Variable! = "0 ")

{

Da. m_title.format ("variable % s", leaf-> node );

Da. m_data = 0;

If (DA. domodal () = idok)

{

M_variable [variablesp]. Data = da. m_data;

M_list.insertitem (variablesp, m_variable [variablesp]. variable, 1 );

Cstring temp;

// ITOA (DA. m_data, temp. getbuffer (100), 10 );

_ Ultoa (DA. m_data, temp. getbuffer (100), 10 );

M_list.setitemtext (variablesp, 1, temp );

}

}

Return da. m_data;

}

}

Return-1;

}

Cstring ctreedlg: Preorder (treestruct * leaf)

// Pre-order traversal

{(Go Back To function List)

Cstring Ss = "";

If (leaf = NULL) return SS;

Ss = SS + leaf-> node;

If (m_binsertspace) SS + = '';

If (leaf-> left! = NULL)

SS + = preorder (leaf-> left );

If (leaf-> right! = NULL)

SS + = preorder (leaf-> right );

Return SS;

}

Cstring ctreedlg: postorder (treestruct * leaf)

// Post-order traversal

{(Go Back To function List)

Cstring Ss = "";

If (leaf = NULL) return SS;

If (leaf-> left! = NULL)

SS + = postorder (leaf-> left );

If (leaf-> right! = NULL)

SS ++ = postorder (leaf-> right );

Ss = SS + leaf-> node;

If (m_binsertspace) SS + = '';

Return SS;

}

Cstring ctreedlg: inorder (treestruct * leaf)

// Sequential Traversal

{(Go Back To function List)

Cstring Ss = "";

If (leaf = NULL) return SS;

If (leaf-> left! = NULL)

SS + = inorder (leaf-> left );

If (leaf-> left & m_binsertspace) SS + = '';

Ss = SS + leaf-> node;

If (leaf-> Right & m_binsertspace) SS + = '';

If (leaf-> right! = NULL)

SS + = inorder (leaf-> right );

Return SS;

}

Bool ctreedlg: testwholeexpression (cstring Str)

{(Go Back To function List)

Int length = Str. getlength ();

Int sp = 0;

For (INT I = 0; I <length; I ++)

{

If (STR [I] = '(') SP ++;

If (STR [I] = ') sp --;

}

If (SP! = 0) return false;

Return true;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.