-
Notifications
You must be signed in to change notification settings - Fork 0
/
classe complex.cpp
184 lines (148 loc) · 3.6 KB
/
classe 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
class complex{
private:
double re;
double im;
public:
complex(double reel,double imaginaire=0){
this->re=reel;
this->im=imaginaire;
}
static double real(complex a1){
return a1.re;
}
static double imag(complex a1){
return a1.im;
}
static complex conj(complex a1){
a1.im=a1.im*(-1);
return a1;
}
static double norm(complex a1){
return sqrt(pow(a1.re,2)+pow(a1.im,2));
}
static double arg(complex a1){
double tmp=2*atan(a1.im/(a1.re+norm(a1)));
return tmp;
}
static complex polar(double mag,double angle=0){
complex n((double)(mag*cos(angle)),(double)(mag*sin(angle)));
return n;
}
complex operator+(complex & c){
complex tmp(real(*this)+real(c),imag(*this)+imag(c));
return tmp;
}
friend complex operator+(double & n ,complex & c){
complex tmp(n+real(c),imag(c));
return tmp;
}
friend complex operator+(complex & c,double & n){
complex tmp(n+real(c),imag(c));
return tmp;
}
complex operator-(complex & c){
complex tmp(real(*this)-real(c),imag(*this)-imag(c));
return tmp;
}
friend complex operator-(double & n ,complex & c){
complex tmp(n-real(c),imag(c));
return tmp;
}
friend complex operator-(complex & c,double & n){
complex tmp(real(c)-n,imag(c));
return tmp;
}
complex operator*(complex & c){
complex tmp((real(*this)*real(c))-(imag(*this)*imag(c)),imag(c)*real(*this)+imag(*this)*real(c));
return tmp;
}
friend complex operator*(complex & c,double & n){
complex tmp(n*real(c),n*imag(c));return tmp;
}
friend complex operator*(double & n ,complex & c){
complex tmp(n*real(c),n*imag(c));return tmp;
}
complex operator/(complex & c){
complex tmp(((real(*this)*real(c))+(imag(*this)*imag(c)))/(pow(real(c),2)+pow(imag(c),2)));
return tmp;
}
friend complex operator/(complex & c,double & n){
complex tmp(real(c)/n,imag(c)/n);return tmp;
}
friend complex operator/(double & n ,complex & c){
complex tmp(real(c)/n,imag(c)/n);return tmp;
}
bool operator==(complex & a)
{
if(real(*this)==real(a) && imag(*this)==imag(a))
return true;
return false;
}
bool operator!=(complex & a)
{
if(real(a)==real(*this) && imag(a)==imag(*this))
return false;
return true;
}
complex operator-(){
complex tmp(real(*this),(-1)*imag(*this));
return tmp;
}
friend ostream &operator<<(ostream& ,complex & a);
friend istream &operator>>(istream& ,complex & a);
};
ostream &operator<<(ostream& outt, complex &a){
if(a.imag(a)==1){
if(a.real(a)!=0)
outt << a.real(a)<<"+"<<"i" ;
else
outt <<"i" ;
return outt;
}
if(a.imag(a)==-1){
if(a.real(a)!=0)
outt << a.real(a)<<"-"<<"i" ;
else
outt <<"-"<<"i" ;
return outt;
}
if(a.imag(a)>=1){
if(a.real(a)!=0)
outt << a.real(a)<<"+"<<a.imag(a)<<"i" ;
else
outt <<a.imag(a)<<"i";
return outt;
}
if(a.imag(a)<0){
if(a.real(a)!=0)
outt << a.real(a)<<a.imag(a)<<"i" ;
else
outt <<a.imag(a)<<"i";
return outt;
}
if(a.imag(a)==0){
if(a.real(a)!=0)
outt << a.real(a);
else
outt <<a.imag(a);
return outt;
}
}
istream& operator>>(istream& inn,complex &a){
cout << "\n Inserer la partie reele ! ";
inn >> a.re;
cout << "\n Inserer la partie imaginaire ! ";
inn >> a.im;
return inn;
}
int main(){
complex a(1,3);
double pi=3.14,n=2;
complex b=a.polar(n,pi/2);
cout <<b<<"\n";
}