Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitd2422da2c1b2fcf75a3212eb36e94a718a39c0a5 (patch)
Author
 Angeld55 <achi_d@abv.bg>
2019-09-20 16:02:36 +0300
Committer
 GitHub <noreply@github.com>
2019-09-20 16:02:36 +0300
Files153acaf6a9084a50972b7542d737290219f72cf3
Object-oriented_programming_FMI-d2422da2c1b2fcf75a3212eb36e94a718a39c0a5.tar
Object-oriented_programming_FMI-d2422da2c1b2fcf75a3212eb36e94a718a39c0a5.tar.gz
Object-oriented_programming_FMI-d2422da2c1b2fcf75a3212eb36e94a718a39c0a5.zip
Parent5e5abf297fcac67a3eb59db0a520eb6e63ffa531 (diff)
Add files via upload
diff --git a/Polymorphism/MathExpression/Expression.hpp b/Polymorphism/MathExpression/Expression.hpp
index 4bcfe98..dd344d8 100644
--- a/Polymorphism/MathExpression/Expression.hpp
+++ b/Polymorphism/MathExpression/Expression.hpp
@@ -1 +1,14 @@
-d
+#include <iostream>
+
+class Expression
+{
+public:
+ virtual void Print() = 0;
+ virtual int Evaluate() = 0;
+ virtual ~Expression(){}
+ int getType(){ return type; }
+protected:
+ int type; //Ако обектът е NUM, ще пазим тук 1, а ако обектът е Операция, ще пазим тук 2.
+};
+
+
diff --git a/Polymorphism/MathExpression/Num.hpp b/Polymorphism/MathExpression/Num.hpp
new file mode 100644
index 0000000..d2da4ae
--- /dev/null
+++ b/Polymorphism/MathExpression/Num.hpp
@@ -0,0 +1,21 @@
+#include "Expression.hpp"
+class Num : public Expression
+{
+ int number;
+public:
+ Num(int number)
+ {
+ this->number = number;
+ type = 1;
+ }
+ void Print()
+ {
+ std::cout << number;
+ }
+ int Evaluate()
+ {
+ return number;
+ }
+
+
+}; \ No newline at end of file
diff --git a/Polymorphism/MathExpression/Operation.hpp b/Polymorphism/MathExpression/Operation.hpp
new file mode 100644
index 0000000..cfa9534
--- /dev/null
+++ b/Polymorphism/MathExpression/Operation.hpp
@@ -0,0 +1,46 @@
+class Operation : public Expression
+{
+
+public:
+ Expression* left;
+ char op;
+ Expression* right;
+ Operation(Expression* left, char ch, Expression* right)
+ {
+
+ this->left = left;
+ op = ch;
+ this->right = right;
+ type = 2;
+
+ }
+ void Print()
+ {
+ std::cout << '(';
+ left->Print();
+ std::cout << op;
+ right->Print();
+ std::cout << ')';
+ }
+ int Evaluate()
+ {
+ int result = left->Evaluate();
+ int value2 = right->Evaluate();
+
+ if (op == '+')
+ result += value2;
+ else if (op == '*')
+ result *= value2;
+ else
+ return 0; //Invalid operation
+ return result;
+ }
+ ~Operation()
+ {
+
+ delete left;
+ delete right;
+ op = '\0';
+ }
+
+}; \ No newline at end of file
diff --git a/Polymorphism/MathExpression/main.hpp b/Polymorphism/MathExpression/main.hpp
new file mode 100644
index 0000000..a309f44
--- /dev/null
+++ b/Polymorphism/MathExpression/main.hpp
@@ -0,0 +1,20 @@
+#include "Expression.hpp"
+int main()
+{
+ Expression* l = new Num(3);
+ Expression* r = new Num(7);
+ Expression* final1 = new Operation(l, '+', r);
+
+ Expression* l2 = new Num(3);
+ Expression* r2 = new Num(7);
+ Expression* final2 = new Operation(l2, '+', r2);
+
+ std::cout << final1->Evaluate();
+
+ std::cout << AreTheSame(final1, final2);
+
+ delete final1, final2; //Винаги трием най-големите 'Expression-и'. Те се грижат за по-малките от които са образувани.
+
+
+
+} \ No newline at end of file