• .Net实现表达式计算(公式) 表达式字符串


    文档原址:http://blog.csdn.net/fangxing80/article/details/5992661

    实现复杂公式计算,比如含IF分支判断等,可考虑通过调用EXCEL公式获取值。

    Excel公式计算参考:http://www.cnblogs.com/senyier/p/3498526.html

    方法一: MSScriptControl.ScriptControl

    VB.NET

     

    1. Dim exp As String = "3 + 4"  
    2. Dim t As Type = Type.GetTypeFromProgID("MSScriptControl.ScriptControl")  
    3. Dim obj As Object = Activator.CreateInstance(t)  
    4.   
    5. t.InvokeMember("Language", System.Reflection.BindingFlags.SetProperty, _  
    6.            Nothing, obj, New Object() {"vbscript"})  
    7.   
    8. Dim result As Object = t.InvokeMember("Eval", System.Reflection.BindingFlags.InvokeMethod, _  
    9.                                        Nothing, obj, New Object() {exp})  
    10. MsgBox("method 1: " & CStr(result))  

     

    C#

    [c-sharp] view plaincopy
    1. var exp = "3 + (2 + 3)/5";  
    2. var type = Type.GetTypeFromProgID("MSScriptControl.ScriptControl");  
    3. var obj = Activator.CreateInstance(type);  
    4. type.InvokeMember("Language", BindingFlags.SetProperty, null,   
    5.                   obj, new object[] { "javascript" });  
    6.   
    7. var result = type.InvokeMember("Eval", BindingFlags.InvokeMethod, null,  
    8.                                obj, new object[] { exp });  
    9. Console.WriteLine("{0} = {1}", exp, result);  

     

    方法二: CodeDOM

    VB.NET

     

    1. Dim oCodeProvider As VBCodeProvider = New VBCodeProvider  
    2. Dim oCParams As CompilerParameters = New CompilerParameters  
    3. Dim oCResults As CompilerResults = Nothing  
    4. Dim oAssy As System.Reflection.Assembly = Nothing  
    5. Dim oExecInstance As Object = Nothing  
    6. Dim oRetObj As Object = Nothing  
    7. Dim oMethodInfo As MethodInfo = Nothing  
    8. Dim oType As Type = Nothing  
    9. Dim strSource As String = _  
    10.     "Public Class MainClass " + vbCrLf + _  
    11.     "   Public Shared Function Eval() As Integer" + vbCrLf + _  
    12.     "      Return 3 + 4" + vbCrLf + _  
    13.     "   End Function" + vbCrLf + _  
    14.     "End Class"  
    15. oCParams.CompilerOptions = "/t:library"  
    16. oCParams.GenerateInMemory = True  
    17.   
    18. oCResults = oCodeProvider.CompileAssemblyFromSource(oCParams, strSource)  
    19.   
    20. If oCResults.Errors.Count <> 0 Then  
    21.     MsgBox("Error")  
    22. End If  
    23. oAssy = oCResults.CompiledAssembly  
    24.   
    25. 'oExecInstance = oAssy.CreateInstance("MainClass")  
    26. 'oType = oExecInstance.GetType  
    27. 'oMethodInfo = oType.GetMethod("Eval")  
    28. 'oRetObj = oMethodInfo.Invoke(oExecInstance, Nothing)  
    29.   
    30. oType = oAssy.GetType("MainClass")  
    31. oRetObj = oType.InvokeMember("Eval", BindingFlags.InvokeMethod, NothingNothingNothing)  
    32. MsgBox("method 2: " & CStr(oRetObj))  

     

    C#

    [c-sharp] view plaincopy
    1. var exp = "3 + (2 + 3)/5";  
    2. var csCodeProvider = new CSharpCodeProvider();  
    3. var csParams = new CompilerParameters();  
    4. var source = "public class MainClass { public static object Eval() { return (#exp#); } }";  
    5. source = source.Replace("#exp#", exp);  
    6. csParams.CompilerOptions = "/t:library";  
    7. csParams.GenerateInMemory = true;  
    8.   
    9. var csResults = csCodeProvider.  
    10.                 CompileAssemblyFromSource(csParams, source);  
    11. if (csResults.Errors.Count > 0)  
    12. {  
    13.     Console.WriteLine(csResults.Errors[0].ToString());  
    14.     return;  
    15. }  
    16.   
    17. var ass = csResults.CompiledAssembly;  
    18. var type = ass.GetType("MainClass");  
    19. var result = type.InvokeMember("Eval", BindingFlags.InvokeMethod,   
    20.                                 nullnullnull);  
    21.   
    22. Console.WriteLine("{0} = {1}", exp, result);  

     

    方法三: DataColumn.Expression & DataTable.Compute方法。

    VB.NET

    1. Dim dt As DataTable = New DataTable  
    2. dt.Columns.Add("Val1"GetType(Integer))  
    3. dt.Columns.Add("Val2"GetType(Integer))  
    4. dt.Columns.Add("Result").Expression = String.Format("Val1 + Val2"Me.TextBox1.Text)  
    5.   
    6. dt.Rows.Add(New Object() {3, 4})  
    7.   
    8. MsgBox("method 3: " & dt.Rows(0)("Result"))  

    C#

    [c-sharp] view plaincopy
    1. var exp = "3 + (2 + 3)/5";  
    2. DataTable dt = new DataTable();  
    3. dt.Columns.Add("Result").Expression = exp;  
    4. dt.Rows.Add(dt.NewRow());  
    5.   
    6. var result = dt.Rows[0]["Result"];  
    7.   
    8. Console.WriteLine("{0} = {1}", exp, result);  
  • 相关阅读:
    刷题86—动态规划(三)
    刷题85—动态规划(二)— 股票6道
    刷题84—动态规划(一)
    刷题83——硬币
    刷题82——二叉树的右视图
    刷题81——统计「优美子数组」
    android adb 流程原理代码分析(一)
    android默认开启adb调试方法分析
    recovery 下界面UI旋转90 180 270修改
    sublime使用Package Control不能正常使用的解决办法
  • 原文地址:https://www.cnblogs.com/senyier/p/3498570.html
Copyright © 2020-2023  润新知