Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 08 / Complex
diff options
context:
space:
mode:
Commitab2914d945d9065fabd564ff8185405390841510 (patch)
Author
 Angel Dimitriev <angel_dimitriev@abv.bg>
2022-04-12 19:32:14 +0300
Committer
 GitHub <noreply@github.com>
2022-04-12 19:32:14 +0300
Files645a18aa1be1a5ce2a76f24fd86d14f73f160478 /Sem. 08/Complex
Object-oriented_programming_FMI-ab2914d945d9065fabd564ff8185405390841510.tar
Object-oriented_programming_FMI-ab2914d945d9065fabd564ff8185405390841510.tar.gz
Object-oriented_programming_FMI-ab2914d945d9065fabd564ff8185405390841510.zip
Parent333283d165dab16a7d55209cf92d65efd13af3af (diff)
Create Complex.cpp
1 files changed, 102 insertions, 0 deletions
-rw-r--r--Sem. 08/Complex/Complex.cpp102
diff --git a/Sem. 08/Complex/Complex.cpp b/Sem. 08/Complex/Complex.cpp
new file mode 100644
index 0000000..9344f0a
--- /dev/null
+++ b/Sem. 08/Complex/Complex.cpp
@@ -0,0 +1,102 @@
+#include "Complex.h"
+
+Complex::Complex()
+{
+ real = 0;
+ im = 0;
+}
+Complex::Complex(double real, double im)
+{
+ this->real = real;
+ this->im = im;
+}
+Complex& Complex::operator+=(const Complex& other)
+{
+ real += other.real;
+ im += other.im;
+ return *this;
+}
+Complex& Complex::operator-=(const Complex& other)
+{
+ real -= other.real;
+ im -= other.im;
+ return *this;
+}
+Complex& Complex::operator*=(const Complex& other)
+{
+ int oldReal = real;
+ real = real*other.real - im*other.im;
+ im = oldReal*other.im + im * other.real;
+ return *this;
+}
+Complex& Complex::operator/=(const Complex& other) // a/=b
+{
+ Complex Conjugated = other.getConjugated(); //взимаме комплексно спрегнатата на другата дроб (b)
+
+ Complex otherCopy(other);// копираме другата (b), за да не я промяняме.
+
+ //Умножаваме двете по комплексно спрегнатата.
+ *this *= Conjugated;
+ otherCopy *= Conjugated; //Тук трябва да остане само реална част.
+
+ if (otherCopy.real != 0)
+ {
+ real /= otherCopy.real;
+ im /= otherCopy.real;
+ }
+
+ return *this;
+}
+
+
+//След дефинирането на +=, -=, *= и /=, можем лесно да дефинираме +, -, * и /, преизползвайки вече написаните опратори.
+Complex operator+(const Complex& lhs, const Complex& rhs) { //a+b
+
+ Complex result(lhs); // Създаваме нов обект, който е копие на a (използваме копи-конструктора)
+ result += rhs; //към копието на a, добавяме b (Използваме вече дефинирания оператор +=)
+
+ return result;
+}
+//Операторите -, *, / са аналогични с +.
+Complex operator-(const Complex& lhs, const Complex& rhs) {
+ Complex result(lhs);
+ result -= rhs;
+
+ return result;
+}
+
+Complex operator*(const Complex& lhs, const Complex& rhs) {
+
+ Complex result(lhs);
+ result *= rhs;
+
+ return result;
+}
+
+Complex operator/(const Complex& lhs, const Complex& rhs) {
+
+ Complex result(lhs);
+ result /= rhs;
+
+ return result;
+}
+
+Complex Complex::getConjugated() const
+{
+ Complex result(*this);
+ result.im *= -1;
+
+ return result;
+}
+
+// за да можем да правим cout<<c1 или да запазваме във файл и др.
+std::ostream& operator<<(std::ostream& ofs, const Complex& r) {
+
+ return ofs << r.real << ' ' << r.im << 'i';
+}
+
+// за да можем да правим cin>>c1 или да четем от файл и др.
+std::istream& operator>>(std::istream& ifs, Complex& r) {
+
+ return ifs >> r.real >> r.im;
+}