Travaux dirigs 5 Les structures B Shishedjiev Informatique
Travaux dirigés 5 Les structures B. Shishedjiev - Informatique II 1
Un peu de géométrie • Faire un programme qui lit un ensemble de points dans la plaine X, Y et construit tous les triangles possibles aux lesquels les points peuvent être de sommets. 2 3 1 1 4 5 typedef struct { double x; double y; } Point; typedef struct { Point sommets[3]; } Triangle; 6 B. Shishedjiev - Informatique II 2
Les traingles 2 1 B. Shishedjiev - Informatique II 3 3
Les fonctions Point make. Point(double, double); int lire. Point(Point*); Triangle make. Triangle(Point, Point); int lire. Points(Point[], int); double determinant(Triangle); void aff. Triangles(Triangle[], int); B. Shishedjiev - Informatique II 4
Faire les Triangles void main(void) { int np, nt; int i, j, k; Point points[MAXPOINTS]; Triangle triangles[MAXTRIANGLES]; np = lire. Points(points, MAXPOINTS); nt=0; for (i=0; i<np-2; i++) for(j=i+1; j <np-1; j++) for (k=j+1; k<np; k++) if (determinant(make. Triangle(poin ts[i], points[j], points[k]))!=0. 0) triangles[nt++] = make. Triangle(points[i], points[ j], points[k]); printf("n"); aff. Triangles(triangles, nt); B. Shishedjiev - Informatique II } Algorithme i=1 i<=n-2 non oui j=i+1 j<=n-1 non oui k=j+1 oui k<=n non oui existe triangle i, j, k non Former triangle nt nt=nt+1 k=k+1 j=j+1 i=i+1 fin 5
Certains fonctions int lire. Point(Point *p){ int res; double x, y; res = scanf("%lf%lf", &x, &y); if (res == 2) *p = make. Point(x, y); return res; } Point make. Point(double x, double y){ Point p; p. x = x; p. y = y; return p; } Triangle make. Triangle(Point p 1, Point p 2, Point p 3){ Triangle t; t. sommets[0] = p 1; t. sommets[1] = p 2; t. sommets[2] = p 3; return t ; } B. Shishedjiev - Informatique II 6
Certains fonctions int lire. Points(Point p[], int maxn){ int n=0, res; printf("Tapez lea coordonnes des points en finissant avec EOFn"); do{ printf("Point no %2 d: ", n+1); res = lire. Point(&p[n]); if(res==2) n++; }while (n < maxn && res != EOF); return n; } B. Shishedjiev - Informatique II 7
Trier les triangles selon leurs surfaces typedef struct { Point sommets[3]; double surf; } Triangle; void tri. Triangles(Triangle t[], int n ) { Triangle temp; int i, j, m; for (j = n; j>1; j--){ m=0; for (i=1; i<j; i++) if (t[m]. surf < t[i]. surf) m=i; if (m !=j-1) { temp = t[m]; t[m] = t[j-1]; t[j-1] = temp; } } } B. Shishedjiev - Informatique II 8
Les nombres complexes typedef struct { double re, im; } Complex; Complex make. Complex(double, double); double real. Part(Complex); double imag. Part(Complex); Complex moins. Complex(Complex); Complex add. Complex(Complex, Complex); Complex sub. Complex(Complex, Complex); Complex mul. Complex(Complex, Complex); Complex div. Complex(Complex, Complex); double mod. Complex(Complex); Complex conj. Complex(Complex); void aff. Complex(Complex); int lire. Complex(Complex *); B. Shishedjiev - Informatique II 9
Les nombres complexes Complex make. Complex(double re, double im) { Complex r; r. re =re; r. im = im; return r; } double real. Part(Complex c) { return c. re; } Complex moins. Complex(Complex c) { return make. Complex(-c. re, -c. im); } Complex add. Complex(Complex a, Complex b) { return make. Complex(a. re+b. re, a. im+b. im); } Complex sub. Complex(Complex a, Complex b) { return add. Complex(a, moins. Complex(b)); } B. Shishedjiev - Informatique II 10
Les nombres complexes Complex div. Complex(Complex a, Complex b) { double mod = mod. Complex(b); return make. Complex((a. re*b. re+a. im*b. im)/mod, (a. im*b. rea. re*b. im)/mod); } double mod. Complex(Complex a){ return a. re*a. re +a. im*a. im; } void aff. Complex(Complex c){ printf("(%7. 3 lf+%7. 3 lfi)", c. re, c. im); } int lire. Complex(Complex *c){ int res; res = scanf("%lf+%lfi", &c->re, &c->im); return res == 2; } B. Shishedjiev - Informatique II 11
- Slides: 11