Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 08
diff options
context:
space:
mode:
Commitce24f073c5e946b5bad600fca1ca8808657ee9ce (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2021-04-16 15:50:04 +0300
Committer
 angeld55 <angel_dimitriev@abv.bg>
2021-04-16 15:50:04 +0300
Files948e5f8ef602b7eff3822b289ce5c0ab16ba4bf2 /Sem. 08
Object-oriented_programming_FMI-ce24f073c5e946b5bad600fca1ca8808657ee9ce.tar
Object-oriented_programming_FMI-ce24f073c5e946b5bad600fca1ca8808657ee9ce.tar.gz
Object-oriented_programming_FMI-ce24f073c5e946b5bad600fca1ca8808657ee9ce.zip
Parentb233a370168ea6239f8233a299460f8f55cbf3c7 (diff)
added sem 8
diff --git a/Sem. 08/Complex/Complex.cpp b/Sem. 08/Complex/Complex.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Sem. 08/Complex/Complex.cpp
diff --git a/Sem. 08/Complex/Complex.h b/Sem. 08/Complex/Complex.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Sem. 08/Complex/Complex.h
diff --git a/Sem. 08/Complex/Source.cpp b/Sem. 08/Complex/Source.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Sem. 08/Complex/Source.cpp
diff --git a/Sem. 08/Nvector/Nvector.cpp b/Sem. 08/Nvector/Nvector.cpp
new file mode 100644
index 0000000..b4f983d
--- /dev/null
+++ b/Sem. 08/Nvector/Nvector.cpp
@@ -0,0 +1,176 @@
+#include "Nvector.h"
+
+Nvector::Nvector(size_t n) : size(n)
+{
+ data = new int[n];
+
+ for (int i = 0; i < n; i++)
+ data[i] = 0;
+}
+
+Nvector::Nvector(const Nvector& other)
+{
+ copyFrom(other);
+}
+
+Nvector& Nvector::operator=(const Nvector& other)
+{
+ if (this != &other)
+ {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+Nvector::~Nvector()
+{
+ free();
+}
+
+void Nvector::copyFrom(const Nvector& other)
+{
+ size = other.size;
+ data = new int[size];
+
+ for (int i = 0; i < size; i++)
+ data[i] = other.data[i];
+}
+void Nvector::free()
+{
+ delete[] data;
+ size = 0;
+}
+
+
+
+//v += v1;
+Nvector& Nvector::operator+=(const Nvector& other)
+{
+ if (size != other.size)
+ throw "The vectors should have the same size!";
+
+ for (int i = 0; i < size; i++)
+ data[i] += other.data[i];
+
+ return *this;
+}
+
+Nvector& Nvector::operator-=(const Nvector& other)
+{
+ if (size != other.size)
+ throw "The vectors should have the same size!";
+
+ for (int i = 0; i < size; i++)
+ data[i] -= other.data[i];
+
+ return *this;
+}
+
+const Nvector operator+(const Nvector& lhs, const Nvector& rhs)
+{
+ Nvector lhsCopy(lhs);
+ lhsCopy += rhs;
+
+ return lhsCopy;
+}
+
+const Nvector operator-(const Nvector& lhs, const Nvector& rhs)
+{
+ Nvector lhsCopy(lhs);
+ lhsCopy -= rhs;
+
+ return lhsCopy;
+}
+
+int& Nvector::operator[](size_t index)
+{
+ if (index >= size)
+ throw "Invalid index";
+
+ return data[index];
+}
+
+int Nvector::operator[](size_t index) const
+{
+ if (index >= size)
+ throw "Invalid index";
+
+ return data[index];
+}
+
+size_t Nvector::operator~() const
+{
+ return size;
+}
+
+std::ostream& operator<<(std::ostream& os, const Nvector& v)
+{
+ os << "[ ";
+ for (int i = 0; i < v.size; i++)
+ os << v[i] << " ";
+ os << " ]";
+
+ return os;
+}
+
+std::istream& operator>>(std::istream& is, Nvector& v)
+{
+ for (int i = 0; i < v.size; i++)
+ is >> v[i];
+
+ return is;
+}
+
+bool operator||(const Nvector& lhs, const Nvector& rhs)
+{
+ if (~lhs != ~rhs)
+ return false;
+
+ double coef = (double)lhs[0] / rhs[0];
+
+ double eps = 0.01;
+
+ for (int i = 1; i < ~lhs; i++)
+ {
+ double currCoef = (double)lhs[i] / rhs[i];
+
+ if (abs(currCoef - coef) > eps)
+ return false;
+ }
+ return true;
+}
+
+Nvector& Nvector::operator*=(size_t scalar)
+{
+ for (int i = 0; i < size; i++)
+ data[i] *= scalar;
+
+ return *this;
+}
+
+const Nvector operator*(const Nvector& lhs, size_t scalar)
+{
+ Nvector copy(lhs);
+ copy *= scalar;
+ return copy;
+}
+const Nvector operator*(size_t scalar, const Nvector& lhs)
+{
+ return lhs * scalar;
+}
+size_t operator%(const Nvector& lhs, const Nvector& rhs)
+{
+ if (~lhs != ~rhs)
+ throw "The vectors should have the same size!";
+
+ size_t res = 0;
+ for (int i = 0; ~lhs; i++)
+ res += lhs[i] * rhs[i];
+
+ return res;
+}
+
+size_t operator|=(const Nvector& lhs, const Nvector& rhs)
+{
+ return lhs % rhs == 0; //the size check is done by %
+} \ No newline at end of file
diff --git a/Sem. 08/Nvector/Nvector.h b/Sem. 08/Nvector/Nvector.h
new file mode 100644
index 0000000..745a844
--- /dev/null
+++ b/Sem. 08/Nvector/Nvector.h
@@ -0,0 +1,52 @@
+#pragma once
+#include <iostream>
+
+class Nvector
+{
+private:
+ int* data;
+ size_t size;
+
+ void copyFrom(const Nvector& other);
+ void free();
+
+public:
+ Nvector(size_t size);
+
+ Nvector(const Nvector& other);
+ Nvector& operator=(const Nvector& other);
+ ~Nvector();
+
+ Nvector& operator+=(const Nvector& other);
+ Nvector& operator-=(const Nvector& other);
+
+ Nvector& operator*=(size_t scalar);
+
+ int& operator[](size_t); //get set
+
+ int operator[](size_t) const; // get
+
+ void print();
+
+ size_t operator~() const;//ize;
+
+ friend std::ostream& operator<<(std::ostream& os, const Nvector& v);
+ friend std::istream& operator>>(std::istream&, Nvector& v);
+};
+
+const Nvector operator+(const Nvector& lhs, const Nvector& rhs);
+const Nvector operator-(const Nvector& lhs, const Nvector& rhs);
+
+const Nvector operator*(const Nvector& lhs, size_t scalar);
+//So it could be commutative. Is there a better way?
+const Nvector operator*(size_t scalar, const Nvector& lhs);
+
+//are parallel
+bool operator||(const Nvector& lhs, const Nvector& rhs);
+
+
+//scalar product
+size_t operator%(const Nvector& lhs, const Nvector& rhs);
+
+//are perpendicular
+bool operator||(const Nvector& lhs, const Nvector& rhs); \ No newline at end of file
diff --git a/Sem. 08/Nvector/Source.cpp b/Sem. 08/Nvector/Source.cpp
new file mode 100644
index 0000000..332ac22
--- /dev/null
+++ b/Sem. 08/Nvector/Source.cpp
@@ -0,0 +1,16 @@
+#include <iostream>
+#include <fstream>
+#include "Nvector.h"
+
+using namespace std;
+
+
+
+int main()
+{
+ Nvector v(3);
+ Nvector v2 = 3 * v;
+ cout << v2;
+
+}
+
diff --git a/Sem. 08/Nvector/TwoDimCoordSystem.cpp b/Sem. 08/Nvector/TwoDimCoordSystem.cpp
new file mode 100644
index 0000000..e7a7072
--- /dev/null
+++ b/Sem. 08/Nvector/TwoDimCoordSystem.cpp
@@ -0,0 +1,18 @@
+#include "TwoDimCoordSystem.h"
+
+TwoDimCoordSystem::TwoDimCoordSystem(const Nvector& v1, const Nvector& v2) : v1(v1), v2(v2)
+{
+ if (~v1 != 2 || ~v2 != 2)
+ throw "Size of vectors should be 2!";
+ if (v1 || v2)
+ throw "Vectors should not be parallel!";
+}
+
+Nvector TwoDimCoordSystem::getVectorByCoordinates(size_t coordX, size_t coordY) const
+{
+ //todo
+}
+Nvector TwoDimCoordSystem::getCoordinatesByVector(const Nvector& other) const
+{
+ //todo
+} \ No newline at end of file
diff --git a/Sem. 08/Nvector/TwoDimCoordSystem.h b/Sem. 08/Nvector/TwoDimCoordSystem.h
new file mode 100644
index 0000000..a2fdbfe
--- /dev/null
+++ b/Sem. 08/Nvector/TwoDimCoordSystem.h
@@ -0,0 +1,11 @@
+#include "Nvector.h"
+class TwoDimCoordSystem
+{
+ Nvector v1;
+ Nvector v2;
+
+ TwoDimCoordSystem(const Nvector& v1, const Nvector& rhs);
+
+ Nvector getVectorByCoordinates(size_t coordX, size_t coordY) const;
+ Nvector getCoordinatesByVector(const Nvector& other) const;
+}; \ No newline at end of file