Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitf1e6772e0b264388cf96500b75c6ae47272f2238 (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2022-06-07 21:18:29 +0300
Committer
 angeld55 <angel_dimitriev@abv.bg>
2022-06-07 21:18:29 +0300
Filesba30e9f48d807ad1478ab985bc4e8781f2bf3341
Object-oriented_programming_FMI-f1e6772e0b264388cf96500b75c6ae47272f2238.tar
Object-oriented_programming_FMI-f1e6772e0b264388cf96500b75c6ae47272f2238.tar.gz
Object-oriented_programming_FMI-f1e6772e0b264388cf96500b75c6ae47272f2238.zip
Parent30601882aa51c1c6aa646c3bb6001f2c848adde7 (diff)
sem 15
diff --git a/Sem. 14/Chess_basic_prototype/ChessBoard/ChessBoard.cpp b/Sem. 14/Chess_basic_prototype/ChessBoard/ChessBoard.cpp
index ed9823d..70276df 100644
--- a/Sem. 14/Chess_basic_prototype/ChessBoard/ChessBoard.cpp
+++ b/Sem. 14/Chess_basic_prototype/ChessBoard/ChessBoard.cpp
@@ -60,7 +60,7 @@ void ChessBoard::moveFigure(size_t x, size_t y, size_t destX, size_t destY)
{
if (board[x][y].f && board[x][y].f->canBeMoved(x, y, destX, destY))
{
- delete board[destX][destY].f;
+ delete board[destX][destY].f; //взимане на фигура
board[destX][destY].f = board[x][y].f;
board[x][y].f = nullptr;
}
diff --git a/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.cpp b/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.cpp
index 4d94406..e69de29 100644
--- a/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.cpp
+++ b/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.cpp
@@ -1 +0,0 @@
-todo \ No newline at end of file
diff --git a/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.h b/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.h
index 9e2300a..53bbf85 100644
--- a/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.h
+++ b/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.h
@@ -17,5 +17,5 @@ public:
bool getIsWhite() const { return isWhite; };
FigureType getType() const { return type; }
- virtual ~Figure(){};
+ virtual ~Figure() = default;
}; \ No newline at end of file
diff --git a/Sem. 15/ExpessionCalculator/ExpressionCalculator.cpp b/Sem. 15/ExpessionCalculator/ExpressionCalculator.cpp
new file mode 100644
index 0000000..86c68c8
--- /dev/null
+++ b/Sem. 15/ExpessionCalculator/ExpressionCalculator.cpp
@@ -0,0 +1,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);
+} \ No newline at end of file
diff --git a/Sem. 15/ExpessionCalculator/ExpressionCalculator.h b/Sem. 15/ExpessionCalculator/ExpressionCalculator.h
new file mode 100644
index 0000000..eb21a99
--- /dev/null
+++ b/Sem. 15/ExpessionCalculator/ExpressionCalculator.h
@@ -0,0 +1,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);
+
+}; \ No newline at end of file
diff --git a/Sem. 15/MyString/MyString.cpp b/Sem. 15/MyString/MyString.cpp
new file mode 100644
index 0000000..c9d025e
--- /dev/null
+++ b/Sem. 15/MyString/MyString.cpp
@@ -0,0 +1,200 @@
+#include "MyString.h"
+#include <cstring>
+#include <iostream>
+#include <iomanip>
+
+void MyString::copyFrom(const MyString& other)
+{
+ str = new char[strlen(other.str) + 1];
+ strcpy(str, other.str);
+ size = other.size;
+}
+void MyString::free()
+{
+ delete[] str;
+}
+
+MyString::MyString(const char* data)
+{
+ if(data == nullptr)
+ {
+ str = new char[1];
+ str[0] = '\0';
+ size = 0;
+ }
+ else
+ {
+ size = strlen(data);
+ str = new char[size + 1];
+ strcpy(str, data);
+ }
+}
+
+size_t getNumSize(size_t num)
+{
+ size_t res = 0;
+ while (num)
+ {
+ res++;
+ num /= 10;
+ }
+ return res;
+}
+MyString::MyString(size_t n)
+{
+ int numSize = getNumSize(n);
+ str = new char[numSize + 1];
+
+ str[numSize] = '\0';
+
+ for (size_t i = 0; i < numSize; i++, n /= 10)
+ str[numSize - 1 - i] = (n % 10) + '0';
+ size = numSize;
+}
+
+MyString::MyString()
+{
+ str = new char[1];
+ str[0] = '\0';
+ size = 0;
+}
+
+MyString::MyString(const MyString& other)
+{
+ copyFrom(other);
+}
+MyString& MyString::operator=(const MyString& other)
+{
+ if (this != &other)
+ {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+MyString::~MyString()
+{
+ free();
+}
+
+
+size_t MyString::getSize() const
+{
+ return size;
+}
+void MyString::concatFrom(const MyString& other)
+{
+ char* temp = new char[getSize() + other.getSize() + 1];
+ strcpy(temp, str);
+ strcat(temp, other.str);
+
+ delete[] str;
+ str = temp;
+ size = size + other.getSize();
+}
+
+const char* MyString::c_str() const
+{
+ return str;
+}
+
+MyString& MyString::operator+=(const MyString& other)
+{
+ concatFrom(other);
+ return *this;
+}
+
+MyString operator+(const MyString& lhs, const MyString& rhs)
+{
+ MyString copyOfLeft(lhs);
+ copyOfLeft += rhs;
+ return copyOfLeft;
+}
+
+std::ostream& operator<<(std::ostream& stream, const MyString& str)
+{
+ stream << str.str;
+ return stream;
+}
+
+std::istream& operator>>(std::istream& stream, MyString& str)
+{
+ delete[] str.str;
+ char buff[1024];
+ stream >> std::setw(1024) >> buff;
+
+ str.size = strlen(buff);
+ str.str = new char[str.size + 1];
+ strcpy(str.str, buff);
+
+ return stream;
+}
+
+bool operator==(const MyString& lhs, const MyString& rhs)
+{
+ return strcmp(lhs.c_str(), rhs.c_str()) == 0;
+}
+bool operator<=(const MyString& lhs, const MyString& rhs)
+{
+ return strcmp(lhs.c_str(), rhs.c_str()) <= 0;
+
+}
+bool operator<(const MyString& lhs, const MyString& rhs)
+{
+ return strcmp(lhs.c_str(), rhs.c_str()) < 0;
+
+}
+
+MyString::MyString(MyString&& otherString)
+{
+ str = otherString.str;
+ size = otherString.size;
+ otherString.str = nullptr;
+}
+
+MyString& MyString::operator=(MyString&& otherString)
+{
+
+ if (this != &otherString)
+ {
+ free();
+ str = otherString.str;
+ otherString.str = nullptr;
+ size = otherString.size;
+ }
+ return *this;
+}
+
+MyString MyString::subString(size_t start, size_t end) const
+{
+ if (end < start || end >= size)
+ throw std::logic_error("Invalid params");
+
+ MyString result;
+ delete[] result.str;
+
+ result.str = new char[end - start + 2];
+ for (int i = start; i <= end; i++)
+ result.str[i - start] = str[i];
+
+
+ result.str[(end - start) + 1] = '\0';
+ result.size = end - start + 1;
+
+ return result;
+}
+
+const char& MyString::operator[](size_t index) const
+{
+ if (index > size)
+ throw std::out_of_range("Out of range!");
+ return str[index];
+}
+
+
+char& MyString::operator[](size_t index)
+{
+ if (index > size)
+ throw std::out_of_range("Out of range!");
+ return str[index];
+} \ No newline at end of file
diff --git a/Sem. 15/MyString/MyString.h b/Sem. 15/MyString/MyString.h
new file mode 100644
index 0000000..b6d7c13
--- /dev/null
+++ b/Sem. 15/MyString/MyString.h
@@ -0,0 +1,48 @@
+#pragma once
+#include <iostream>
+#include <fstream>
+class MyString
+{
+ char* str;
+ size_t size;
+
+ void copyFrom(const MyString& other);
+ void free();
+
+ void concatFrom(const MyString& other);
+
+public:
+ MyString();
+ MyString(const char*);
+ MyString(size_t n); //convert constructor
+
+ MyString(const MyString&);
+
+ MyString(MyString&&); //move constructor that accepts rvalue;
+
+ MyString& operator=(const MyString& other);
+ MyString& operator=(MyString&& other); //move op= that accepts rvalue;
+
+ const char& operator[](size_t index) const;
+ char& operator[](size_t index);
+
+ ~MyString();
+
+ size_t getSize() const;
+
+ const char* c_str() const;
+
+ MyString& operator+=(const MyString& other);
+
+ friend std::ostream& operator<<(std::ostream& stream, const MyString& str);
+ friend std::istream& operator>>(std::istream& stream, MyString& str);
+
+ MyString subString(size_t start, size_t end) const;
+
+};
+
+MyString operator+(const MyString& lhs, const MyString& rhs);
+
+bool operator==(const MyString& lhs, const MyString& rhs);
+bool operator<=(const MyString& lhs, const MyString& rhs);
+bool operator<(const MyString& lhs, const MyString& rhs);
diff --git a/Sem. 15/Source.cpp b/Sem. 15/Source.cpp
new file mode 100644
index 0000000..8faca6f
--- /dev/null
+++ b/Sem. 15/Source.cpp
@@ -0,0 +1,10 @@
+#include <iostream>
+#include <exception>
+#include "MyString/MyString.h"
+#include "ExpessionCalculator/ExpressionCalculator.h"
+
+int main()
+{
+ ExpressionCalculator calc("(Pv(!P))");
+ std::cout << calc.isTautology();
+} \ No newline at end of file