Solving Boolean Expressions Using Straight Chain

Can someone help me with solving boolean expressions using redirection. A good tutorial will also help me.

Example: A.(A + B) = A

A.(A + B) => AA + AB [Application of the distribution law]

AA + AB => A + AB [Application of the law of idempotency]

A + AB => A.(1 + B)

A.(1 + B) => A.(1) => A

I have made great efforts, but still can not do it. The procedure will require parsing a boolean expression, and then checking the recursive rule. I was thinking of creating a binary expression tree, and then doing a rule check. Is my approach right? If not, offer me an alternative.

+4
source share
2 answers

One approach to solving the problem may be to use the brute force method. By this I mean: try all possible combinations of the values A and B (or, be that as it may, how many values ​​you have) and create a table of the truth of the results.

The following example illustrates this (although it is more in the style of C rather than C++ ).

 #include <iostream> #include <algorithm> #include <cmath> #include <cassert> const unsigned g_unValues = 2; bool expression(int values[]) { return !!(values[0] * (values[0] + values[1])); } void truth_table(bool (*func)(int[]), unsigned nvalues); int main(int argc, char** argv) { truth_table(expression, g_unValues); return 0; } void truth_table(bool (*func)(int[]), unsigned nvalues) { assert(pow(2, nvalues) <= sizeof(unsigned)); int values[nvalues]; unsigned individuals[nvalues]; unsigned result = 0; std::fill_n(individuals, nvalues, 0); // Display truth table header for (unsigned j = 0; j < nvalues; j++) std::cout << char('A'+j) << ' '; std::cout << "| Result" << std::endl; for (unsigned i = 1; i <= pow(2, nvalues); i++) { for (unsigned j = 0; j < nvalues; j++) { values[j] = i & 0x1<<j; if (values[j]) individuals[j] |= 0x1<<i; } bool eval = func(values); if (eval) result |= 0x1<<i; // Display truth table entry for (unsigned j = 0; j < nvalues; j++) std::cout << !!values[j] << ' '; std::cout << "| " << eval << std::endl; } for (unsigned j = 0; j < nvalues; j++) { if (result != individuals[j]) continue; std::cout << "Expression equivalence: " << char('A'+j) << std::endl; break; } } 

This code alone is not very useful, but it can give you some ideas if you choose brute force. You can adapt the code to create expression from a string provided by the user. For expressions that are not simplified to a single output, you can replace the code that compares the input columns of the truth table with the result column with the minimum row generation method (simplification of the initial input logical expression).

Hope this is somehow useful, good luck :)

0
source

I think the C ++ Prop library ( http://www.cs.nyu.edu/leunga/prop.html ) may be useful for this: it provides a symbolic representation of the term and rewriting support that could be used to implement your system

0
source

Source: https://habr.com/ru/post/1499899/


All Articles