| 1 |
/* $Id: c-example.c,v 1.1 2015/01/24 17:32:17 sarrazip Exp $ |
| 2 |
C BoolStuff example program. |
| 3 |
|
| 4 |
Written by Pierre Sarrazin <http://sarrazip.com/> |
| 5 |
This file is in the public domain. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <boolstuff/c-api.h> |
| 9 |
#include <assert.h> |
| 10 |
#include <stdlib.h> |
| 11 |
|
| 12 |
|
| 13 |
static void print_variable_names(char *names[]) |
| 14 |
{ |
| 15 |
size_t i; |
| 16 |
|
| 17 |
for (i = 0; names[i] != NULL; ++i) |
| 18 |
printf("%s ", names[i]); |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
int main() |
| 23 |
{ |
| 24 |
char line[512]; |
| 25 |
|
| 26 |
while (fgets(line, sizeof(line), stdin) != NULL) |
| 27 |
{ |
| 28 |
boolexpr_t expr, dnf; |
| 29 |
size_t errorIndex; |
| 30 |
enum bool_error_t errorCode; |
| 31 |
|
| 32 |
expr = boolstuff_parse(line, &errorIndex, &errorCode); |
| 33 |
|
| 34 |
if (expr == NULL) |
| 35 |
{ |
| 36 |
printf("Error #%d at character #%u\n\n", errorCode, (unsigned) errorIndex); |
| 37 |
continue; |
| 38 |
} |
| 39 |
|
| 40 |
printf("Original expression : "); |
| 41 |
boolstuff_print_tree(stdout, expr); |
| 42 |
printf("\n"); |
| 43 |
|
| 44 |
dnf = boolstuff_get_disjunctive_normal_form(expr); |
| 45 |
|
| 46 |
/* NOTE: 'expr' is now unusable. |
| 47 |
The original tree has been transformed by the preceding call. |
| 48 |
*/ |
| 49 |
expr = NULL; |
| 50 |
|
| 51 |
printf("Disjunctive normal form : "); |
| 52 |
if (dnf != NULL) |
| 53 |
boolstuff_print_tree(stdout, dnf); |
| 54 |
else |
| 55 |
printf("FALSE"); |
| 56 |
|
| 57 |
printf("\n"); |
| 58 |
|
| 59 |
if (dnf != NULL) |
| 60 |
{ |
| 61 |
size_t numRoots, i; |
| 62 |
boolexpr_t *treeRoots; /* array */ |
| 63 |
|
| 64 |
treeRoots = boolstuff_get_dnf_term_roots(dnf, &numRoots); |
| 65 |
|
| 66 |
for (i = 0; i < numRoots; ++i) |
| 67 |
{ |
| 68 |
boolexpr_t term; |
| 69 |
char **positivesArray, **negativesArray; |
| 70 |
|
| 71 |
term = treeRoots[i]; |
| 72 |
boolstuff_get_tree_variables(term, &positivesArray, &negativesArray); |
| 73 |
|
| 74 |
printf("Term : "); |
| 75 |
boolstuff_print_tree(stdout, term); |
| 76 |
printf("\n"); |
| 77 |
|
| 78 |
printf("Positives: "); |
| 79 |
print_variable_names(positivesArray); |
| 80 |
printf("\n"); |
| 81 |
printf("Negatives: "); |
| 82 |
print_variable_names(negativesArray); |
| 83 |
printf("\n"); |
| 84 |
|
| 85 |
boolstuff_free_variables_sets(positivesArray, negativesArray); |
| 86 |
} |
| 87 |
|
| 88 |
boolstuff_free_node_array(treeRoots); |
| 89 |
} |
| 90 |
|
| 91 |
printf("\n"); |
| 92 |
boolstuff_destroy_tree(dnf); |
| 93 |
|
| 94 |
/* Note that we do not destroy 'expr', because it was implicitly |
| 95 |
destroyed by the call to boolstuff_get_disjunctive_normal_form(). |
| 96 |
*/ |
| 97 |
} |
| 98 |
|
| 99 |
return EXIT_SUCCESS; |
| 100 |
} |