将:
((([张三]-[李四])+([王五]-[赵六]))*([松七]))*-1
替换为:
((([2.0]-[2.0])+([2.0]-[2.0]))*([2.0]))*-1也就是说想法验证((([张三]-[李四])+([王五]-[赵六]))*([松七]))*-1
这个表达式是否能计算也结果。请各位高手帮忙。我的想法是用正则表达式,可是我不会。

解决方案 »

  1.   

    这个直接计算就是了,计算不出来就算失败了就是了。
    ——正则也不能告诉你能替换就一定能计算出来啊VB源代码:
    http://www.codeproject.com/vb/net/expression_evaluator.asp*VS2005下有一定问题,会提示过期,不过还可以用
    引用:Microsoft.Vsa.dll
    引用:Microsoft.JScript.dllpublic static object EvalExpression(string strExpression)
    {
    Microsoft.JScript.Vsa.VsaEngine engine =
    Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
    return Microsoft.JScript.Eval.JScriptEvaluate(strExpression, engine);
    }调用:
    int ret = (int)EvalExpression("(8+2)/2+4"); // 9或者
    *只能计算+-*/
    http://www.moon-soft.com/doc/41118.htm //中序转换成后序表达式再计算
      // 如:23+56/(102-100)*((36-24)/(8-6))
      // 转换成:23|56|102|100|-|/|*|36|24|-|8|6|-|/|*|+"
      //以便利用栈的方式都进行计算。
      private string  CalculateParenthesesExpression(string Expression)
      {
       ArrayList operatorList = new ArrayList();
       string operator1;
       string ExpressionString = "";
       string operand3;
       Expression = Expression.Replace(" ","");
       while(Expression.Length > 0)
       {
        operand3 = "";
        //取数字处理
        if(Char.IsNumber(Expression[0]))
        {
         while(Char.IsNumber(Expression[0]))
         {
          operand3 += Expression[0].ToString() ;
          Expression = Expression.Substring(1);
          if(Expression == "")break;
         }
         ExpressionString += operand3 + "|";
        }    //取“C”处理
        if(Expression.Length >0 && Expression[0].ToString() == "(")
        {
         operatorList.Add("(");  
         Expression = Expression.Substring(1); 
        }    //取“)”处理
        operand3 = "";
        if(Expression.Length >0 && Expression[0].ToString() == ")")
        {
         do
         {
          
          if(operatorList[operatorList.Count -1].ToString() != "(")
          {
           operand3 += operatorList[operatorList.Count -1].ToString() + "|" ;
           operatorList.RemoveAt(operatorList.Count - 1) ;
          }
          else
          {
           operatorList.RemoveAt(operatorList.Count - 1) ;
           break;
          }
          
         }while(true);
         ExpressionString += operand3;
         Expression = Expression.Substring(1); 
        }    //取运算符号处理
        operand3 = "";
        if(Expression.Length >0 &&  (Expression[0].ToString() == "*" || Expression[0].ToString() == "/" || Expression[0].ToString() == "+" || Expression[0].ToString() == "-"))
        {
         operator1 = Expression[0].ToString();
         if(operatorList.Count>0)
         {
           
          if(operatorList[operatorList.Count -1].ToString() == "(" || verifyOperatorPriority(operator1,operatorList[operatorList.Count - 1].ToString()))
           
           {
            operatorList.Add(operator1); 
           }
           else  
           {
            operand3 += operatorList[operatorList.Count - 1].ToString() + "|"; 
            operatorList.RemoveAt(operatorList.Count - 1);
            operatorList.Add(operator1); 
            ExpressionString += operand3 ;
            
           }
          
         }
         else
         {
          operatorList.Add(operator1); 
         }
         Expression = Expression.Substring(1); 
        }
       }   operand3 = "";
       while(operatorList.Count != 0)
       {
        operand3 += operatorList[operatorList.Count -1].ToString () + "|";
        operatorList.RemoveAt(operatorList.Count -1); 
       }    ExpressionString += operand3.Substring(0, operand3.Length -1);  ;
          return CalculateParenthesesExpressionEx(ExpressionString);  }
      // 第二步:把转换成后序表达的式子计算
      //23|56|102|100|-|/|*|36|24|-|8|6|-|/|*|+"
      private string  CalculateParenthesesExpressionEx(string Expression)
      {
       //定义两个栈
       ArrayList operandList =new ArrayList();
       float operand1;
       float operand2;
       string[] operand3;
      
       Expression = Expression.Replace(" ","");
       operand3 = Expression.Split(Convert.ToChar("|"));
     
       for(int i = 0;i < operand3.Length;i++)
       {
        if(Char.IsNumber(operand3[i],0))
        {
         operandList.Add( operand3[i].ToString());
        }
        else
        {
          //两个操作数退栈和一个操作符退栈计算
         operand2 =(float)Convert.ToDouble(operandList[operandList.Count-1]);
         operandList.RemoveAt(operandList.Count-1);  
         operand1 =(float)Convert.ToDouble(operandList[operandList.Count-1]);
         operandList.RemoveAt(operandList.Count-1);
         operandList.Add(calculate(operand1,operand2,operand3[i]).ToString()) ;
        }
         
       }
       return operandList[0].ToString(); 
      }   //判断两个运算符优先级别
      private bool verifyOperatorPriority(string Operator1,string Operator2)
      {
       
       if(Operator1=="*" && Operator2 =="+") 
        return true;
       else if(Operator1=="*" && Operator2 =="-") 
        return true;
       else if(Operator1=="/" && Operator2 =="+") 
        return true;
       else if(Operator1=="/" && Operator2 =="-") 
        return true;
       else
        return false;
      }  //计算
      private float calculate(float operand1, float operand2,string operator2)
      {
       switch(operator2)
       {
        case "*":
         operand1 *=  operand2;
         break;
        case "/":
         operand1 /=  operand2;
         break;
        case "+":
         operand1 +=  operand2;
         break;
        case "-":
         operand1 -=  operand2;
         break;
        default:
         break;
       }
       return operand1;
      }
     
     }
    }'VB的源代码
    Option Explicit
    Private Declare Function EbExecuteLine Lib vba6.dll (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As LongPrivate Function ExecuteLine(sCode As String, Optional fCheckOnly As Boolean) As Boolean
        ExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, Abs(fCheckOnly)) = 0
    End Function
    Sub calc(ByVal x As String)
    Dim result
    ExecuteLine dim x as double
    ExecuteLine x= & x
    ExecuteLine clipboard.settext x
    result = Clipboard.GetText
    MsgBox x & = & result
    Set result = Nothing
    End SubPrivate Sub Command1_Click()
    calc Text1
    End SubPrivate Sub Form_Load()
    Text1.Text = 1+2*3-4/5
    End Sub
    ==================================================================
    博客空间:http://blog.csdn.net/lovingkiss
    资源下载:http://download.csdn.net/user/lovingkiss
    优惠接单开发,组件控件定制开发,成品源代码批发
    联系方式:Q64180940 全天在线
    ==================================================================
      

  2.   

    glacier111(★冰河★) 谢谢关注,受到启发,我试试看!!
      

  3.   

    using System.Text.RegularExpressions;
    ...
    string res="";
    string source=@"((([张三]-[李四])+([王五]-[赵六]))*([松七]))*-1";
    res=Regex.Replace(source,@"[\u4e00-\u9fa5]+","2.0");
    //
      

  4.   

    try:using System.Text.RegularExpressions;
    String s = "((([张三]-[李四])+([王五]-[赵六]))*([松七]))*-1";
                Regex _Regex = new Regex(@"\[\w+\]");
                string output = _Regex.Replace(s, @"[2.0]");
      

  5.   

    数字也不一定就可以计算的,除数为0,一样的崩溃的~~==================================================================
    博客空间:http://blog.csdn.net/lovingkiss
    资源下载:http://download.csdn.net/user/lovingkiss
    优惠接单开发,组件控件定制开发,成品源代码批发
    联系方式:Q64180940 全天在线
    ==================================================================
      

  6.   

    string res="";
    string source=@"((([张三]-[李四])+([王五]-[赵六]))*([松七]))*-1";
    res=Regex.Replace(source,@"[\u4e00-\u9fa5]+","2.0");
    这种方法我比较赞同!!