La présentation est en train de télécharger. S'il vous plaît, attendez

La présentation est en train de télécharger. S'il vous plaît, attendez

B.Shishedjiev - Informatique II1 Travaux dirigés 5 Les structures.

Présentations similaires


Présentation au sujet: "B.Shishedjiev - Informatique II1 Travaux dirigés 5 Les structures."— Transcription de la présentation:

1 B.Shishedjiev - Informatique II1 Travaux dirigés 5 Les structures

2 B.Shishedjiev - Informatique II2 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. 1 1 3 4 5 6 2 typedef struct { double x; double y; } Point; typedef struct { Point sommets[3]; } Triangle;

3 B.Shishedjiev - Informatique II3 Les traingles 1 3 2

4 B.Shishedjiev - Informatique II4 Les fonctions Point makePoint(double,double); int lirePoint(Point*); Triangle makeTriangle(Point,Point,Point); int lirePoints(Point[], int); double determinant(Triangle); void affTriangle(Triangle); void affTriangles(Triangle[], int);

5 B.Shishedjiev - Informatique II5 Faire les Triangles Algorithme i<=n-2 j=i+1 i=1 j<=n-1 k=j+1 j=j+1 i=i+1 k=k+1 Former triangle nt nt=nt+1 k<=n fin oui non existe triangle i,j,k non oui void main(void) { int np, nt; int i,j,k; Point points[MAXPOINTS]; Triangle triangles[MAXTRIANGLES]; np = lirePoints(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(makeTriangle(poin ts[i],points[j], points[k]))!=0.0) triangles[nt++] = makeTriangle(points[i],points[ j],points[k]); printf("\n"); affTriangles(triangles,nt); }

6 B.Shishedjiev - Informatique II6 Certains fonctions int lirePoint(Point *p){ int res; double x,y; res = scanf("%lf%lf",&x,&y); if (res == 2) *p = makePoint(x,y); return res; } Point makePoint(double x, double y){ Point p; p.x = x; p.y = y; return p; } Triangle makeTriangle(Point p1, Point p2, Point p3){ Triangle t; t.sommets[0] = p1; t.sommets[1] = p2; t.sommets[2] = p3; return t ; }

7 B.Shishedjiev - Informatique II7 Certains fonctions int lirePoints(Point p[], int maxn){ int n=0, res; printf("Tapez lea coordonnes des points en finissant avec EOF\n"); do{ printf("Point no %2d: ",n+1); res = lirePoint(&p[n]); if(res==2) n++; }while (n < maxn && res != EOF); return n; }

8 B.Shishedjiev - Informatique II8 Trier les triangles selon leurs surfaces typedef struct { Point sommets[3]; double surf; } Triangle; void triTriangles(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; }

9 B.Shishedjiev - Informatique II9 Les nombres complexes typedef struct { double re, im; } Complex; Complex makeComplex(double, double); double realPart(Complex); double imagPart(Complex); Complex moinsComplex(Complex); Complex addComplex(Complex, Complex); Complex subComplex(Complex, Complex); Complex mulComplex(Complex, Complex); Complex divComplex(Complex, Complex); double modComplex(Complex); Complex conjComplex(Complex); void affComplex(Complex); int lireComplex(Complex *);

10 B.Shishedjiev - Informatique II10 Les nombres complexes Complex makeComplex(double re, double im) { Complex r; r.re =re; r.im = im; return r; } double realPart(Complex c) { return c.re; } Complex moinsComplex(Complex c) { return makeComplex(-c.re,-c.im); } Complex addComplex(Complex a, Complex b) { return makeComplex(a.re+b.re,a.im+b.im); } Complex subComplex(Complex a, Complex b) { return addComplex(a, moinsComplex(b)); }

11 B.Shishedjiev - Informatique II11 Les nombres complexes Complex divComplex(Complex a, Complex b) { double mod = modComplex(b); return makeComplex((a.re*b.re+a.im*b.im)/mod,(a.im*b.re- a.re*b.im)/mod); } double modComplex(Complex a){ return a.re*a.re +a.im*a.im; } void affComplex(Complex c){ printf("(%7.3lf+%7.3lfi)", c.re,c.im); } int lireComplex(Complex *c){ int res; res = scanf("%lf+%lfi",&c->re,&c->im); return res == 2; }


Télécharger ppt "B.Shishedjiev - Informatique II1 Travaux dirigés 5 Les structures."

Présentations similaires


Annonces Google