| 1 |
/* $Id: non-string-nodes.cpp,v 1.2 2010/06/08 04:36:47 sarrazip Exp $ |
| 2 |
Example where the expression tree nodes are not based on strings. |
| 3 |
|
| 4 |
Written by Pierre Sarrazin <http://sarrazip.com/> |
| 5 |
This file is in the public domain. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <boolstuff/BoolExprParser.h> |
| 9 |
#include <assert.h> |
| 10 |
#include <stdlib.h> |
| 11 |
|
| 12 |
using namespace std; |
| 13 |
using namespace boolstuff; |
| 14 |
|
| 15 |
|
| 16 |
class Node |
| 17 |
{ |
| 18 |
public: |
| 19 |
Node(int n = 0) : value(n) {} |
| 20 |
int value; |
| 21 |
}; |
| 22 |
|
| 23 |
|
| 24 |
ostream &operator << (ostream &out, const Node &n) |
| 25 |
{ |
| 26 |
return out << n.value; |
| 27 |
} |
| 28 |
|
| 29 |
bool operator == (Node a, Node b) |
| 30 |
{ |
| 31 |
return a.value == b.value; |
| 32 |
} |
| 33 |
|
| 34 |
bool operator < (Node a, Node b) |
| 35 |
{ |
| 36 |
return a.value < b.value; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
int main(int /* argc */, char * /* argv */ []) |
| 41 |
{ |
| 42 |
{ |
| 43 |
try |
| 44 |
{ |
| 45 |
// Form the expression 11 & (22 | 33): |
| 46 |
BoolExpr<Node> *a = new BoolExpr<Node>(11); |
| 47 |
BoolExpr<Node> *b = new BoolExpr<Node>(22); |
| 48 |
BoolExpr<Node> *c = new BoolExpr<Node>(33); |
| 49 |
BoolExpr<Node> *orOper = |
| 50 |
new BoolExpr<Node>(BoolExpr<Node>::OR, b, c); |
| 51 |
BoolExpr<Node> *expr = |
| 52 |
new BoolExpr<Node>(BoolExpr<Node>::AND, a, orOper); |
| 53 |
|
| 54 |
cout << expr << "\n"; |
| 55 |
|
| 56 |
expr = BoolExpr<Node>::getDisjunctiveNormalForm(expr); |
| 57 |
if (expr != NULL) |
| 58 |
cout << expr; |
| 59 |
else |
| 60 |
cout << "FALSE"; |
| 61 |
|
| 62 |
cout << "\n"; |
| 63 |
|
| 64 |
delete expr; |
| 65 |
} |
| 66 |
catch (BoolExprParser::Error &err) |
| 67 |
{ |
| 68 |
cerr << "Column " << err.index + 1 |
| 69 |
<< ": error #" << err.code << endl; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return EXIT_SUCCESS; |
| 74 |
} |