简体   繁体   English

将CharSequence转换为数学表达式

[英]Conversion of CharSequence to maths expression

In my App i have two TextViews one contains an expression eg. 在我的应用程序中,我有两个TextViews,其中一个包含表达式。 3 + 4 = 3 + 4 =

And the second contains an answer eg. 第二个包含答案。 7 7

How would i go about turning this into a valid maths expression so the app could calculate it and return the answer as an int? 我将如何将其转换为有效的数学表达式,以便应用程序可以对其进行计算并将答案作为整数返回?

Depending on the complexity of the expressions you expect in your TextViews, you might need to construct a parser/interpreter for them. 根据您在TextView中期望的表达式的复杂程度,您可能需要为其构造一个解析器/解释器。 If that's the case, I heartily recommend ANTLR . 如果是这样,我衷心推荐ANTLR For more information about using ANTLR on Android, see this question . 有关在Android上使用ANTLR的更多信息,请参阅此问题

Another parser generator that I know of is JavaCC , but ANTLR is a lot more flexible and powerful. 我知道的另一个解析器生成器是JavaCC ,但是ANTLR更加灵活和强大。

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

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