File: non-string-nodes.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 (74 lines) | stat: -rw-r--r-- 1,442 bytes parent folder | download | duplicates (6)
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
/*  $Id: non-string-nodes.cpp,v 1.2 2010/06/08 04:36:47 sarrazip Exp $
    Example where the expression tree nodes are not based on strings.

    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;


class Node
{
public:
    Node(int n = 0) : value(n) {}
    int value;
};


ostream &operator << (ostream &out, const Node &n)
{
    return out << n.value;
}

bool operator == (Node a, Node b)
{
    return a.value == b.value;
}

bool operator < (Node a, Node b)
{
    return a.value < b.value;
}


int main(int /* argc */, char * /* argv */ [])
{
    {
	try
	{
	    // Form the expression 11 & (22 | 33):
	    BoolExpr<Node> *a = new BoolExpr<Node>(11);
	    BoolExpr<Node> *b = new BoolExpr<Node>(22);
	    BoolExpr<Node> *c = new BoolExpr<Node>(33);
	    BoolExpr<Node> *orOper =
			    new BoolExpr<Node>(BoolExpr<Node>::OR, b, c);
	    BoolExpr<Node> *expr =
			    new BoolExpr<Node>(BoolExpr<Node>::AND, a, orOper);

	    cout << expr << "\n";

	    expr = BoolExpr<Node>::getDisjunctiveNormalForm(expr);
	    if (expr != NULL)
		cout << expr;
	    else
		cout << "FALSE";

	    cout << "\n";

	    delete expr;
	}
	catch (BoolExprParser::Error &err)
	{
	    cerr << "Column " << err.index + 1
		<< ": error #" << err.code << endl;
	}
    }

    return EXIT_SUCCESS;
}