简体   繁体   English

前缀Postfix Infix Java表达式求值程序

[英]Prefix Postfix Infix Java expression evaluator

i have been working on an expression evaluator in java and out of frustration I have also come here to ask about it. 我一直在研究java中的表达式评估器,出于挫折感,我也来这里询问它。 I have rewritten it at least 15 times by now and each to no avail. 我现在已经重写了至少15次,但都无济于事。

Basically I need to take a String in either prefix, infix or postfix and evaluate it to an integer. 基本上我需要在前缀,中缀或后缀中使用String并将其计算为整数。 The expression can use any arithmetic operators included in java. 表达式可以使用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. 我遇到的问题: - 当我尝试解析表达式树或将它们全部解析为postfix时,我遇到了单项式运算符的问题。 - 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 ABCDEF 对于任意数字ABCDEF

Evaluate in prefix postfix and infix: 在前缀postfix和中缀中进行评估:

A-- + ++B - --C * D++ / E % F A-- + ++ B - - C * D ++ / E%F

For any arbitrary bits ABCDEFG and number n 对于任意位ABCDEFG和数字n

Evaluate in prefix, postfix, and infix: A & B | 评估前缀,后缀和中缀:A&B | C ^ ~D C ^ ~D

n >> A n >> A

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 谢谢你的帮助:D

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. 对于这种情况,您可以手动编写一个,而不是试图弄清楚如何使用解析器生成器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM