C # Calculation Expression (similar to the calculator function ),

Source: Internet
Author: User

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 );}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.