Prefix Postfix Infix Java 表达式计算器
我一直在用java开发一个表达式评估器,出于沮丧,我也来这里询问它。到目前为止我已经重写了至少15次,但每次都无济于事。
基本上,我需要在前缀、中缀或后缀中获取一个字符串,并将其计算为整数。该表达式可以使用 java 中包含的任何算术运算符。
我遇到的问题: - 当我尝试解析表达式树或将它们全部解析为后缀时,我遇到了单项式运算符的问题。 - 我无法让它将多位数字识别为一个数字,随后它会破坏整棵树或它会切换数字。 - 我无法让程序找出正确放置括号的位置,也无法让它使用堆栈来识别括号的开头和结尾。
所以基本上程序需要这样做:
对于任何任意数字 ABCDEF
在前缀后缀和中缀中求值:
A-- + ++B - --C * D++ / E % F
对于任意任意位 ABCDEFG 和数字 n
在前缀中求值,后缀和中缀: A&乙| C^~Dn
>> A
n << A
我的教科书和我订购的书籍都没有多大帮助,我真的不明白如何做到这一点,请帮助,即使只是解释如何做到这一点对我来说就足够了。感谢您的帮助:D
i have been working on an expression evaluator in java and out of frustration I have also come here to ask about it. I have rewritten it at least 15 times by now and each to no avail.
Basically I need to take a String in either prefix, infix or postfix and evaluate it to an integer. The expression can use any arithmetic operators included in java.
The problems im having:
- When i try to parse to an expression tree or parse them all to postfix i have problems with the monomial operators.
- I can't get it to identify a multi digit number as one number and subsequently it ruins the whole tree or it switches around the digits.
- I can't get the program to figure out where to put down the parentheses correctly and i can't get it to identify the beggining and end of the parentheses using a stack.
So basically the program needs to do this:
For any arbitrary numbers A B C D E F
Evaluate in prefix postfix and infix:
A-- + ++B - --C * D++ / E % F
For any arbitrary bits A B C D E F G and number n
Evaluate in prefix, postfix, and infix:
A & B | C ^ ~D
n >> A
n << A
My textbook isn't much help nor are the books i have ordered, I really can't understand how to do this, please help, even just an explanation of how to do it would be good enough for me. Thanks for any help :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您已成功标记所有标识符、运算符和文字,下一步将是阅读一些有关递归下降解析器的内容。对于这种情况,您可能可以手动编写一个解析器生成器,而不是试图弄清楚如何使用解析器生成器。
Assuming that you have successfully tokenized all your identifiers, operators, and literals, the next step would be to read a bit about recursive descent parsers. You can probably write one by hand for this case rather than trying to figure out how to use a parser generator.