如何从字符串中解析算术运算符?

发布于 2024-09-24 21:29:26 字数 66 浏览 1 评论 0原文

是否可以为 int 变量分配一个以字符串形式编写的表达式结果的值?例如,我有一个字符串“5 - 3”,预期结果是 2。

Is it possible to assign an int variable a value that is a result of expression written in a string? E.g. I have a string "5 - 3" and the expected result is 2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风向决定发型 2024-10-01 21:29:26

您可以使用 计算器.NET

You can use Calculator.NET

看轻我的陪伴 2024-10-01 21:29:26

如果您喜欢自己编写(我就是这么做的,这很有趣!)。查看维基百科:

调车场算法
逆波兰表示法

伪代码非常清晰,是一次很棒的学习体验

If you fancy writing your own (I did, it's fun!). Have a look on Wikipedia for:

Shunting Yard Algorithm and
Reverse Polish Notation

The pseudo-code is really clear and it's a great learning experience

独自唱情﹋歌 2024-10-01 21:29:26

该框架没有内置任何内容,因此您可以构建自己的数学表达式解析器,或者使用现有的众多库之一。

搜索“.net 算术解析器”会出现许多结果,有些是免费的,有些不是。

例如:

There is nothing built into the framework, so you can either built your own mathematical expression parser, or use one of the many libraries out there.

A search on ".net arithmetic parser" comes up with many results, some free, some not.

For instance:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文