From 9603d0847a01c555c2c9286294b3ccdea3e38be8 Mon Sep 17 00:00:00 2001 From: angeld55 Date: Sat, 12 Jun 2021 18:30:23 +0300 Subject: sem 16 --- Sem. 16/LogicExpressions/LogicExpr.h | 150 --------------------- .../LogicExpressions/LogicExpressions/LogicExpr.h | 150 +++++++++++++++++++++ Sem. 16/LogicExpressions/Source.cpp | 15 +++ Sem. 16/Source.cpp | 15 --- 4 files changed, 165 insertions(+), 165 deletions(-) delete mode 100644 Sem. 16/LogicExpressions/LogicExpr.h create mode 100644 Sem. 16/LogicExpressions/LogicExpressions/LogicExpr.h create mode 100644 Sem. 16/LogicExpressions/Source.cpp delete mode 100644 Sem. 16/Source.cpp diff --git a/Sem. 16/LogicExpressions/LogicExpr.h b/Sem. 16/LogicExpressions/LogicExpr.h deleted file mode 100644 index ebb84b4..0000000 --- a/Sem. 16/LogicExpressions/LogicExpr.h +++ /dev/null @@ -1,150 +0,0 @@ -#include -#include -#include -#include - -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& 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& 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& vars) - { - return CheckValues(expr, vars, true); - } - - bool isContradiction(LogicExpr* expr, const std::vector& vars) - { - return CheckValues(expr, vars, false); - } -} - diff --git a/Sem. 16/LogicExpressions/LogicExpressions/LogicExpr.h b/Sem. 16/LogicExpressions/LogicExpressions/LogicExpr.h new file mode 100644 index 0000000..9188541 --- /dev/null +++ b/Sem. 16/LogicExpressions/LogicExpressions/LogicExpr.h @@ -0,0 +1,150 @@ +#include +#include +#include +#include + +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& 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(const LogicExpr* expr, const std::vector& 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(const LogicExpr* expr, const std::vector& vars) + { + return CheckValues(expr, vars, true); + } + + bool isContradiction(const LogicExpr* expr, const std::vector& vars) + { + return CheckValues(expr, vars, false); + } +} + diff --git a/Sem. 16/LogicExpressions/Source.cpp b/Sem. 16/LogicExpressions/Source.cpp new file mode 100644 index 0000000..2b810b8 --- /dev/null +++ b/Sem. 16/LogicExpressions/Source.cpp @@ -0,0 +1,15 @@ +#include "LogicExpressions/LogicExpr.h" + +using namespace LogicExpressions; + + +int main() +{ + LogicExpr* expr = new Operation(new Var('p'), '|', new Negation(new Var('p'))); // p & !p + expr->print(); + cout << " " << isTautology(expr, { 'p' }) << endl; + + LogicExpr* expr2 = new Operation(new Var('q'),'^',new Operation(new Var('p'), '^', new Negation(new Var('p')))); // q ^ (p ^ !p) + expr2->print(); + cout << " " << isContradiction(expr2, { 'p', 'q' }); +} \ No newline at end of file diff --git a/Sem. 16/Source.cpp b/Sem. 16/Source.cpp deleted file mode 100644 index 2b810b8..0000000 --- a/Sem. 16/Source.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "LogicExpressions/LogicExpr.h" - -using namespace LogicExpressions; - - -int main() -{ - LogicExpr* expr = new Operation(new Var('p'), '|', new Negation(new Var('p'))); // p & !p - expr->print(); - cout << " " << isTautology(expr, { 'p' }) << endl; - - LogicExpr* expr2 = new Operation(new Var('q'),'^',new Operation(new Var('p'), '^', new Negation(new Var('p')))); // q ^ (p ^ !p) - expr2->print(); - cout << " " << isContradiction(expr2, { 'p', 'q' }); -} \ No newline at end of file -- cgit v1.3.1-23-gd82d