-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcomplex.cpp
101 lines (99 loc) · 2.9 KB
/
tcomplex.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
#include "tcomplex.h"
#include "tanumber.h"
#include <math.h>
TComplex::TComplex() {
this->Real = 1.0;
this->Img = 0.0;
}
TComplex::TComplex(double real, double img) {
Real = real;
Img = img;
}
TComplex::TComplex(QString &str) {
int separator = str.indexOf("i*");
this->Real = str.left(separator-1).toInt();
this->Img = str.right(separator-1).toInt();
if(str[separator-1] == "-") this->Img = -this->Img;
}
TComplex* TComplex::copy(){
TComplex* d = new TComplex();
d->Real = this->Real;
d->Img = this->Img;
return d;
}
TComplex& TComplex::operator+(TANumber& B){
TComplex pi = static_cast<TComplex&>(B);
TComplex* result = new TComplex();
result->Real = this->Real + pi.Real;
result->Img = this->Img + pi.Img;
return *result;
}
TComplex& TComplex::operator-(TANumber& B){
TComplex pi = static_cast<TComplex&>(B);
TComplex* result = new TComplex();
result->Real = this->Real - pi.Real;
result->Img = this->Img - pi.Img;
return *result;
}
TComplex& TComplex::operator*(TANumber& B){
TComplex pi = static_cast<TComplex&>(B);
TComplex* result = new TComplex();
result->Real = (this->Real * pi.Real) - (this->Img * pi.Img);
result->Img = (this->Real * pi.Img) + (this->Img * pi.Real);
return *result;
}
TComplex& TComplex::operator/(TANumber& B){
TComplex pi = static_cast<TComplex&>(B);
TComplex* result = new TComplex();
result->Real = (this->Real*pi.Real + this->Img*pi.Img)/(pi.Real*pi.Real + pi.Img*pi.Img);
result->Img = (this->Img*pi.Real - this->Real*pi.Img)/(pi.Real*pi.Real + pi.Img*pi.Img);
return *result;
}
TComplex& TComplex::sqr(){
TComplex* result = new TComplex();
result->Real = (this->Real * this->Real) - (this->Img * this->Img);
result->Img = (this->Real * this->Img) + (this->Img * this->Real);
return *result;
}
TComplex& TComplex::rev(){
TComplex* result = new TComplex();
result->Real = this->Real/(this->Real*this->Real + this->Img*this->Img);
result->Img = -this->Img/(this->Real*this->Real + this->Img*this->Img);
return *result;
}
bool TComplex::operator==(TANumber& other){
TComplex pi = static_cast<TComplex&>(other);
if(fabs(this->Real-pi.Real) < 0.00001 || fabs(this->Img-pi.Img) < 0.00001){
return true;
}
else{
return false;
}
}
bool TComplex::operator!=(const TComplex &other){
if(fabs(this->Real-other.Real) > 0.00001 || fabs(this->Img-other.Img) > 0.00001){
return true;
}
else{
return false;
}
}
TComplex& TComplex::operator-(){
TComplex* d = new TComplex();
d->Real = -this->Real;
d->Img = this->Img;
return *d;
}
bool TComplex::isZero(){
if(this->Real == 0.0) return true;
else return false;
}
TComplex& TComplex::get(){
return *this;
}
void TComplex::set(TANumber& A){
*this = static_cast<TComplex&>(A);
}
CEditor& TComplex::getRedactor(){
return *(new CEditor());
}