-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.c
89 lines (85 loc) · 2.68 KB
/
complex.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "complex.h"
struct cplx {
double reel;
double imaginaire;
};
int main(){
struct cplx complex1 = make_cplx(94, 44);
struct cplx complex2 = make_cplx(23, -34);
descript(mulCplx(&complex1, &complex2));
return 0;
}
struct cplx make_cplx(double reel, double imaginaire){
struct cplx result;
result.imaginaire = imaginaire;
result.reel = reel;
return result;
}
struct cplx make_cplx2(double rho, double theta){
struct cplx complex;
complex.reel = rho*cos(theta);
complex.imaginaire = rho*sin(theta);
return complex;
}
double moduleCplx(struct cplx *complex){
double result = sqrt((pow(complex->reel, 2) + pow(complex->imaginaire, 2)));
return result;
}
double argumentCplx(struct cplx *complex){
double result;
result = atan(complex->imaginaire / complex->reel);
return result;
}
struct cplx conjugue(struct cplx *complex){
struct cplx ncomp;
ncomp.reel = complex->reel;
ncomp.imaginaire = -complex->imaginaire;
return ncomp;
}
struct cplx divCplx(struct cplx *complex1, struct cplx *complex2){
struct cplx new, cjg1, n1, n2;
cjg1 = conjugue(complex2);
n1 = mulCplx(complex1, &cjg1);
n2 = mulCplx(complex2, &cjg1);
new.reel = n1.reel / n2.reel;
new.imaginaire = n1.imaginaire / n2.reel;
return new;
}
struct cplx mulCplx(struct cplx *complex1, struct cplx *complex2){
struct cplx new;
new.reel = -complex1->imaginaire * complex2->imaginaire + complex1->reel * complex2->reel;
new.imaginaire = complex1->imaginaire * complex2->reel + complex1->reel * complex2->imaginaire;
return new;
}
struct cplx addCplx(struct cplx *complex1, struct cplx *complex2){
struct cplx new;
new.reel = complex1->reel + complex2->reel;
new.imaginaire = complex1->imaginaire + complex2->imaginaire;
return new;
}
struct cplx subCplx(struct cplx *complex1, struct cplx *complex2){
struct cplx new;
new.reel = complex1->reel - complex2->reel;
new.imaginaire = complex1->imaginaire - complex2->imaginaire;
return new;
}
void print_cplx(struct cplx complex){
char sign = '+';
double affI = complex.imaginaire;
if(complex.imaginaire < 0) {
sign = '-';
affI = affI-(affI*2);
}
printf("%f %c i%f\n", complex.reel, sign, affI);
}
void describe(struct cplx complex){
printf("\nComplexe : ");
print_cplx(complex);
printf("\nModule (rho) --> %f\nArgument (Theta) --> %f\n\n", moduleCplx(&complex), argumentCplx(&complex));
printf("Conjugué --> ");
print_cplx(conjugue(&complex));
printf("\n");
}