Is there a method that will evaluate a string and produce an integer (assuming the string is an equation) in C#

烈酒焚心 提交于 2019-11-29 18:42:47

There isn't exactly such a method in C#, but you can easily write one using the compiler at runtime to evaluate the expression. Check out CSharpCodeProvider.

You can use the Microsoft.JScript which exposes all client side javascript functions in C#

using System;
using Microsoft.JScript;
using Microsoft.JScript.Vsa;

namespace JustTest
{
    class Program
    {
        static void Main(string[] args)
        {
            object value = Eval.JScriptEvaluate("1+2-(3*5)", VsaEngine.CreateEngine());
            Console.Write(value);
            Console.ReadLine();
        }
    }
}

Try this it uses CodeDOM to create a calculator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!