理解算术表达式编译器 [英] Understanding Arithmetic Expression Compilers

查看:105
本文介绍了理解算术表达式编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好! (对不起,如果我有一些语法错误,英语不是我的母语,但我会尽我所能!)

我正在尝试编写一个程序来编译包含算术表达式的字符串并解决/评估它。我不是在寻找代码或库,我只想获得更多的知识,这样我才能理解我正在尝试做的事情。



我已经阅读了关于中缀,前缀和后缀的内容,但我不知道这将如何编程。我现在怎么样才知道我有一个字符串,例如7 + 4 * 5 - 10/2,在每次加法或减法操作中,我将其分解为更小的部分。在那些我存储的部分,如果它是正面的还是负面的,我还存储数字或方程式,如果它包含更强的运算符(乘法,除法和模数)。所以前面的字符串将被转换为这个数组:

 [+,7] [+,4 * 5] [ - ,10/2] 



我这样做是因为我可以在不遇到麻烦的情况下加扰零件序列:

 [+,7] [+,4 * 5] [ - ,10/2] == [+,4 * 5] [+,7] [ - ,10/2] 



现在我计算每个零件方程式,我得到:

 [+,7] [+,20] [ - ,5] 



现在我可以得到所有部分的总和。

 [+,7]加[+,20] = 27; [+,27]添加[ - ,5] = 22; 



如果我在表达式中有一个括号,我可以在它们内部的字符串上使用相同的函数。然后粘贴括号的结果。



我可以粘贴代码,但我认为没有意义,因为我刚刚解释了整个程序。



- 现在我的问题是:我是否会想到这个错误,我是否忘记了可以简化并提高效率的东西?



我非常期待你的回答!

解决方案

你的问题没有解决方案/答案。

引用:

我可以粘贴代码,但我认为没有意义,因为我刚刚解释了整个程序。

这就是你所在的位置错误。你给了我们一个关于表达式编译的模糊的一般性说法,几乎可以用于任何程序或技术。

Quote:

- 现在我的问题是:我是否会想到这个错误,我忘记了可以简化并提高效率的东西吗?

在效率和简单性方面,唯一感兴趣的是你的确切方法或程序(你没有提供)。

Quote:

我读过关于中缀,前缀和后缀,但我不知道这将如何编程。

由于理解中缀,前缀和后缀是编写表达式编译器的先决条件,甚至怀疑你可以做什么。


我按照这些教程想出了怎么做!

HTTP s://www.youtube.com/watch?v = vXPL6UavUeA& list = LLAbEvqKZEqYBAmzzgVbKojQ& index = 2 [ ^ ]

https://www.youtube.com/watch?v=MeRb_1bddWg&list=LLAbEvqKZEqYBAmzzgVbKojQ&index=3 [ ^ ]

感谢您的所有响应。 :)

Hello there! (Sorry if I have some grammatical errors, english is not my native language, but I try my best!)
I'm currently trying to write a program that compiles a string containing an arithmetic expression and solves / evaluates it. I'm not looking for code or libraries, I just want to get some more knowledge, so that I can understand the things that I'm trying to do.

I've read about infix, prefix and postfix, but I don't know how this would programmatically. How I'm going about it now is that i have a string e.g. "7 + 4*5 - 10/2", at every add or subtract operation I break it up into smaller parts. In those parts I store if it either positive or negative, I also store the number, or equation if it contains stronger operators (multiplication, division and modulo). So the previous string would be converted into this array:

[+, 7] [+, 4*5] [-, 10/2]


I do this since I can scramble the sequence of parts without getting into trouble later:

[+, 7] [+, 4*5] [-, 10/2] == [+, 4*5] [+, 7] [-, 10/2]


Now I calculate each of the parts equation, so that I get:

[+, 7] [+, 20] [-, 5]


Now I can take the sum of all the parts.

[+, 7] add [+, 20] = 27; [+, 27] add [-, 5] = 22;


If I had a parenthese in the expression, I could just use the same function on the string inside them. Then paste the result of the parentheses instead.

I could paste the code, but I see no point since I've just explained basically the whole program.

- Now here is my question: Am I going about this wrong, have I forgotten something that could simplify and increase efficiency?

I really look forward for your answers!

解决方案

There is no solution/answer to your question.

Quote:

I could paste the code, but I see no point since I've just explained basically the whole program.

This is where you are wrong. You gave us a vague general speak about expression compilation that can apply to almost any program or technique used to do it.

Quote:

- Now here is my question: Am I going about this wrong, have I forgotten something that could simplify and increase efficiency?

When it comes to efficiency and simplicity, the only thing of interest is your exact method or program (that you didn't provide).

Quote:

I've read about infix, prefix and postfix, but I don't know how this would programmatically.

Since understanding infix, prefix and postfix is a prerequisite to programming an expression compiler, there is even doubt about what you can have really done.


I figured out how to do it by following these tutorials!
https://www.youtube.com/watch?v=vXPL6UavUeA&list=LLAbEvqKZEqYBAmzzgVbKojQ&index=2[^]
https://www.youtube.com/watch?v=MeRb_1bddWg&list=LLAbEvqKZEqYBAmzzgVbKojQ&index=3[^]
Thanks for all your responds. :)


这篇关于理解算术表达式编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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