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 :)
source share