如何把字符串 "100*23/(23+78)"  计算他的值? 编成语言: c# .业务背景:用户定义的公式为 a*b/(c+d) . 系统把里面的参数替换后为 "100*23/(23+78)"
问题是如何计算它的值。

解决方案 »

  1.   

    或许用正则表达式可以把各个部分提取出来就可以了.
    甚至可以用string类的split()也可以.
      

  2.   

    可以用jscript命名空间里的函数
      

  3.   

    using System;
    using System.Text;
    using System.CodeDom.Compiler;
    using Microsoft.VisualBasic;
    using System.Reflection;class Evaluator
        {
            //private static CodeDomProvider comp = new CSharpCodeProvider();
            private static CodeDomProvider comp = new VBCodeProvider();
            private static CompilerParameters cp = new CompilerParameters();
            private static MethodInfo mi;        public static object Eval(string expression)
            {
                StringBuilder codeBuilder = new StringBuilder();            codeBuilder.AppendLine("Imports System");
                codeBuilder.AppendLine("Imports System.Math");
                codeBuilder.AppendLine("Imports Microsoft.VisualBasic");
                codeBuilder.AppendLine();
                codeBuilder.AppendLine("Public Module Mode");
                codeBuilder.AppendLine("   Public Function Func() As Object");
                codeBuilder.AppendLine("        Return " + expression);
                codeBuilder.AppendLine("   End Function");
                codeBuilder.AppendLine("End Module");            cp.ReferencedAssemblies.Add("System.dll");
                cp.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");
                cp.GenerateExecutable = false;
                cp.GenerateInMemory = true;            string code = codeBuilder.ToString();
                CompilerResults cr = comp.CompileAssemblyFromSource(cp, code);            if (cr.Errors.HasErrors)
                {
                    return null;
                }
                else
                {
                    Assembly a = cr.CompiledAssembly;
                    Type t = a.GetType("Mode");
                    //object mode = a.CreateInstance("Mode");
                    mi = t.GetMethod("Func", BindingFlags.Static | BindingFlags.Public);
                    return mi.Invoke(null, new object[0]);
                }
            }
        }//........
    public static void Main()
    {
        Console.WriteLine(Evaluator.Eval("100*23/(23+78)").ToString());
        Console.ReadKey();
    }
      

  4.   

    crossbowvic(漫步的兔子),你太强了!
      

  5.   

    不知道这个是不是你想要的.
    VB的代码.你可以仔细看看,应该可以看明白.
    Module modCalculate    Const pi As Double = 3.1415926535    Public Function CalculateString(ByVal Exppression As String, ByRef Good As Boolean, ByVal xxx As Double) As Double        Dim isg As Boolean
            Dim ExppressionLength As Integer
            Dim pp As Integer
            pp = 1
            ExppressionLength = Len(Exppression)
            CalculateString = E(UCase(Exppression), isg, pp, xxx)
            Good = isg
            If pp <= ExppressionLength Then Good = False
        End Function
        Private Function E(ByVal Exppression As String, ByRef Good As Boolean, ByRef Position As Integer, ByVal xxx As Double) As Double
            Dim tmp1 As Double
            Dim tmp2 As Double
            Dim isg As Boolean
            On Error GoTo Eerr        tmp1 = T(Exppression, isg, Position, xxx)
            If Match(Exppression, "+", Position) Then
                Position = Position + 1
                tmp2 = E2(Exppression, isg, Position, xxx)
                If Not (isg) Then
                    Good = False
                    Exit Function
                End If
                E = tmp1 + tmp2
            Else
                If Match(Exppression, "-", Position) Then
                    Position = Position + 1
                    tmp2 = E2(Exppression, isg, Position, xxx)
                    If Not (isg) Then
                        Good = False
                        Exit Function
                    End If
                    E = tmp1 - tmp2
                Else                E = tmp1
                End If
            End If
            Good = isg
            Exit Function
    Eerr:
            Good = False
        End Function
        Private Function E2(ByVal Exppression As String, ByRef Good As Boolean, ByRef Position As Integer, ByVal xxx As Double) As Double
            Dim tmp1 As Double
            Dim tmp2 As Double
            Dim isg As Boolean
            tmp1 = T2(Exppression, isg, Position, xxx)
            If Match(Exppression, "+", Position) Then
                Position = Position + 1
                tmp2 = E2(Exppression, isg, Position, xxx)
                If Not (isg) Then
                    Good = False
                    Exit Function
                End If            E2 = tmp1 + tmp2
            Else
                If Match(Exppression, "-", Position) Then
                    Position = Position + 1
                    tmp2 = E2(Exppression, isg, Position, xxx)
                    If Not (isg) Then
                        Good = False
                        Exit Function
                    End If                E2 = tmp1 - tmp2
                Else                E2 = tmp1
                End If
                Good = isg
            End If
        End Function
        Private Function T(ByVal Exppression As String, ByRef Good As Boolean, ByRef Position As Integer, ByVal xxx As Double) As Double
            Dim tmp1 As Double
            Dim tmp2 As Double
            Dim isg As Boolean        tmp1 = F(Exppression, isg, Position, xxx)
            If isg Then
                If Match(Exppression, "\", Position) Then
                    Position = Position + 1
                    tmp2 = T2(Exppression, isg, Position, xxx)
                    If Not (isg) Then
                        Good = False
                        Exit Function
                    End If                T = CInt(tmp1) \ CInt(tmp2)
                Else
                    If Match(Exppression, "^", Position) Then
                        Position = Position + 1
                        tmp2 = T2(Exppression, isg, Position, xxx)
                        If Not (isg) Then
                            Good = False
                            Exit Function
                        End If                    T = tmp1 ^ tmp2
                    Else
                        If Match(Exppression, "*", Position) Then
                            Position = Position + 1
                            tmp2 = T2(Exppression, isg, Position, xxx)
                            If Not (isg) Then
                                Good = False
                                Exit Function
                            End If                        T = tmp1 * tmp2                    Else
                            If Match(Exppression, "/", Position) Then
                                Position = Position + 1
                                tmp2 = T2(Exppression, isg, Position, xxx)
                                If Not (isg) Then
                                    Good = False
                                    Exit Function
                                End If                            T = tmp1 / tmp2
                            Else                            T = tmp1
                            End If
                        End If
                    End If
                End If
            End If
            Good = isg
        End Function
        Private Function T2(ByVal Exppression As String, ByRef Good As Boolean, ByRef Position As Integer, ByVal xxx As Double) As Double
            Dim tmp1 As Double
            Dim tmp2 As Double
            Dim isg As Boolean
            tmp1 = F2(Exppression, isg, Position, xxx)
            If isg Then
                If Match(Exppression, "\", Position) Then
                    Position = Position + 1
                    tmp2 = T2(Exppression, isg, Position, xxx)
                    If Not (isg) Then
                        Good = isg
                        Exit Function
                    End If
                    T2 = CInt(tmp1) \ CInt(tmp2)
                Else
                    If Match(Exppression, "^", Position) Then
                        Position = Position + 1
                        tmp2 = T2(Exppression, isg, Position, xxx)
                        If Not (isg) Then
                            Good = isg
                            Exit Function
                        End If
                        T2 = tmp1 ^ tmp2
                    Else
                        If Match(Exppression, "*", Position) Then
                            Position = Position + 1
                            tmp2 = T2(Exppression, isg, Position, xxx)
                            If Not (isg) Then
                                Good = isg
                                Exit Function
                            End If
                            T2 = tmp1 * tmp2
                        Else
                            If Match(Exppression, "/", Position) Then
                                Position = Position + 1
                                tmp2 = T2(Exppression, isg, Position, xxx)
                                If Not (isg) Then
                                    Good = isg
                                    Exit Function
                                End If
                                T2 = tmp1 / tmp2
                            Else
                                T2 = tmp1
                            End If '/
                        End If '*                End If '^
                End If '\
            End If
            Good = isg
        End Function
        Private Function F(ByVal Exppression As String, ByRef Good As Boolean, ByRef Position As Integer, ByVal xxx As Double) As Double
            Dim Tmp As Double
            Dim isg As Boolean
            Tmp = OpDigital(Exppression, isg, Position, xxx)
            If Not (isg) Then Tmp = g(Exppression, isg, Position, xxx)
            F = Tmp
            Good = isg
        End Function
        Private Function F2(ByVal Exppression As String, ByRef Good As Boolean, ByRef Position As Integer, ByVal xxx As Double) As Double
            Dim Tmp As Double
            Dim isg As Boolean
            Tmp = NonopDigital(Exppression, isg, Position, xxx)
            If Not (isg) Then Tmp = g(Exppression, isg, Position, xxx)
            F2 = Tmp
            Good = isg
        End Function
      

  6.   

    Private Function g(ByVal Exppression As String, ByRef Good As Boolean, ByRef Position As Integer, ByVal xxx As Double) As Double
            Dim tmp1 As Double
            Dim tmp2 As Double
            Dim isg As Boolean
            Call PassBlank(Exppression, Position)
            If Match(Exppression, "SIN", Position) Then
                Position = Position + 3
                Call PassBlank(Exppression, Position)
                g = System.Math.Sin(G2(Exppression, Good, Position, xxx))
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "ABS", Position) Then
                Position = Position + 3
                Call PassBlank(Exppression, Position)
                g = System.Math.Abs(G2(Exppression, Good, Position, xxx))
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "CUB", Position) Then
                Position = Position + 3
                Call PassBlank(Exppression, Position)
                g = (G2(Exppression, Good, Position, xxx)) ^ (1 / 3)
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "SQR", Position) Then
                Position = Position + 3
                Call PassBlank(Exppression, Position)
                g = System.Math.Sqrt(G2(Exppression, Good, Position, xxx))
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "SGN", Position) Then
                Position = Position + 3
                Call PassBlank(Exppression, Position)
                g = System.Math.Sign(G2(Exppression, Good, Position, xxx))
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "RND", Position) Then
                Position = Position + 3
                Call PassBlank(Exppression, Position)
                g = Rnd() * G2(Exppression, Good, Position, xxx)
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "COS", Position) Then
                Position = Position + 3
                Call PassBlank(Exppression, Position)
                g = System.Math.Cos(G2(Exppression, Good, Position, xxx))
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "TAN", Position) Then
                Position = Position + 3
                Call PassBlank(Exppression, Position)
                g = System.Math.Tan(G2(Exppression, Good, Position, xxx))
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "LOG", Position) Then
                Position = Position + 3
                Call PassBlank(Exppression, Position)
                g = System.Math.Log(G2(Exppression, Good, Position, xxx)) / System.Math.Log(10.0#)
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "LN", Position) Then
                Position = Position + 2
                Call PassBlank(Exppression, Position)
                g = System.Math.Log(G2(Exppression, Good, Position, xxx))
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "EXP", Position) Then
                Position = Position + 3
                Call PassBlank(Exppression, Position)
                g = System.Math.Exp(G2(Exppression, Good, Position, xxx))
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "ARCTAN", Position) Then
                Position = Position + 6
                Call PassBlank(Exppression, Position)
                g = System.Math.Atan(G2(Exppression, Good, Position, xxx))
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "ARCSIN", Position) Then
                Position = Position + 6
                Call PassBlank(Exppression, Position)
                tmp1 = G2(Exppression, Good, Position, xxx)
                If tmp1 <> 1 And tmp1 <> -1 Then
                    g = System.Math.Atan(tmp1 / System.Math.Sqrt(1 - tmp1 * tmp1))
                Else
                    If tmp1 = 1 Then g = pi / 2 Else g = -pi / 2
                End If
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "ARCCOS", Position) Then
                Position = Position + 6
                Call PassBlank(Exppression, Position)
                tmp1 = G2(Exppression, Good, Position, xxx)
                If tmp1 <> 1 And tmp1 <> -1 Then
                    g = System.Math.Atan(-tmp1 / System.Math.Sqrt(1 - tmp1 * tmp1)) + pi / 2
                Else
                    If tmp1 = 1 Then g = 0.0# Else g = pi
                End If
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "POW", Position) Then
                Position = Position + 3
                Call PassBlank(Exppression, Position)
                If Match(Exppression, "(", Position) Then
                    Position = Position + 1
                    tmp1 = E(Exppression, isg, Position, xxx)
                    If Match(Exppression, ",", Position) And isg Then
                        Position = Position + 1
                        tmp2 = E(Exppression, isg, Position, xxx)
                        If Match(Exppression, ")", Position) And isg Then
                            Position = Position + 1
                            Call PassBlank(Exppression, Position)
                            Good = True
                            g = tmp1 ^ tmp2
                            Exit Function
                        End If
                    End If
                End If
                Good = False
                Exit Function
            End If
            g = G2(Exppression, Good, Position, xxx)
            Call PassBlank(Exppression, Position)
        End Function
      

  7.   

    Private Function G2(ByVal Exppression As String, ByRef Good As Boolean, ByRef Position As Integer, ByVal xxx As Double) As Double
            Dim Tmp As Double
            Dim isg As Boolean
            If Match(Exppression, "(", Position) Then
                Position = Position + 1
                Call PassBlank(Exppression, Position)
                Tmp = E(Exppression, isg, Position, xxx)
                If isg And Match(Exppression, ")", Position) Then
                    G2 = Tmp
                    Position = Position + 1
                    Good = True
                Else
                    Good = False
                End If
            End If
        End Function
        Private Function NonopDigital(ByVal Exppression As String, ByRef Good As Boolean, ByRef Position As Integer, ByVal xxx As Double) As Double
            Dim tmp1 As Double
            Dim tmp2 As Integer
            Dim tmpStr As String
            Call PassBlank(Exppression, Position)
            If Match(Exppression, "X", Position) Then
                NonopDigital = xxx
                Good = True
                Position = Position + 1
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            tmp1 = 0
            tmp2 = 0
            tmpStr = Mid(Exppression, Position, 1)
            If tmpStr >= "0" And tmpStr <= "9" Then
                While tmpStr >= "0" And tmpStr <= "9" Or tmpStr = " "
                    tmp2 = tmp2 + 1
                    tmpStr = Mid(Exppression, Position + tmp2, 1)
                End While
                If tmpStr = "." Then tmp2 = tmp2 + 1
                tmpStr = Mid(Exppression, Position + tmp2, 1)
                While tmpStr >= "0" And tmpStr <= "9" Or tmpStr = " "
                    tmp2 = tmp2 + 1
                    tmpStr = Mid(Exppression, Position + tmp2, 1)
                End While
                If tmpStr = "E" Then tmp2 = tmp2 + 1
                tmpStr = Mid(Exppression, Position + tmp2, 1)
                While tmpStr >= "0" And tmpStr <= "9" Or tmpStr = " "
                    tmp2 = tmp2 + 1
                    tmpStr = Mid(Exppression, Position + tmp2, 1)
                End While
                tmp1 = Val(Mid(Exppression, Position))
                Position = Position + tmp2
                Good = True
            Else
                Good = False
            End If
            NonopDigital = tmp1
        End Function
        Private Function OpDigital(ByVal Exppression As String, ByRef Good As Boolean, ByRef Position As Integer, ByVal xxx As Double) As Double
            Dim Tmp As Double
            Dim isg As Boolean
            Dim sign As Double
            sign = 1
            Call PassBlank(Exppression, Position)
            If Match(Exppression, "X", Position) Then
                OpDigital = xxx
                Good = True
                Position = Position + 1
                Call PassBlank(Exppression, Position)
                Exit Function
            End If
            If Match(Exppression, "-", Position) Then
                Position = Position + 1
                sign = -1
            Else
                If Match(Exppression, "+", Position) Or Mid(Exppression, Position, 1) >= "0" And Mid(Exppression, Position, 1) <= "9" Then
                    sign = 1
                Else
                    Good = False
                    Exit Function
                End If
            End If
            Tmp = F2(Exppression, isg, Position, xxx)
            If Not (isg) Then Good = False Else Good = True
            OpDigital = Tmp * sign
        End Function
        Private Function Match(ByVal Exppression As String, ByVal Exppression2 As String, ByRef Position As Integer) As Boolean
            If Mid(Exppression, Position, Len(Exppression2)) = Exppression2 Then Match = True Else Match = False
        End Function
        Private Sub PassBlank(ByVal Exppression As String, ByRef Position As Integer)
            While Mid(Exppression, Position, 1) = " "
                Position = Position + 1
            End While
        End SubEnd Module
      

  8.   

    http://www.sooweb.net/Article/Programme/NET/1697.html