1 / 11

Travaux dirigés 5

Travaux dirigés 5. Les structures. 2. 3. 1. 1. 4. 5. 6. 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. typedef struct { double x; double y;

mahina
Download Presentation

Travaux dirigés 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. 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); B.Shishedjiev - Informatique II

  5. Algorithme i=1 non i<=n-2 oui j=i+1 non j<=n-1 oui k=j+1 oui non k<=n oui existe triangle i,j,k non Former triangle nt nt=nt+1 k=k+1 j=j+1 i=i+1 fin Faire les Triangles 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(points[i],points[j], points[k]))!=0.0) triangles[nt++] = makeTriangle(points[i],points[j],points[k]); printf("\n"); affTriangles(triangles,nt); } B.Shishedjiev - Informatique II

  6. 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 ; } B.Shishedjiev - Informatique II

  7. 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; } B.Shishedjiev - Informatique II

  8. 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; } } } B.Shishedjiev - Informatique II

  9. 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 *); B.Shishedjiev - Informatique II

  10. 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)); } B.Shishedjiev - Informatique II

  11. 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; } B.Shishedjiev - Informatique II

More Related