Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 16 / LogicExpressions / LogicExpr.h
blob: ebb84b4b60e06122ec69bfb027d2c4f7dfff5e5c (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
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

namespace LogicExpressions
{

	const int ENGLISH_ALPHABET_SIZE = 26;
	struct BooleanInterpretation
	{
		bool values[ENGLISH_ALPHABET_SIZE];
		BooleanInterpretation()
		{
			for (int i = 0; i < ENGLISH_ALPHABET_SIZE; i++)
				values[i] = false;
		}
		void setVarValue(char v, bool value)
		{
			values[v - 'a'] = value;
		}

		bool getVarValue(char v) const
		{
			return values[v - 'a'];
		}
	};

	struct LogicExpr
	{
		virtual void print() const = 0;
		LogicExpr() = default;
		LogicExpr(const LogicExpr&) = delete;
		LogicExpr& operator=(const LogicExpr& other) = delete;

		virtual bool evaluate(const BooleanInterpretation& interpret) const = 0;
		virtual ~LogicExpr(){};
	};
	struct Var : public LogicExpr
	{
		char v;
		Var(char v) :v(v){}

		void print() const
		{
			cout << v;
		}

		bool evaluate(const BooleanInterpretation& interpret) const
		{
			return interpret.getVarValue(v);
		}
	};
	struct Operation : public LogicExpr
	{
		LogicExpr* left;
		char op;
		LogicExpr* right;

		Operation(LogicExpr* lhs, char op, LogicExpr* rhs) : left(lhs), op(op), right(rhs){}

		void print() const
		{
			cout << "(";
			left->print();
			cout << op;
			right->print();
			cout << ")";
		}
		~Operation()
		{
			delete left;
			delete right;
		}

		bool evaluate(const BooleanInterpretation& interpret) const
		{
			bool res1 = left->evaluate(interpret);
			bool res2 = right->evaluate(interpret);

			switch (op)
			{
			case '^':return res1 && res2;
			case '|':return res1 || res2;
			case '>':return !res1 || res2;
			case '+': return res1 ^ res2;
			case '-': return (res1 && res2) || (!res1 && !res2);
			}

		}
	};
	struct Negation : public LogicExpr
	{
		LogicExpr* current;
		Negation(LogicExpr* current) :current(current){}

		void print() const
		{
			cout << "!(";
			current->print();
			cout << ")";
		}
		~Negation()
		{
			delete current;
		}

		bool evaluate(const BooleanInterpretation& interpret) const
		{
			bool result = current->evaluate(interpret);
			return !result;
		}
	};

	BooleanInterpretation convertNumberToInterpretation(size_t n, const std::vector<char>& vars)
	{
		BooleanInterpretation current;
		for (int i = vars.size() - 1; i >= 0; i--, n/=2)
			current.setVarValue(vars[i], n % 2);

		return std::move(current);
	}

	bool CheckValues(LogicExpr* expr, const std::vector<char>& vars, bool value)
	{
		size_t checksCounts = 1 << vars.size();

		for (int i = 0; i < checksCounts; i++)
		{
			BooleanInterpretation currentInterpret = convertNumberToInterpretation(i, vars);
		
			if (expr->evaluate(currentInterpret) != value)
				return false;
		}
		return true;
	}

	bool isTautology(LogicExpr* expr, const std::vector<char>& vars)
	{
		return CheckValues(expr, vars, true);
	}

	bool isContradiction(LogicExpr* expr, const std::vector<char>& vars)
	{
		return CheckValues(expr, vars, false);
	}
}