Website Summary Files Commits Branches @commit @diff @stats
path: root / Operator_overloading / Complex / Complex.hpp
blob: 8a6edd50496009329d142f7444d2de1bb65390d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
class Complex
{
private:
	double real;
	double im;

public:
	Complex();
	Complex(double real, double im);

	Complex getConjugated() const;  //връща комплексно спрегнатото число на текущия обект.

    //assignment operators 
	// a+=b Към а се добавя стойността на b. а се променя , b не се. Операцията  ще върне a за да може да се правят верижни изрази (a+=b+=c)
	Complex& operator+=(const Complex&);
	Complex& operator-=(const Complex&);
	Complex& operator*=(const Complex&);
	Complex& operator/=(const Complex&);

	

	//stream opeartors   ( std::cout>> c1)  (std::cin<<c1 )
	friend std::ostream& operator<<(std::ostream&, const Complex&);
	friend std::istream& operator>>(std::istream&, Complex&);

};
// a+b. Операторът връща НОВА променлива(Complex), която е резултатът от операцията.
Complex operator+(const Complex&, const Complex&);
Complex operator-(const Complex&, const Complex&);
Complex operator*(const Complex&, const Complex&);
Complex operator/(const Complex&, const Complex&);



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;
}