How can I expand expressions like (A OR B) AND C to A AND C OR A AND B?

只愿长相守 提交于 2020-01-14 14:28:26

问题


I have tried Javaluator which helped me in evaluating the expressions like (A OR B) AND C . But now I only want to expand the expression (A OR B) AND C to A AND C OR A AND B can any body tell me how can I do this in Java any API or any other help?


回答1:


If you don't need to do it yourself, you may use Wolfram|Alpha API, it has a plenty of boolean-algebra-related features, like converting to various normal forms and so on. If that's a homework and you should invent your own wheel, you may use some parsing tools (or invent your wheel, again) to tokenize the string and then apply the set ob boolean algebra rules: http://mathworld.wolfram.com/BooleanAlgebra.html

I think I should make this more concrete - you can't solve this problem in general case without writing that set of rules in your code (hardcoding it). The most concise way is to use something like ANTLR to produce a boolean-rules-constrained language and then just feed your inputs to it.




回答2:


This is called The De Morgan rules. I think the best option is to use a Karnaugh Map to do this. This truth table generator will help you on your way.




回答3:


(A OR B) AND C to A AND C OR A AND B

This is false.... (A or B) and C is equals to (a and c) or (B and C)........




回答4:


If number of variables is not very big you may test expression for every set of variables. And if it's true on set A = true, B = false, C = true add to result OR (A and !B and C)

This form is called "Full disjunctive normal form"



来源:https://stackoverflow.com/questions/18187491/how-can-i-expand-expressions-like-a-or-b-and-c-to-a-and-c-or-a-and-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!