评估存储在一个字符串算术前pression(C#) [英] evaluate an arithmetic expression stored in a string (C#)

查看:132
本文介绍了评估存储在一个字符串算术前pression(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在C#应用程序中,我想计算出给定为字符串的算术前pression。
所以像我得到的字符串:

I'm working on a application in C# in which I want to calculate an arithmetic expression that is given as a string. So like I got a string:

string myExpr="4*(80+(5/2))+2";

和我要计算的算术前pression的结果。
而在例如JavaScript,PHP等,你可以只用eval做的伎俩,这并不语言似乎是在C#中的一个选项。

And I want to calculate the outcome of the arithmetic expression. While in a language such as Javascript, PHP etc. you could just use Eval to do the trick this doesnt seem to be an option in C#.

我想这是可以写一个code以devide到无数简单的前pressions,计算并添加在一起,但是这将需要相当长的一段时间,我可能有很多的烦恼我试图这样做。

I suppose it is possible to write a code to devide it into countless simple expressions, calculate them and add them together but this would take quite some time and I'm likely to have lots of troubles in my attempt to do so.

所以...我的问题,有没有'简单'的方式来做到这一点?

So... my question, Is there any 'simple' way to do this?

推荐答案

有一个JavaScript库,你可以参考一下,然后就这样做:

There's a javascript library you can reference, then just do something like:

var engine = VsaEngine.CreateEngine();
Eval.JScriptEvaluate(mySum, engine);

Edit(编辑)
图书馆是Microsoft.JScript程序

Edit; Library is Microsoft.JScript

这篇关于评估存储在一个字符串算术前pression(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆