日期:2013-06-20  浏览次数:20480 次

using System;
using System.Collections;

namespace XXXXX
{
 public class Expression
 {
  private Expression() {}

  #region No01.表达式分割为ArrayList形式
  /// <summary>
  /// 要求表达式以空格\t作为分隔符
  /// 转换表达式折分为:
  /// 变量及数值 ,变量不允许为@
  /// 字符串“”
  /// 运算符号{+、-、*、/、++、+=、--、-=、*=、/=、!、!=、>、>=、>>、<、<=、<>、|、|=、||、&、&=、&&}
  /// 括号{包括(、)}
  /// </summary>
  /// <param name="sExpression"></param>
  /// <returns></returns>
  public static ArrayList ConvertExpression(string sExpression)
  {
   ArrayList alist = new ArrayList();

   string word = null;
   int    i    = 0;
   string   c    = "";
 
   while(i < sExpression.Length)
   {
    #region "
    if (word != null && word != "")
    if (word.Substring(0,1) == "\"")
    {
     do
     {
      c = sExpression[i++].ToString();
      if (c == "\"") {  alist.Add(word + c); word = c =  null; break;}
      else { word += c; c = null; }
     }while(i < sExpression.Length);
    }
    if (i > sExpression.Length -1)
    { alist.Add(word); alist.Add(c); word = c= null; break;}
    #endregion

    #region 字符判别
    switch (c = sExpression[i++].ToString())
    {
      #region ( )
     case "\"":
      if (i > sExpression.Length -1)
      { alist.Add(word); alist.Add(c); word = c= null; break;}
      else
      {
       word = c; c= null;
       do
       { c = sExpression[i++].ToString();
        if (c == "\"") {  alist.Add(word + c); word = c =  null; break;}
        else { word += c; c = null; }
       }while(i < sExpression.Length );
       break;
      }

     case "(": alist.Add(word); alist.Add(c); word = c= null;  break;
     case ")": alist.Add(word); alist.Add(c); word = c= null;  break;
     case " ": alist.Add(word); word = c= null;  break;
      #endregion

      #region + - * / %
     case "+":
      if (i > sExpression.Length -1)
      { alist.Add(word); alist.Add(c); word = c= null; }
      else
       switch (c = sExpression[i++].ToString())
       {