-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooleanExpression.cpp
More file actions
109 lines (86 loc) · 2.56 KB
/
BooleanExpression.cpp
File metadata and controls
109 lines (86 loc) · 2.56 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <algorithm>
#include "BooleanExpression.h"
BooleanExpression::BooleanExpression(unsigned int ndisjuncts) :
m_ndisjuncts(ndisjuncts)
{
m_clauses = new BooleanTerm *[m_ndisjuncts];
for (int i = 0; i < m_ndisjuncts; i++)
m_clauses[i] = NULL;
}
BooleanExpression::~BooleanExpression()
{
for (int i = 0; i < m_ndisjuncts; i++)
delete m_clauses[i];
delete [] m_clauses;
}
// TODO: can this be cleaned up??
void BooleanExpression::term(unsigned int disjunct, BooleanTerm * term)
{
m_clauses[disjunct] = term;
for (int i = 0; i < term->nfactors(); i++)
{
if (dynamic_cast<const BooleanFactor<int> *>(term->m_factors[i]))
{
const BooleanFactor<int> * f = (const BooleanFactor<int> *)term->m_factors[i];
if (f->m_lOperand->type() == VARIABLE)
m_variables.push_back(dynamic_cast<IVariableOperand *>(f->m_lOperand));
if (f->m_rOperand->type() == VARIABLE)
m_variables.push_back(dynamic_cast<IVariableOperand *>(f->m_rOperand));
}
else if (dynamic_cast<const BooleanFactor<char> *>(term->m_factors[i]))
{
const BooleanFactor<char> * f = (const BooleanFactor<char> *)term->m_factors[i];
if (f->m_lOperand->type() == VARIABLE)
m_variables.push_back(dynamic_cast<IVariableOperand *>(f->m_lOperand));
if (f->m_rOperand->type() == VARIABLE)
m_variables.push_back(dynamic_cast<IVariableOperand *>(f->m_rOperand));
}
else if (dynamic_cast<const BooleanFactor<const char *> *>(term->m_factors[i]))
{
const BooleanFactor<const char *> * f = (const BooleanFactor<const char *> *)term->m_factors[i];
if (f->m_lOperand->type() == VARIABLE)
m_variables.push_back(dynamic_cast<IVariableOperand *>(f->m_lOperand));
if (f->m_rOperand->type() == VARIABLE)
m_variables.push_back(dynamic_cast<IVariableOperand *>(f->m_rOperand));
}
}
}
std::vector<IVariableOperand *> & BooleanExpression::variables()
{
return m_variables;
}
bool BooleanExpression::evaluate()
{
bool flag = false;
for (int i = 0; i < m_ndisjuncts && !flag; i++)
{
if (m_clauses[i])
flag = m_clauses[i]->value();
}
return flag;
}
BooleanTerm::~BooleanTerm()
{
std::for_each(m_factors.begin(), m_factors.end(), free);
}
void BooleanTerm::factor(IBooleanFactor * f)
{
m_factors.push_back(f);
}
size_t BooleanTerm::nfactors() const
{
return m_factors.size();
}
bool BooleanTerm::value()
{
bool flag = true;
int n = m_factors.size();
for (int i = 0; i < n && flag; i++)
{
flag &= m_factors[i]->value();
}
return flag;
}
IBooleanFactor::~IBooleanFactor()
{
}