/// <summary>
///
/// </summary>
/// <param name="AText"> </param>
/// <param name="ALStr"> </param>
/// <param name="ACStr"> </param>
/// <param name="ARStr"> </param>
static void Bracket( string AText ,
out string ALStr, out string ACStr, out string ARStr)
{
int L = 0 , R = 0 ; //
ALStr = ACStr = ARStr = "" ;
for ( int i = 0 ; i < AText . Length; i ++ )
{
if (AText[i] == '(' ) L ++ ;
else if (AText[i] == ')' ) R ++ ;
if (L == 0 ) ALStr += AText[i];
else if (L > R) ACStr += AText[i];
else
{
ARStr = AText . Substring(i + 1 );
break ;
}
}
if (ACStr . Length >= 1 ) ACStr = ACStr . Remove( 0 , 1 );
}
static string fCalc( string AText)
{
if (AText . IndexOf( '(' ) >= 0 )
{
string L, C, R;
Bracket(AText, out L, out C, out R);
return fCalc(L + fCalc(C) + R);
}
else if ((AText . IndexOf( '+' ) >= 0 ) || (AText . IndexOf( '-' ) >= 0 ))
{
int i = AText . IndexOf( '+' );
int j = AText . IndexOf( '-' );
if (i < 0 ) i = AText . Length - 1 ;
if (j < 0 ) j = AText . Length - 1 ;
int K = Math . Min(i, j);
string L = AText . Substring( 0 , K);
string R = AText . Substring(K + 1 );
if (L . Length <= 0 ) L = "0" ;
if (R . Length <= 0 ) R = "0" ;
if (i == K)
return ( double . Parse(fCalc(L)) + double . Parse(fCalc(R))) . ToString();
else return ( double . Parse(fCalc(L)) - double . Parse(fCalc(R))) . ToString();
}
else if ((AText . IndexOf( '*' ) >= 0 ) || (AText . IndexOf( '/' ) >= 0 ))
{
int i = AText . IndexOf( '*' );
int j = AText . IndexOf( '/' );
if (i < 0 ) i = AText . Length - 1 ;
if (j < 0 ) j = AText . Length - 1 ;
int K = Math . Min(i, j);
string L = AText . Substring( 0 , K);
string R = AText . Substring(K + 1 );
if (L . Length <= 0 ) L = "0" ;
if (R . Length <= 0 ) R = "0" ;
if (i == K)
return ( double . Parse(fCalc(L)) * double . Parse(fCalc(R))) . ToString();
else return ( double . Parse(fCalc(L)) / double . Parse(fCalc(R))) . ToString();
}
else if ((AText . IndexOf( '_' ) >= 0 ))
return ( - double . Parse(fCalc(AText . Substring( 1 )))) . ToString();
else return double . Parse(AText) . ToString();
}
/// <summary>
///
/// </summary>
/// <param name="AText"> </param>
/// <returns> </returns>
static string Calc( string AText)
{
string vText = "" ;
int L = AText . Length;
#region
for ( int i = 0 ; i < L; i ++ )
{
if ((AText[i] == '-' ) && (i + 1 < L) &&
( "+-()" . IndexOf(AText[i + 1 ]) < 0 ))
{
if ((i == 0 ) || ((i > 0 ) && ( "*/" . IndexOf(AText[i - 1 ]) >= 0 )))
vText += "_" ;
else if (((i > 0 ) && ( "+-" . IndexOf(AText[i - 1 ]) >= 0 )) ||
((i + 1 < L) && ( "+-()" . IndexOf(AText[i + 1 ]) < 0 )))
vText += "+_" ;
else vText += AText[i];
}
else vText += AText[i];
}
#endregion
return fCalc(vText);
}