Use System. LINQ. expressions to implement four arithmetic calculators (2)

Source: Internet
Author: User
Exploitation System. LINQ. Expressions Implement a four-digit Calculator (2) )

How to convert a mathematical expression to an Expression Tree?

As mentioned above, several classes are used to encapsulate mathematical expressions.5*4/(3 + 2-1)"After conversion to the Expression Tree, such:


"Expression list" and"/(3 + 2-1)"Are all usedBwexpressionnodecollectionIndicates,"5","* 4","3","+ 2","-1"Both useBwconstantexpressionnode.
How can I convert a mathematical expression string to an Expression Tree? The answer is to use a regular expression.

The regular expression that matches a number is""D + (". "D + )?", The expression that matches the symbol is"("+ |-|" * | /)". The regular expression matching the brackets is""([^" (")] * (? 'Open' "() [^" (")] *) + ((? '-Open' ") [^" (")] *) + )*(? (Open )(?!)) ")". How to write and use regular expressions? Please search for them online!

In a mathematical expression, if the first digit is a negative number, you can directly write it as"-2 + 3". If the negative number is in the middle of the expression, enclose it in parentheses."2 * (-2) + 3". No matter where the negative number is, there is a rule. In the simple expression summarized above, the negative number is always the first. SoProgramDuring conversion, you only need to consider the case where the first number is a negative number (the negative number in the brackets can only be the first one ). The regular expression that matches a negative number is"-? "D + (". "D + )?".

To generate an Expression Tree, see the following flowchart:


All the work for generating the Expression Tree is handed overBwexpressionnodecollection, I'm designingBwexpressionnodecollectionClass, use the constructor to input a mathematical expression string:

///<Summary>

///Expression Node Set

///</Summary>

///<Param name = "input">Expression string</Param>

///<Param name = "type">Operator before this expression</Param>

PublicBwexpressionnodecollection (StringInput,BwexpressionnodetypeType)

{

Base. Expressionnodetype = type;

This. Expressionstring = input;

This. Expressionnodelist =New Bwexpressionnodelist();

Double? Val = parsenegative ();

If(Val. hasvalue)

{

This. Expressionnodelist. Add (New Bwconstantexpressionnode(Val. value,Bwexpressionnodetype. Start ));

}

Else

{

StringParenthesescontent = getparentheses ();

If(!String. Isnullorempty (parenthesescontent ))

{

This. Expressionnodelist. Add (New Bwexpressionnodecollection(Parenthesescontent,Bwexpressionnodetype. Start ));

}

Else

{

Throw Error. Genericexception;

}

}

While(!String. Isnullorempty (This. Expressionstring ))

{

Char? Sign = getsign ();

If(Sign. hasvalue)

{

BwexpressionnodetypeNexttype;

Switch(Sign)

{

Case '+':

Nexttype =Bwexpressionnodetype. Addition;

Break;

Case '-':

Nexttype =Bwexpressionnodetype. Subtration;

Break;

Case '*':

Nexttype =Bwexpressionnodetype. Multiplication;

Break;

Case '/':

Nexttype =Bwexpressionnodetype. Division;

Break;

Default:

Throw Error. Genericexception;

}

Double? Operand = parsepositive ();

If(Operand. hasvalue)

{

This. Expressionnodelist. Add (New Bwconstantexpressionnode(Operand. Value, nexttype ));

}

Else

{

StringParenthesescontent = getparentheses ();

If(!String. Isnullorempty (parenthesescontent ))

{

This. Expressionnodelist. Add (New Bwexpressionnodecollection(Parenthesescontent, nexttype ));

}

Else

{

Throw Error. Genericexception;

}

}

}

Else

{

Throw Error. Genericexception;

}

}

}

We can see that if a mathematical expression encounters a bracket, the mathematical expression in the bracket is generated into a childBwexpressionnodecollectionObject.

So far, the expression tree has been generated:

string input = @" 1 + 5*9-(1 + 2) * 3/4 + 6 * (-2) ";

bwexpressionnodecollection nodes = New bwexpressionnodecollection (input, bwexpressionnodetype . start);

next we will introduce how to generate from the Expression Tree system. LINQ. expressions class. Please note!

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.