Stack string expression Calculation

Source: Internet
Author: User

 

Recently, the computing problem has been frequently solved. The requirement for implementing string expressions to calculate the returned result value is as follows:

CsStack. cs:

Using System;

Namespace PYHB
{
/// <Summary>
/// Stack heap settings.

/// Date: 2005-05-17
/// </Summary>
Public class clsStack
{
Private long Top; // The maximum number of stacks
Private int MaxSize; // MaxSize stack capacity
Private string [] Element;
Public clsStack ()
{
//
// TODO: add the constructor logic here
//
Top =-1;
}
/// <Summary>
/// Set the maximum stack capacity
/// </Summary>
/// <Param name = "Size"> </param>
Public void Initialize (int Size)
{
MaxSize = Size;
Element = new string [Size];
}
/// <Summary>
/// Stack entry
/// </Summary>
/// <Param name = "strItem"> </param>
Public void Push (string strItem)
{
If (! IsFull ())
{
Top = Top + 1;
Element [Top] = strItem;
}

}
/// <Summary>
/// Output Stack
/// </Summary>
/// <Returns> </returns>
Public string Pop ()
{
String strRtn = "";
If (! IsEmptly ())
{
StrRtn = Element [Top];
Top = Top-1;
}
Return strRtn;
}
Public string GetTop ()
{
String strRtn = "";
If (! IsEmptly ())
{
StrRtn = Element [Top];
}
Return strRtn;
}
Public bool IsFull ()
{
Bool IsFull = Top = (MaxSize-1 )? True: false;
Return IsFull;
}
Public void MakeEmptly ()
{
Top =-1;
}
Public bool IsEmptly ()
{
Bool IsEmptly = Top =-1? True: false;
Return IsEmptly;
}

}
}

Calculate. cs

Using System;
Using System. Text;
Using System. Windows. Forms;

Namespace PYHB
{
/// <Summary>
/// String expression calculation implementation, returns the string array of calculated results
 
/// Date: 2005-05-17
/// </Summary>
Public class Calculate
{
Private clsStack S = new clsStack ();
Public Calculate ()
{
//
// TODO: add the constructor logic here
//
}


/// <Summary>
/// Returns the string array of the calculated result based on the numeric expression string.
/// </Summary>
/// <Param name = "strSoure"> strSour is an expression string with no "#" in the header. Add "#" at the end. </param>
/// <Returns> calculation result </returns>
Public string [] Run (string [] strSoure)
{
If (strSoure = null)
{
Return null;
}
String [] dRtn = new string [strSoure. Length];
For (int k = 0; k <strSoure. Length; k ++)
{
String [] ATemp;
String strRPN;
StrRPN = GetRPN (strSoure [k]);
Try
{
ATemp = strRPN. Trim (). Split ();
For (int I = 0; I <ATemp. Length; I ++)
{
If (sysFun. IsNumber (ATemp [I])
S. Push (ATemp [I]);
Else
DoOperate (ATemp [I]);
}

DRtn [k] = S. Pop ();
}
Catch {}
}
Return dRtn;
}
/// <Summary>
/// Run returns a suffix expression.
/// StrSour is an expression string with no "#" in the header. You need to add "#" at the end.
/// String suffix expression String with no "#" at the beginning or end

/// </Summary>
/// <Param name = "strSource"> </param>
/// <Returns> </returns>
Private string GetRPN (string strSource)
{
String [] ATemp;
String strRPN = "", Y;
ATemp = strSource. Trim (). Split ();
S. Initialize (ATemp. Length );
S. MakeEmptly ();
S. Push ("#");
Try
{
For (int k = 0; k <ATemp. Length; k ++)
{
// Number
If (sysFun. IsNumber (ATemp [k])
{
StrRPN + = "" + ATemp [k];
}
// Character
Else
{
If (ATemp [k] = ")")
{
Do
{
Y = S. Pop ();
If (Y! = "(")
StrRPN + = "" + Y;
}
While (Y. Trim ()! = "(");
}
Else
{
Do
{
Y = S. Pop ();
If (GetISP (Y)> GetICP (ATemp [k])
StrRPN + = "" + Y;
}
While (GetISP (Y)> GetICP (ATemp [k]);
S. Push (Y );
S. Push (ATemp [k]);
}
}
}
Do
{
Y = S. Pop ();
If (Y! = "#")
StrRPN + = "" + Y;
}
While (Y! = "#");
}
Catch {}
Return strRPN;
}

# Region operator priority Definition
Private enum isp
{
S35 = 0,
S40 = 1,
S94 = 7,
S42 = 5,

Related Article

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.