diff options
clean up
3 files changed, 0 insertions, 166 deletions
| -rw-r--r-- | Sem. 16/LogicExpressions/LogicExpressions/LogicExpr.h | 150 | ||||
| -rw-r--r-- | Sem. 16/LogicExpressions/Source.cpp | 15 | ||||
| -rw-r--r-- | Sem. 16/README.md | 1 |
diff --git a/Sem. 16/LogicExpressions/LogicExpressions/LogicExpr.h b/Sem. 16/LogicExpressions/LogicExpressions/LogicExpr.h deleted file mode 100644 index 9188541..0000000 --- a/Sem. 16/LogicExpressions/LogicExpressions/LogicExpr.h +++ /dev/null @@ -1,150 +0,0 @@ -#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(const 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(const LogicExpr* expr, const std::vector<char>& vars) - { - return CheckValues(expr, vars, true); - } - - bool isContradiction(const LogicExpr* expr, const std::vector<char>& vars) - { - return CheckValues(expr, vars, false); - } -} - diff --git a/Sem. 16/LogicExpressions/Source.cpp b/Sem. 16/LogicExpressions/Source.cpp deleted file mode 100644 index af86b62..0000000 --- a/Sem. 16/LogicExpressions/Source.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "LogicExpressions/LogicExpr.h" - -using namespace LogicExpressions; - -// or |, and ^, negation !, implies >, iff - -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' }) << endl; -}
\ No newline at end of file diff --git a/Sem. 16/README.md b/Sem. 16/README.md deleted file mode 100644 index 8a445d4..0000000 --- a/Sem. 16/README.md +++ /dev/null @@ -1 +0,0 @@ -TBD
|
