如何在C#中将字符串公式转换为数学公式 [英] How to convert string formula to a mathematical formula in C#

查看:1060
本文介绍了如何在C#中将字符串公式转换为数学公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个带有数学公式的字符串变量:

字符串myformula ="3 * 5 + Pow(2,3)";

If I have a string variable with a mathematical formula:

string myformula = "3 * 5 + Pow(2,3)";

How can I convert this string to a mathematical formula that the compiler can calculate?

推荐答案

如果您不想花数周的时间编写自己的代码,我建议如何将其转换为编译器可以计算的数学公式?您会发现一个启用此类功能的第三方类库.首先,您必须解析字符串,验证数学,然后正确执行数学.这可不是一件容易的事,即使对于最有经验的程序员来说,这也是艰巨的任务.
If you don''t want to spend weeks writing your own code, I recommend that you find a 3rd-party class library that enables this kind of thing. First, you have to parse the string, validate the math, and then execute the math correctly. This is no small feat, and would prove to be a daunting task for even the most seasoned programmers.


您也可以为此目的在应用程序中嵌入脚本语言.看来IronPython是最喜欢的一个(例如,请参见此处 [ ^ ]).
You may also embed a scripting language in your application for the purpose. It looks like IronPython is the most preferred one (see, for instance here[^]).


您对以前有关此问题的要求的回答有什么问题? ( C#中的动态数学公式生成器 [
What problems did you have with my answer to your previous request regarding this problem? (Dynamic Mathematical Formula Builder in C#[^]).

The tool is free and perfectly fit for your purpose. What exactly is your problem with that solution. If you tell me maybe I can help you.

Cheers!

—MRB


这篇关于如何在C#中将字符串公式转换为数学公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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