| 1 | |
|---|
| 2 | #include <iostream> |
|---|
| 3 | #include "EbmVar.h" |
|---|
| 4 | #include "EbmExpr.h" |
|---|
| 5 | using namespace std; |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | void printTruthTable ( Ebm* e ) |
|---|
| 9 | { |
|---|
| 10 | if ( e == NULL ) { |
|---|
| 11 | cerr << "[ERROR] Argument Ebm* musn't be NULL." << endl; |
|---|
| 12 | return; |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | cout << " Truth Table of:\n " << e << endl; |
|---|
| 16 | cout << " Support is: "; e->support(cout); cout << endl; |
|---|
| 17 | |
|---|
| 18 | const set<EbmVar*>& support = e->support(); |
|---|
| 19 | |
|---|
| 20 | cout << " "; |
|---|
| 21 | for ( set<EbmVar*>::const_iterator ivar=support.begin() ; ivar != support.end() ; ++ivar ) |
|---|
| 22 | cout << (*ivar)->getName(); |
|---|
| 23 | cout << " | e" << endl; |
|---|
| 24 | |
|---|
| 25 | unsigned int tableSize = 2 << (support.size()-1); |
|---|
| 26 | for ( unsigned int entries=0 ; entries < tableSize ; ++entries ) { |
|---|
| 27 | cout << " "; |
|---|
| 28 | size_t i=support.size()-1; |
|---|
| 29 | for ( set<EbmVar*>::const_iterator ivar=support.begin() ; ivar != support.end() ; ++ivar, --i ) { |
|---|
| 30 | unsigned int entry = (entries >> i) % 2; |
|---|
| 31 | (*ivar)->setValue( entry ); |
|---|
| 32 | cout << entry; |
|---|
| 33 | } |
|---|
| 34 | cout << " | " << e->eval() << endl; |
|---|
| 35 | } |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | int main ( int argc, char* argv[] ) |
|---|
| 40 | { |
|---|
| 41 | Ebm* a = EbmVar::create ( "a" ); |
|---|
| 42 | Ebm* b = EbmVar::create ( "b" ); |
|---|
| 43 | Ebm* c = EbmVar::create ( "c" ); |
|---|
| 44 | |
|---|
| 45 | Ebm* na = EbmExpr::Not ( a ); |
|---|
| 46 | Ebm* nb = EbmExpr::Not ( b ); |
|---|
| 47 | Ebm* nc = EbmExpr::Not ( c ); |
|---|
| 48 | |
|---|
| 49 | Ebm* e0 = EbmExpr::And ( a, b, c ); |
|---|
| 50 | Ebm* e1 = EbmExpr::And ( a, b, nc ); |
|---|
| 51 | Ebm* e2 = EbmExpr::And ( a, nb, c ); |
|---|
| 52 | Ebm* e3 = EbmExpr::And (na, b, c ); |
|---|
| 53 | Ebm* e4 = EbmExpr::Or ( e0, e1, e2, e3 ); |
|---|
| 54 | |
|---|
| 55 | printTruthTable(e4); |
|---|
| 56 | |
|---|
| 57 | Ebm* e5 = Ebm::parse("(a.b)+(a.c)+(b.c)"); |
|---|
| 58 | printTruthTable(e5); |
|---|
| 59 | |
|---|
| 60 | #if 0 |
|---|
| 61 | cout << "e10 = " << Ebm::parse ( "(a . b . c) + (a . b . c') + (a . b' . c) + (a' .b . c)" ) << endl; |
|---|
| 62 | cout << "e11 = " << Ebm::parse ( "(x1.x2) + (x1.x3)" ) << endl; |
|---|
| 63 | cout << "e12 = " << Ebm::parse ( "((var1 + var2).var3 ) + var4" ) << endl; |
|---|
| 64 | cout << "e13 = " << Ebm::parse ( "(a . b)'" ) << endl; |
|---|
| 65 | cout << "e13 = " << Ebm::parse ( "a'" ) << endl; |
|---|
| 66 | #endif |
|---|
| 67 | |
|---|
| 68 | // This is it! |
|---|
| 69 | return 0; |
|---|
| 70 | } |
|---|