使用 C 语言计算中缀表达式的最简单方法是什么?

发布于 2024-07-30 06:50:35 字数 230 浏览 5 评论 0原文

假设用户输入一个中缀表达式作为字符串? 使用 C 语言评估该表达式的结果的最简单(最简单的意思是最短)方法是什么?

可能的方法是将其转换为后缀,然后使用堆栈。但这是一个相当漫长的过程。 有没有什么方法可以使用诸如 atoi()eval() 之类的函数来使工作变得更容易?

Suppose the user inputs an infix expression as a string?
What could be the easiest ( By easiest I mean the shortest) way to evaluate the result of that expression using C language?

Probable ways are converting it to a postfix then by using stacks.But its rather a long process.
Is there any way of using functions such as atoi() or eval() that could make the job easier?

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

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

发布评论

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

评论(6

吻泪 2024-08-06 06:50:36

当然,最有启发性的方法(甚至可能是最简单的,一旦您知道如何做)就是学习如何编写自己的 递归下降解析器。 C 中缀表达式的解析器不是很长。

这是其中一个 Eli Bendersky 关于解析的优秀博客文章。 (这个是与您最相关的一个,但我强烈推荐所有这些。)它包含中缀表达式解析器的源代码——诚然是用 Python 编写的,而不是 C 语言,但转换应该相当简单,而且您'在此过程中会学到很多东西。

Certainly the most instructive way (and possibly even the easiest, once you know how) is to learn how to write your own recursive descent parser. A parser for infix expressions in C isn't very long.

Here's one of a number of excellent blog posts by Eli Bendersky on parsing. (This one is the one that's most relevant to you, but I highly recommend all of them.) It contains source code for an infix expression parser -- admittedly in Python, not C, but the conversion should be fairly straightforward, and you'll learn a lot in the process.

飘然心甜 2024-08-06 06:50:36

你需要解析字符串。 C 中没有 eval()(与大多数静态语言一样),因此您需要编写自己的解析器或找到一些库来提供帮助。

由于最易于使用的解析器适用于 C++ 而不是 C,所以我宁愿使用完整的嵌入式语言。 我最喜欢的是 Lua,如果不包含这些库,它会非常轻量。 此外,语法比 C 更好,因此您的用户可能会更喜欢它。

当然,Lua 是一种成熟的编程语言,因此它可能不合适,或者也许它可以以其他方式提供帮助(以便更轻松地扩展应用程序)。

you need to parse the string. there's no eval() in C (as in most static languages), so you need to either write your own parser or find some library to help.

since most easy to use parsers are for C++ and not C, i'd rather use a full embeddable language. my absolute favorite is Lua, which can be incredibly lightweight if you don't include the libraries. also, the syntax is nicer than C's, so your users might like it better.

of course, Lua is a full-blown programming language, so it might not be appropriate, or maybe it could help in other ways (to make it easier to extend your application).

独自←快乐 2024-08-06 06:50:36

将字符串转换为标记数组,这些标记是操作数和运算符。
将中缀标记数组转换为逆波兰表示法数组。
等式进入 RPN 后,您可以将令牌从堆栈中弹出并对其进行操作。

查看关于逆波兰表示法的维基百科文章。 它显示了如何进行转换和计算。

Convert the string into an array of tokens which are the operands and operators.
Convert the infix token array to a Reverse Polish Notation array.
After the equation is in RPN, then you can pop tokens off the stack and operate on them.

Take a look at the Wikipedia article on Reverse Polish Notation. It shows how to do the conversion and the calculation.

妳是的陽光 2024-08-06 06:50:36

您需要构建某种脚本语言的解释器。

You need to build in interpreter of some scripting language.

淤浪 2024-08-06 06:50:36

一种干净的(可能不是简短的)方法是构建一棵树,就像编译器一样。

例如,假设您有表达式“2+3”。 “+”将是头部。 “2”将是左孩子,“3”将是右孩子。

由于每个表达式的计算结果都是一个值,因此该树可以扩展到无限复杂的表达式:它只需要按照每个运算符的优先级顺序进行排序。 低优先级运算符(如“+”)位于顶部,而高优先级运算符(如“*”)位于底部。然后,您将从下向上计算树上的表达式。

One clean (possible not short) way to do it is to build a tree, like a compiler would.

For example, say you have the expression "2+3". The '+' would be the head. The '2' would be the left child and the '3' would be the right child.

Since each expression evaluates to a value, this tree can be extended for infinitely complex expressions: it just needs to be sorted in order of precedence for each operator. Low precedence operators (like '+' go at the top, while high-precedence operators (like '*') go at the bottom. You would then evaluate the expressions on the tree from the bottom up.

青衫负雪 2024-08-06 06:50:35

C 没有内置的“eval”函数,但有提供它的库。

我强烈建议使用 TinyExpr。 它是免费的开源 C 代码,可实现字符串的数学计算。 TinyExpr只有1个C文件,大约有500行代码。 我认为您不会找到真正完整的更短或更简单的方法(而不仅仅是一个玩具示例)。

这是使用它的完整示例,它应该演示它是多么容易:

#include "tinyexpr.h"
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("%f\n", te_interp("5 * 5", 0)); //Prints 25
    return 0;
}

如果您想自己构建表达式求解器,我建议您查看 TinyExpr 源代码 作为起点。 它非常干净且易于遵循。

C doesn't have an "eval" function built-in, but there are libraries that provide it.

I would highly recommend using TinyExpr. It's free and open-source C code that implements math evaluation from a string. TinyExpr is only 1 C file, and it's about 500 lines of code. I don't think you'll find a shorter or easier way that is actually complete (and not just a toy example).

Here is a complete example of using it, which should demostrate how easy it is:

#include "tinyexpr.h"
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("%f\n", te_interp("5 * 5", 0)); //Prints 25
    return 0;
}

If you want to build an expression solver yourself, I would recommend looking at the TinyExpr source-code as a starting point. It's pretty clean and easy to follow.

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