-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.cpp
executable file
·127 lines (107 loc) · 2.74 KB
/
complex.cpp
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
// complex.cpp -- implementacja metod klasy complex
#include <iostream>
#include <cmath>
#include "complex.h"
using namespace std;
Complex::Complex()
{
Real = 0.0; Imag = 0.0;
}
Complex::Complex(double re) : Real(re), Imag(0.0)
{
}
Complex::Complex(double re, double im) : Real(re), Imag(im)
{
}
Complex::Complex(const Complex& comp) : Real(comp.Real), Imag(comp.Imag)
{
}
// **** FUNKCJE SKKLADOWE KLASY ****
Complex & Complex::operator=(const Complex & comp)
{
if(&comp != this)
{
Real = comp.Real; Imag = comp.Imag;
}
return *this;
}
Complex & Complex::operator+=( const Complex comp)
{
Real += comp.Real;
Imag += comp.Imag;
return *this;
}
Complex & Complex::operator-=( const Complex comp)
{
Real -= comp.Real;
Imag -= comp.Imag;
return *this;
}
Complex & Complex::operator*=( const Complex comp)
{
Real = (Real * comp.Real - Imag * comp.Imag);
Imag = (Real * comp.Imag + Imag * comp.Real);
return *this;
}
Complex & Complex::operator/=( const Complex comp)
{
Real = (Real*comp.Real + Imag*comp.Imag) / (pow(comp.Real,2) + pow(comp.Imag,2));
Imag = (Imag*comp.Real - Real*comp.Imag) / (pow(comp.Real,2) + pow(comp.Imag,2));
return *this;
}
double Complex::cz_Rzeczywista()
{
return Real;
}
double Complex::cz_Urojona()
{
return Imag;
}
double Complex::modul() const
{
return (sqrt(pow(Real,2)+pow(Imag, 2)));
}
double Complex::faza() const
{
return atan(Real/Imag)* 180/M_PI;
}
Complex Complex::operator+ (const Complex& co) const
{
Complex n;
n.Real = this->Real + co.Real;
n.Imag = this->Imag + co.Imag;
return n;
}
Complex Complex::operator+ (const double dd) const
{
return Complex(Real + dd, Imag);
}
Complex Complex::operator-( const Complex & comp2)
{
return (Complex(Real - comp2.Real, Imag - comp2.Imag));
}
Complex Complex::operator-( const double dd)
{
return (Complex(Real - dd, Imag ));
}
bool Complex::operator==(const Complex &comp) const
{
if ((Real == comp.Real) && (Imag == comp.Imag))
return true;
else
return false;
}
// ***** FUNKCJE ZAPRZYJAZNIONE *****
Complex operator*(const Complex & comp1, const Complex & comp2)
{
return (Complex((comp1.Real * comp2.Real - comp1.Imag * comp2.Imag), (comp1.Real * comp2.Imag + comp1.Imag * comp2.Real)));
}
Complex operator/(const Complex & comp1, const Complex & comp2)
{
return (Complex((comp1.Real*comp2.Real + comp1.Imag*comp2.Imag) / (pow(comp2.Real,2) + pow(comp2.Imag,2)), (comp1.Imag*comp2.Real - comp1.Real*comp2.Imag) / (pow(comp2.Real,2) + pow(comp2.Imag,2))));
}
ostream & operator << (ostream & s, const Complex & comp)
{
s << "(" << comp.Real << " + " << comp.Imag << "i)";
return s;
}