C # Calculation Expression (similar to the calculator function ),
1. Using MSScriptControl to execute JavaScript code in C #. Anyone who has used an eval method in javascript knows its convenience and strength.
In C #, we can also use the Com component to execute javascript code.
The following code demonstrates how to use the MSScriptControl component to execute a mathematical expression:
MSScriptControl.ScriptControlClass sc = new MSScriptControl.ScriptControlClass();sc.Language = "javascript";object obj = sc.Eval("500-(110-100)/2*6");Console.WriteLine(obj);
To use MSScriptControl, you must reference the com component Microsoft Script Control 1.0.
2. reference the Microsoft. JScript and Microsoft. Vsa namespaces and call the JScriptEvaluate Method for implementation.
Note: The Microsoft. JScript and Microsoft. Vsa namespaces must be added. (Expired)
/// <Summary> /// four mixed operations /// </summary> /// <param name = "sExpression"> Calculation Expression </param> /// <returns> calculation result </returns> /// <example> /// <code> // object x = Mathx. eval ("500-(110-100)/2*6"); // </code> /// </example> public static object Eval (string sExpression) {Microsoft. JScript. vsa. vsaEngine ve = Microsoft. JScript. vsa. vsaEngine. createEngine (); return Microsoft. JScript. eval. JScriptEvaluate (sExpression, ve );}