Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 15 / ExpessionCalculator / ExpressionCalculator.h
blob: eb21a99f229e933d14ad992b0074f4b6f0ec2796 (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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <exception>
#include "../MyString/MyString.h"

const char AND = '^';
const char OR = 'v';
const char IMPL = '>'; //=>
const char IFF = '='; // <=>
const char XOR = '+';
const char NEG = '!';

class ExpressionCalculator
{

	class BooleanExpr
	{
	public:
		bool vars[26] = { false };
		size_t countVars = 0;


		struct BooleanInterpretation
		{
			bool values[26] = { false };
			BooleanInterpretation()
			{	}
			void setValue(char ch, bool value)
			{
				if (ch < 'A' || ch > 'Z')
					throw "error";

				values[ch - 'A'] = value;
			}
			bool getValue(char ch) const
			{
				if (ch < 'A' || ch > 'Z')
					throw "error";
				return 	values[ch - 'A'];
			}
		};

		virtual bool eval(const BooleanInterpretation& interpret) const = 0;

		virtual BooleanExpr* clone() const = 0;
		virtual ~BooleanExpr() = default;

	};
	class Variable : public BooleanExpr
	{
		char ch;
	public:
		Variable(char ch) : ch(ch)
		{
			vars[ch - 'A'] = true;
			countVars = 1;
		} ///only CAPITALS

		bool eval(const BooleanInterpretation& interpret) const override
		{
			return interpret.getValue(ch);
		}
		BooleanExpr* clone() const override
		{
			return new Variable(*this);
		}

	};

	class BinaryOperation : public BooleanExpr
	{
		BooleanExpr* left;
		char op;
		BooleanExpr* right;
	public:
		BinaryOperation(BooleanExpr* left, char op, BooleanExpr* right) : left(left), op(op), right(right)
		{
			for (int i = 0; i < 26; i++)
			{
				vars[i] = left->vars[i] || right->vars[i];
				if (vars[i])
					countVars++;

			}
		}

		bool eval(const BooleanInterpretation& interpret) const override
		{

			switch (op)
			{
			case 'v': return  left->eval(interpret) || right->eval(interpret);
			case '^': return left->eval(interpret) && right->eval(interpret);
			default: return false;
				break;
			}
		}

		BooleanExpr* clone() const override
		{
			return new BinaryOperation(left->clone(), op, right->clone());
		}



		~BinaryOperation()
		{
			delete left;
			delete right;
		}
	};

	class UnaryOperation : public BooleanExpr
	{
		char ch; //!
		BooleanExpr* expr;

	public:
		UnaryOperation(char ch, BooleanExpr* expr) : ch(ch), expr(expr)
		{
			for (int i = 0; i < 26; i++)
			{
				vars[i] = expr->vars[i];
				if (vars[i])
					countVars++;
			}
		}

		BooleanExpr* clone() const override
		{
			return new UnaryOperation(ch, expr->clone());
		}

		bool eval(const BooleanInterpretation& interpret) const
		{
			if (ch == '!')
			{
				return !expr->eval(interpret);
			}
			else
			{
				// 
				return false;
			}
		}
		~UnaryOperation()
		{
			delete expr;
		}
	};

	BooleanExpr::BooleanInterpretation convert(size_t number, bool vars[26]) const
	{
		BooleanExpr::BooleanInterpretation inter;

		for (int i = 25; i >= 0; i--)
		{
			if (vars[i])
			{
				if (number % 2 == 1)
					inter.setValue('A' + i, 1);
				number /= 2;
			}
		}
		return inter;
	}

	bool checkAll(BooleanExpr* expr, bool value) const
	{
		size_t varsCount = expr->countVars;
		size_t intersCount = 1 << varsCount; ///!!!!! 2^varsCount

		for (int i = 0; i < intersCount; i++)
		{
			if (expr->eval(convert(i, expr->vars)) != value)
				return false;
		}
		return true;
	}

	BooleanExpr* expr;
	void copyFrom(const ExpressionCalculator& other);
	void free();
	
public:
	ExpressionCalculator(const MyString& str);
	ExpressionCalculator(const ExpressionCalculator& other);
	ExpressionCalculator& operator=(const ExpressionCalculator& other);
	~ExpressionCalculator();

	bool isTautology() const;
	bool isContradiction() const;

	friend ExpressionCalculator::BooleanExpr* parse(const MyString& str);

};