C # Calculator--2. Token object for Imitation Query Analyzer

Source: Internet
Author: User
Tags abstract bool constructor expression numeric

In the previous article, we mentioned the idea of using the tree structure to analyze expressions and calculate the evaluation. But for the program, the input expression is just a string. To represent an expression as a tree structure, you must first break the expression into nodes, before you can make a tree of nodes. Here, each node in the tree is called a Tokenrecord object.

According to the above analysis, the token object requires a variable that stores its own value, has its own specific method of calculation, and knows its subordinate values. This leads to the basic information of Tokenrecord (omitting non-critical information):

Property

Index: The column number, type int, in the expression, which indicates an error, starting at 1.

Priority: priority, int type, required when parsing an expression.

Tokenvalue: Mark Value, Object type

Childlist: Subordinate lists, list objects, for storing subordinate elements, implementing the tree structure.

Method

Execute: Performs the operation of the element, the abstract method.

Setchildcount: Sets the number of subordinates, virtual methods, to check the legality of the number of subordinates, called in the constructor. There should be a m_childcount field for storing the number of subordinates. Because a task that is subordinate to an element is checked, the M_childcount is set to protected, and there is no corresponding ChildCount attribute.

SetPriority: Sets the precedence, virtual method, called in the constructor.

Checkchildcount: Check the number of subordinates, called in Execute, to guarantee the validity of the expression.

Where the Tokenvalue property uses the object type because it supports the operation of string, numeric, and logical values. At the beginning of the design, two fields, string and double, were used to store the strings and values, and the logical values were numerically expressed, and later changed to object.

The code for the Tokenrecord class is as follows:

<summary>
Mark Object
</summary>
<remarks>author:alex leo;</remarks>
Public abstract class Tokenrecord
{
#region Properties and fields

Number of subordinates
protected int m_childcount;

private int m_index;
<summary>
Column ordinal
</summary>
public int Index
{
get {return m_index;}
}

<summary>
Priority, you must assign a value
</summary>
protected int m_priority;
<summary>
Priority level
</summary>
<returns></returns>
public int Priority
{
get {return m_priority;}
}

private int m_length;
<summary>
Operator length
</summary>
public int Length
{
get {return m_length;}
}

Private Type M_tokenvaluetype;
<summary>
Token value type
</summary>
Public Type Tokenvaluetype
{
get {return m_tokenvaluetype;}
set {M_tokenvaluetype = value;}
}

Private Object M_tokenvalue;
<summary>
Mark Value
</summary>
public Object Tokenvalue
{
get {return m_tokenvalue;}
set {M_tokenvalue = value;}
}


Private list<tokenrecord> m_childlist = new list<tokenrecord> ();
<summary>
Subordinate list
</summary>
Public list<tokenrecord> Childlist
{
get {return m_childlist;}
}

#endregion

<summary>
Constructors
</summary>
<param name= "Index" > Serial number </param>
<param name= "Length" > Itself </param>
Public Tokenrecord (int Index, int Length)
{
This.m_index = Index;
This.m_length = Length;
This. SetPriority ();
This. Setchildcount ();
}


#region method

<summary>
Overriding the ToString Method
</summary>
<returns></returns>
public override string ToString ()
{
Can be modified to display different information as needed
return this. GetType (). Name + "_" + getvaluestring () + "_" + tokenvaluetype.tostring ();
}

<summary>
Gets a string representation of a value
</summary>
<returns></returns>
public string getvaluestring ()
{
return this. Tokenvalue.tostring ();
}

<summary>
Check the number of subordinates, which can be rewritten if necessary, because some token can be an interval
</summary>
<param name= the error message displayed when the "Errorinformation" > Number of subordinates do not match </param>
internal void Checkchildcount (string errorinformation)
{
if (This.m_ChildList.Count!= this.m_childcount)
throw new Syntaxexception (This.m_index, This.m_length, errorinformation);
}

#region methods that must be overridden

<summary>
Executing code
</summary>
public abstract void Execute ();

<summary>
Set the number of subordinates
</summary>
protected abstract void Setchildcount ();

<summary>
Set Priority
</summary>
protected abstract void setpriority ();

#endregion


#endregion


#region Conversion Token value type

<summary>
Converts a token value to a string type
</summary>
Internal String changetokentostring ()
{
String strvalue;
strvalue = (string) (this. Tokenvalue = this. Tokenvalue.tostring ());
This. Tokenvaluetype = typeof (String);
return strvalue;
}

<summary>
Converts a token value to a numeric type
</summary>
<param name= "errorinformation" > Error message displayed when unable to convert to number </param>
Internal double changetokentodouble (string errorinformation)
{
Double Dblvalue;
if (this. Tokenvaluetype!= typeof (Double))
{
if (double. TryParse (this. Tokenvalue.tostring (), out Dblvalue)
This. Tokenvaluetype = typeof (Double);
Else
throw new Syntaxexception (This.m_index, This.m_length, errorinformation);
}
Else
{
Dblvalue = (double) this. Tokenvalue;
}
return dblvalue;
}

<summary>
Converts a token value to a logical value
</summary>
internal BOOL Changetokentoboolean ()
{
BOOL Blnvalue = false;
if (this. Tokenvaluetype = = typeof (String)
{
Switch (this. Tokenvalue.tostring (). Trim (). ToLower ())
{
Case "true":
Blnvalue = (bool) (this. Tokenvalue = True);
Break
Case "false":
Case "":
Default
Blnvalue = (bool) (this. Tokenvalue = False);
Break
}
This. Tokenvaluetype = typeof (bool);
}
else if (this. Tokenvaluetype = = typeof (Double)
{
Blnvalue = (bool) (Convert.ToInt32 (this. Tokenvalue)!= 0)? (This. Tokenvalue = True): (this. Tokenvalue = false));
Check if the previous line of code is wrong
This. Tokenvaluetype = typeof (bool);
}
else if (this. Tokenvaluetype = = typeof (bool))
{
Blnvalue = (bool) this. Tokenvalue;
}
Else
{
}

return blnvalue;
}

#endregion

}//class Tokenrecord

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.