File: example.cpp

package info (click to toggle)
boolstuff 0.1.14-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,796 kB
  • ctags: 299
  • sloc: sh: 11,017; cpp: 1,649; makefile: 220; ansic: 165; perl: 106
file content (80 lines) | stat: -rw-r--r-- 1,760 bytes parent folder | download | duplicates (2)
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
/*  $Id: example.cpp,v 1.5 2010/06/08 04:36:47 sarrazip Exp $
    Testing program for the library.

    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(int /* argc */, char * /* argv */ [])
{
    BoolExprParser parser;
    string line;

    while (getline(cin, line))
    {
	try
	{
	    BoolExpr<string> *expr = parser.parse(line);
	    cout << "Original expression     : " << flush;
	    assert(expr != NULL);
	    expr->print(cout);
	    cout << "\n";

	    expr = BoolExpr<string>::getDisjunctiveNormalForm(expr);
	    cout << "Disjunctive normal form : ";
	    if (expr != NULL)
		cout << expr;
	    else
		cout << "FALSE";

	    cout << "\n";

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

		V termRoots;
		expr->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 << "\n";
	    delete expr;
	}
	catch (BoolExprParser::Error &err)
	{
	    cerr << "Column " << err.index + 1
		<< ": error #" << err.code << endl;
	}
    }

    return EXIT_SUCCESS;
}