File: c-example.c

package info (click to toggle)
boolstuff 0.1.16-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,912 kB
  • sloc: sh: 11,344; cpp: 1,685; ansic: 261; perl: 196; makefile: 195
file content (100 lines) | stat: -rw-r--r-- 2,562 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*  $Id: c-example.c,v 1.1 2015/01/24 17:32:17 sarrazip Exp $
    C BoolStuff example program.

    Written by Pierre Sarrazin <http://sarrazip.com/>
    This file is in the public domain.
*/

#include <boolstuff/c-api.h>
#include <assert.h>
#include <stdlib.h>


static void print_variable_names(char *names[])
{
    size_t i;

    for (i = 0; names[i] != NULL; ++i)
        printf("%s ", names[i]);
}


int main()
{
    char line[512];

    while (fgets(line, sizeof(line), stdin) != NULL)
    {
        boolexpr_t expr, dnf;
        size_t errorIndex;
        enum bool_error_t errorCode;

        expr = boolstuff_parse(line, &errorIndex, &errorCode);

        if (expr == NULL)
        {
            printf("Error #%d at character #%u\n\n", errorCode, (unsigned) errorIndex);
            continue;
        }

        printf("Original expression     : ");
        boolstuff_print_tree(stdout, expr);
        printf("\n");

        dnf = boolstuff_get_disjunctive_normal_form(expr);

        /*  NOTE: 'expr' is now unusable.
            The original tree has been transformed by the preceding call.
        */
        expr = NULL;

        printf("Disjunctive normal form : ");
        if (dnf != NULL)
            boolstuff_print_tree(stdout, dnf);
        else
            printf("FALSE");

        printf("\n");

        if (dnf != NULL)
        {
            size_t numRoots, i;
            boolexpr_t *treeRoots;  /* array */

            treeRoots = boolstuff_get_dnf_term_roots(dnf, &numRoots);

            for (i = 0; i < numRoots; ++i)
            {
                boolexpr_t term;
                char **positivesArray, **negativesArray;

                term = treeRoots[i];
                boolstuff_get_tree_variables(term, &positivesArray, &negativesArray);

                printf("Term       : ");
                boolstuff_print_tree(stdout, term);
                printf("\n");

                printf("Positives: ");
                print_variable_names(positivesArray);
                printf("\n");
                printf("Negatives: ");
                print_variable_names(negativesArray);
                printf("\n");

                boolstuff_free_variables_sets(positivesArray, negativesArray);
            }

            boolstuff_free_node_array(treeRoots);
        }

        printf("\n");
        boolstuff_destroy_tree(dnf);

        /*  Note that we do not destroy 'expr', because it was implicitly
            destroyed by the call to boolstuff_get_disjunctive_normal_form().
        */
    }

    return EXIT_SUCCESS;
}