Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 15 / ExpessionCalculator / ExpressionCalculator.cpp
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 /Sem. 15/ExpessionCalculator/ExpressionCalculator.cpp
Object-oriented_programming_FMI-f1e6772e0b264388cf96500b75c6ae47272f2238.tar
Object-oriented_programming_FMI-f1e6772e0b264388cf96500b75c6ae47272f2238.tar.gz
Object-oriented_programming_FMI-f1e6772e0b264388cf96500b75c6ae47272f2238.zip
Parent30601882aa51c1c6aa646c3bb6001f2c848adde7 (diff)
sem 15
1 files changed, 77 insertions, 0 deletions
-rw-r--r--Sem. 15/ExpessionCalculator/ExpressionCalculator.cpp77
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