blob: 86c68c8bb8f585ec8e71e07e7099fdf59cd2f1cb (
plain) (
blame)
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
|
#include "ExpressionCalculator.h"
void ExpressionCalculator::copyFrom(const ExpressionCalculator& other)
{
expr = other.expr->clone();
}
void ExpressionCalculator::free()
{
delete expr;
}
bool isOperator(char ch)
{
return ch == AND || ch == OR || ch == IFF || ch == IMPL || ch == XOR || ch == NEG;
}
ExpressionCalculator::BooleanExpr* parse(const MyString& str)
{
if (str.getSize() == 1)
{
return new ExpressionCalculator::Variable(str[0]);
}
MyString strWithoutBrackets = str.subString(1, str.getSize() - 2); //We remove the brackets.
int countBrackets = 0;
for (size_t i = 0; i < strWithoutBrackets.getSize(); i++) //we start searching for the operation.
{ // then we split to two expressions
if (strWithoutBrackets[i] == '(')
countBrackets++;
else if (strWithoutBrackets[i] == ')')
countBrackets--;
else if (isOperator(strWithoutBrackets[i]) && countBrackets == 0)
{
if (strWithoutBrackets[i] == NEG)
return new ExpressionCalculator::UnaryOperation('!', parse(strWithoutBrackets.subString(i + 1, strWithoutBrackets.getSize() - 1)));
else
return new ExpressionCalculator::BinaryOperation(parse(strWithoutBrackets.subString(0, i - 1)), strWithoutBrackets[i], parse(strWithoutBrackets.subString(i+1, strWithoutBrackets.getSize() - 1)));
}
}
}
ExpressionCalculator::ExpressionCalculator(const MyString& str)
{
expr = parse(str);
}
ExpressionCalculator::ExpressionCalculator(const ExpressionCalculator& other)
{
copyFrom(other);
}
ExpressionCalculator& ExpressionCalculator::operator=(const ExpressionCalculator& other)
{
if (this != &other)
{
free();
copyFrom(other);
}
return *this;
}
ExpressionCalculator::~ExpressionCalculator()
{
free();
}
bool ExpressionCalculator::isTautology() const
{
return checkAll(expr, true);
}
bool ExpressionCalculator::isContradiction() const
{
return checkAll(expr, false);
}
|