C++ 中的表达式求值
我正在编写一些类似 excel 的 C++ 控制台应用程序作为家庭作业。 我的应用程序应该能够接受其单元格的公式,例如它应该评估类似这样的内容:
Sum(tablename\fieldname[recordnumber], fieldname[recordnumber], ...)
tablename\fieldname[recordnumber] points to a cell in another table,
fieldname[recordnumber] points to a cell in current table
或
Sin(fieldname[recordnumber])
或
anotherfieldname[recordnumber]
或
"10" // (simply a number)
类似的内容。 函数有 Sum、Ave、Sin、Cos、Tan、Cot、Mul、Div、Pow、Log (10)、Ln、Mod
我知道这很可悲,但这是我的作业:'(
那么有谁知道评估某些东西的技巧吗像这样?
I'm writing some excel-like C++ console app for homework.
My app should be able to accept formulas for it's cells, for example it should evaluate something like this:
Sum(tablename\fieldname[recordnumber], fieldname[recordnumber], ...)
tablename\fieldname[recordnumber] points to a cell in another table,
fieldname[recordnumber] points to a cell in current table
or
Sin(fieldname[recordnumber])
or
anotherfieldname[recordnumber]
or
"10" // (simply a number)
something like that.
functions are Sum, Ave, Sin, Cos, Tan, Cot, Mul, Div, Pow, Log (10), Ln, Mod
It's pathetic, I know, but it's my homework :'(
So does anyone know a trick to evaluate something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,顺便说一句,这是一个很好的家庭作业问题。
这实际上取决于您想要的重量。 您可以创建一个完整的表达式解析器(这很有趣,但也很耗时)。
为了做到这一点,您需要描述完整的语法并编写一个前端(看看 lex 和 yacc 或 flexx 和 bison。
但正如我看到您的问题,您可以将自己限制为三个子情况:
我认为一点面向对象设计可以帮助你,
我不确定你是否必须处理实时刷新和循环依赖检查,否则它们也可能很棘手。
Ok, nice homework question by the way.
It really depends on how heavy you want this to be. You can create a full expression parser (which is fun but also time consuming).
In order to do that, you need to describe the full grammar and write a frontend (have a look at lex and yacc or flexx and bison.
But as I see your question you can limit yourself to three subcases:
I think a little OO design can helps you out here.
I'm not sure if you have to deal with real time refresh and circular dependency checks. Else they can be tricky too.
对于解析,我会看看递归下降解析。 然后有一个表将所有可能的函数名称映射到函数指针:
For the parsing, I'd look at Recursive descent parsing. Then have a table that maps all possible function names to function pointers:
你应该写一个解析器。 解析器应采用表达式 ie 的每一行,并应识别命令并构造解析树。 这是第一阶段。 在第二阶段,您可以通过用数据替换命令的每个元素来评估树。
You should write a parser. Parser should take the expression i.e., each line and should identify the command and construct the parse tree. This is the first phase. In the second phase you can evaluate the tree by substituting the data for each elements of the command.
之前的回复者已经击中要害:您需要解析单元格内容,并解释它们。
StackOverflow 已经提供了大量有关构建编译器和解释器的问题,您可以在其中找到指向资源的指针。 其中一些是:
等等。
旁白:我从来没有精力将它们全部联系在一起,甚至没有精力尝试建立一个全面的列表。
Previous responders have hit it on the head: you need to parse the cell contents, and interpret them.
StackOverflow already has a whole slew of questions on building compilers and interperters where you can find pointers to resources. Some of them are:
and so on.
Aside: I never have the energy to link them all together, or even try to build a comprehensive list.
我猜你不能使用 yacc/lex (或类似的),所以你必须“手动”解析:
迭代字符串并将其分成几个部分。 什么部分取决于你的语法(syntax)。 这样您就可以找到函数名称和参数。 这个的难度取决于语法的复杂性。
也许您应该阅读一些有关词法分析的内容。
I guess you cannot use yacc/lex (or the like) so you have to parse "manually":
Iterate over the string and divide it into its parts. What a part is depends on you grammar (syntax). That way you can find the function names and the parameters. The difficulty of this depends on the complexity of your syntax.
Maybe you should read a bit about lexical analysis.