File: example.cpp

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 (90 lines) | stat: -rw-r--r-- 2,488 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
/*  $Id: example.cpp,v 1.6 2015/01/21 03:00:42 sarrazip Exp $
    C++ BoolStuff example program.

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

#include <boolstuff/BoolExprParser.h>
#include <assert.h>
#include <stdlib.h>

using namespace std;
using namespace boolstuff;


static
ostream &
operator << (ostream &out, const set<string> &s)
{
    for (set<string>::const_iterator it = s.begin(); it != s.end(); it++)
        out << *it << " ";
    return out;
}


int main()
{
    BoolExprParser parser;
    string line;

    while (getline(cin, line))
    {
        try
        {
            BoolExpr<string> *expr = parser.parse(line);
            
            assert(expr != NULL);  // any error must have thrown an exception

            cout << "Original expression     : " << flush;
            expr->print(cout);
            cout << "\n";

            BoolExpr<string> *dnf = BoolExpr<string>::getDisjunctiveNormalForm(expr);

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

            cout << "Disjunctive normal form : ";
            if (dnf != NULL)
                cout << dnf;
            else
                cout << "FALSE";

            cout << "\n";

            if (dnf != NULL)
            {
                typedef vector<const BoolExpr<string> *> V;
                typedef V::const_iterator IT;

                V termRoots;
                dnf->getDNFTermRoots(inserter(termRoots, termRoots.end()));
                for (IT it = termRoots.begin(); it != termRoots.end(); it++)
                {
                    const BoolExpr<string> *term = *it;
                    set<string> positives, negatives;
                    term->getTreeVariables(positives, negatives);

                    cout << "Term       : " << term << "\n";
                    cout << "  Positives: " << positives << "\n";
                    cout << "  Negatives: " << negatives << "\n";
                }
            }

            cout << endl;
            delete dnf;

            // Note that we do not destroy 'expr', because it was implicitly
            // destroyed by the call to boolstuff_get_disjunctive_normal_form().
        }
        catch (BoolExprParser::Error &err)
        {
            cout << "Error #" << err.code << " at character #" << err.index << endl;
        }
    }

    return EXIT_SUCCESS;
}