You are on page 1of 15

//COMPLEX #include <stdio.h> #include <math.h> #define PI 3.

14 //test struct cplx { float re; float im; }; void encode(struct cplx *); struct cplx add(struct cplx,struct cplx); void affiche(struct cplx); struct cplx soust(struct cplx,struct cplx); float mod(struct cplx); float arg(struct cplx); struct cplx rect(float,float); struct cplx mult(struct cplx,struct cplx); struct cplx div(struct cplx,struct cplx); struct cplx inv(struct cplx); int main() { struct cplx a,b; int choix; char cont,cont2; do { printf("Encodage du 1er complexe\n"); encode(&a); printf("\nEncodage du 2eme comlexe\n"); encode(&b); do { printf("\n1. Addition\n2. Soustraction\n3. Multiplication\n4. Division\n5. Inversion\n"); printf("Votre choix : "); scanf("%d",&choix); switch(choix) { case 1 : printf("La somme vaut "); affiche(add(a,b)); break; case 2 : printf("La difference vaut "); affiche(soust(a,b)); break; case 3 : printf("Le produit vaut "); affiche(mult(a,b)); break; case 4 : printf("Le quotient vaut "); affiche(div(a,b)); break;

case 5

: printf("L'inverse vaut "); affiche(inv(a)); printf(" et "); affiche(inv(b)); break; default : printf("Choix incorrect !"); } do { printf("\nNouvelle operation ? (o/n) "); scanf(" %c",&cont); } while(!(cont=='o' || cont=='n')); } while(cont=='o'); do { printf("Nouvel encodage ? (o/n) "); scanf(" %c",&cont2); } while(!(cont2=='o' || cont2=='n')); } while(cont2=='o'); return 0; } void encode(struct cplx *ac) { int choix; float m,a; do { printf("Format rectangulaire (1) ou polaire (2) ? "); scanf("%d",&choix); } while(!(choix==1 || choix==2)); if(choix==1) { printf("Entrez la partie reelle : "); scanf("%f",&ac->re); printf("Entrez la partie imaginaire : "); scanf("%f",&ac->im); } else { printf("Entrez le module : "); scanf("%f",&m); printf("Entrez l'argument (en degre) : "); scanf("%f",&a); *ac=rect(m,PI/180*a); } } struct cplx add(struct cplx x,struct cplx y) { struct cplx z; z.re=x.re+y.re; z.im=x.im+y.im;

return z; } void affiche(struct cplx x) { if(x.re>=0) printf("%.2f ",x.re); else printf("- %.2f ",-x.re); if(x.im>=0) printf("+ %.2f i",x.im); else printf("- %.2f i",-x.im); printf(" ou %.2f e(%.2f i)",mod(x),180/PI*arg(x)); } struct cplx soust(struct cplx x,struct cplx y) { struct cplx z; z.re=x.re-y.re; z.im=x.im-y.im; return z; } float mod(struct cplx x) { return sqrtf(x.re*x.re+x.im*x.im); } float arg(struct cplx x) { if(x.re>=0) return atan(x.im/x.re); else return atan(x.im/x.re)+PI; } struct cplx rect(float m,float a) { struct cplx x; x.re=m*cos(a); x.im=m*sin(a); return x; } struct cplx mult(struct cplx x,struct cplx y) { float m,a; m=mod(x)*mod(y); a=arg(x)+arg(y); return rect(m,a); } struct cplx div(struct cplx x,struct cplx y)

{ float m,a; m=mod(x)/mod(y); a=arg(x)-arg(y); return rect(m,a); } struct cplx inv(struct cplx x) { float m,a; m=1/mod(x); a=-arg(x); return rect(m,a); }

// TRI A BULLE #include <stdio.h> #define TMAX 50 main() { void encode(int*,int*); void tri_bul(int*,int); void tri_sim(int*,int); void tri_sel(int*,int); void tri_ins(int*,int); void affiche(int*,int); int taille,choix; int tab[TMAX]; printf("Encodage du tableau :\n"); encode(tab,&taille); printf("\nVoici les differents tris possibles:\n1. Tri a bulles \n2. Tri simple\n3. Tri par selection\n4. Tri par insertion\n"); printf("\nVotre choix : "); scanf(" %d",&choix); switch (choix) { case 1: tri_bul(tab,taille); break; case 2: tri_sim(tab,taille); break; case 3: tri_sel(tab,taille); break; case 4: tri_ins(tab,taille); break;

default: printf("le choix n est pas valide"); } affiche(tab,taille); return 0; } void encode (int *adtab, int *adtaille) { printf("\nEncodez taille du tableau (maximum %d valeurs):",TMAX); scanf("%d",adtaille); int i; for (i=0;i<*adtaille;i++) { printf("encodez la valeur numero %d : ",i+1); scanf("%d",adtab+i); } } void tri_bul (int *adtab, int taille) { int i,temp,echange; do { echange=0; for (i=0;i<taille-1;i++) if (*(adtab+i+1)<*(adtab+i)) { temp=*(adtab+i+1); *(adtab+i+1)=*(adtab+i); *(adtab+i)=temp; echange=1; } }while (echange==1); } void tri_sim(int *adtab, int taille) { int i,j,temp; for (i=0;i<taille-1;i++) for (j=i+1;j<taille;j++) { if (*(adtab+j)<*(adtab+i)) { temp=*(adtab+j); *(adtab+j)=*(adtab+i); *(adtab+i)=temp; } } } void tri_sel(int *adtab, int taille) {

int i,j,pos_min,temp; for (i=0;i<taille-1;i++) { pos_min=i; for (j=i+1;j<taille;j++) { if ((*(adtab+j))<(*(adtab+i))) { pos_min=j; } } if (pos_min!=i) { temp=*(adtab+i); *(adtab+i)=*(adtab+pos_min); *(adtab+pos_min)=temp; } } } void tri_ins(int *adtab,int taille) { int i,j,temp; for(i=1;i<=taille-1;i++) { temp=*(adtab+i); j=i-1; while(*(adtab+j)>temp && j>=0) { *(adtab+j+1)=*(adtab+j); j=j-1; printf("test ok"); } *(adtab+j+1)=temp; } } void affiche (int *adtab, int taille) { printf("\nLes valeurs sont desormais triees dans leur ordre croissant.\n"); int i; for (i=0;i<taille;i++) printf("%d\n",*(adtab+i)); } // TAB #include <stdio.h> #include <math.h> #define tmax 20 main()

{ int taille,i,choix, min(int[],int,int), max(int[],int,int),tab[tmax]; float moy(int[],int,int),ec_type(int[],int,int); char reponse='o'; printf("quelle est la taille du tableau (max %d)?\n",tmax); scanf("%d",&taille); for (i=0;i<=taille-1;i++) { printf("valeur entiere de l'element %d : ",i+1); scanf("%d",&tab[i]); } while (reponse=='o') {printf("Valeur minimale, pressez 1\nValeur maximale, pressez 2\nMoyenne,pressez 3\nEcart-type, pressez 4\n"); scanf("%d",&choix); switch (choix) { case 1: min(tab,taille,i); break; case 2: max(tab,taille,i); break; case 3: moy(tab,taille,i); break; case 4: ec_type(tab,taille,i); break; } printf("souhaitez vous encore faire une opration oui ou non?\n"); scanf (" %c",&reponse);} } int min(int tab[],int taille, int i) { int j,temp; for (i=0;i<=taille-2;i++) for (j=i+1;j<=taille-1;j++) {if (tab[j]<tab[i]) { temp=tab[i]; tab[i]=tab[j]; tab[j]=temp;}} printf("la valeur minimale est : %d\n",tab[0]); } int max(int tab[], int taille, int i) { int j,temp; for (i=0;i<=taille-2;i++) for (j=i+1;j<=taille-1;j++) {if (tab[j]<tab[i]) { temp=tab[i];

tab[i]=tab[j]; tab[j]=temp;}} printf("la valeur maximae est : %d\n",tab[taille-1]); } float moy(int tab[],int taille, int i) { float somme=0; for (i=0;i<=taille-1;i++) somme=somme+tab[i]; printf("la valeur moyenne est : %f\n",somme/taille); } float ec_type(int tab[],int taille,int i) { float somme=0,moyenne,variance; for (i=0;i<=taille-1;i++) somme=somme+tab[i]; moyenne=somme/taille; for(i=0;i<=taille-1;i++) variance= variance + (tab[i]-moyenne)* (tab[i]-moyenne); printf("l ecart type est: %f\n", sqrt(variance)); } //VECT #include <stdio.h> struct vecteur { float x; float y; float z; }; int main() { int choix; char cont; struct vecteur v1,v2; void encode(struct vecteur *); void affiche(struct vecteur); struct vecteur add(struct vecteur,struct vecteur); struct vecteur soust(struct vecteur,struct vecteur); float pscal(struct vecteur,struct vecteur); struct vecteur pvect(struct vecteur,struct vecteur); printf("encodage du 1er vecteur:\n"); encode(&v1); printf("\nencodage du 2eme vecteur:\n"); encode(&v2); do { printf("\n1.addition\n2.soustraction\n3.produit scalaire \n4.produit vectoriel"); printf("\nvotre choix : ");

scanf ("%d",&choix); switch(choix) { case 1 : printf("\nL'addition de ces 2 vecteurs vaut: "); affiche(add(v1,v2)); break; case 2: printf("\nLa soustraction de ces 2 vecteurs vaut: "); affiche(soust(v1,v2)); break; case 3: printf("\nLe produit scalaire de ces 2 vecteurs vaut: %f ",pscal(v1,v2)); break; case 4: printf("\nLe produit vectoriel de ces 2 vecteurs vaut: "); affiche(pvect(v1,v2)); break; default : printf("\nentree incorrecte"); } printf("\n\neffectuer une autre operation (o/n): "); scanf(" %c",&cont); printf("\n"); }while(cont=='o'); } void encode(struct vecteur *advec) { printf("composante en x: "); scanf(" %f",&advec->x); printf("composante en y: "); scanf(" %f",&advec->y); printf("composante en z: "); scanf(" %f",&advec->z); } void affiche(struct vecteur v3) { if (v3.x>=0) printf("%.3f i ",v3.x); else printf("- %.3f i ",-v3.x); if (v3.y>=0) printf("+ %.3f j ",v3.y); else printf("- %.3f j ",-v3.y); if (v3.z>=0) printf("+ %.3f k ",v3.z); else printf("- %.3f k ",-v3.z);

} struct vecteur add(struct vecteur v1, struct vecteur v2) { struct vecteur v3; v3.x=v1.x+v2.x; v3.y=v1.y+v2.y; v3.z=v1.z+v2.z; return v3; } struct vecteur soust(struct vecteur v1, struct vecteur v2) { struct vecteur v3; v3.x=v1.x-v2.x; v3.y=v1.y-v2.y; v3.z=v1.z-v2.z; return v3; } float pscal(struct vecteur v1, struct vecteur v2) { float v3; v3=v1.x*v2.x+v1.y*v2.y+v1.z*v2.z; return v3; } struct vecteur pvect(struct vecteur v1, struct vecteur v2) { struct vecteur v3; v3.x=(v1.y*v2.z)-(v1.z*v2.y); v3.y=(v1.z*v2.x)-(v1.x*v2.z); v3.z=(v1.x*v2.y)-(v1.y*v2.x); return v3; } //MULT MAT #include <stdio.h> #define TMAX 10 int main() { float matA[TMAX][TMAX]; float matB[TMAX][TMAX]; float matC[TMAX][TMAX]; int nla, nca, nlb, ncb, nlc, ncc; void encode(float *, int *, int *); void multiplication(float *,float *,float *, int, int, int, int, int *, int *); void affiche(float *, int, int); printf("encodage de la 1ere matrice:\n"); encode(&matA[0][0], &nla, &nca); printf("\nencodage de la 2eme matrice:\n");

encode(&matB[0][0], &nlb, &ncb); printf("\n"); multiplication(&matA[0][0],&matB[0][0],&matC[0][0], nla, nca, nlb, ncb,&nlc,&ncc); affiche( &matC[0][0], nlc, ncc); return 0; } void encode (float *admat, int *lig, int*col) { printf("nombre de lignes : "); scanf (" %d",lig); printf("nombre de colonnes: "); scanf(" %d",col); int i,j; float temp; for (i=0;i<*lig;i++) { for (j=0;j<*col;j++) { printf("entrez l element %d %d :", i+1 ,j+1); scanf (" %f",&temp); *(admat+i*TMAX+j)=temp; } } } void multiplication (float *admatA, float *admatB, float *admatC, int nla,int nca,int nlb,int ncb, int *adnlc, int *adncc) { *adnlc=nla; *adncc=ncb; int i,j,k; for (i=0;i<nla;i++) { for (j=0;j<ncb;j++) { *(admatC+i*TMAX+j)=0; for (k=0;k<nca;k++) { *(admatC+i*TMAX+j)+=((*(admatA+i*TMAX+k))*(*(admatB +TMAX*k+j))); } } } } void affiche(float *admatC, int nlc, int ncc) { int i,j; for (i=0;i<nlc;i++) { for (j=0;j<ncc;j++)

{ printf("%5f } printf("\n"); } } // MATRICE #include <stdio.h> #define TMAX 20 struct matrice { int nbl; int nbc; float tab[TMAX][TMAX]; }; main() { void encode(struct matrice *); struct matrice addition(struct matrice,struct matrice); struct matrice soustraction(struct matrice,struct matrice); struct matrice transposition(struct matrice); void affiche(struct matrice); struct matrice mat1; struct matrice mat2; int choix; printf("1. Addition\n2. Soustraction\n3. Transposition\n"); printf("Votre choix : "); scanf ("%d",&choix); switch(choix) { case 1: { printf("\nEncodage de la premiere matrice:\n"); encode(&mat1); printf("\nEncodage de la deuxieme matrice:\n"); encode(&mat2); printf("\nle resultat de cette addition est :\n"); affiche(addition(mat1,mat2)); break; } case 2: { printf("\nEncodage de la premiere matrice:\n"); encode(&mat1); printf("\nEncodage de la deuxieme matrice:\n"); encode(&mat2); printf("\nle resultat de cette soustraction est :\n"); affiche(soustraction(mat1,mat2)); break; ",*(admatC+i*TMAX+j));

} case 3: { encode(&mat1); printf("\nle resultat de cette transposition est: \n"); affiche(transposition(mat1)); break; } default: printf("ce chiffre ne correspond pas a un des choix proposes"); } return 0; } void encode (struct matrice *mat) { printf("Veuiller entrer le nombre de lignes (max:%d) :", TMAX); scanf("%d",&mat->nbl); printf("Veuiller entrer le nombre de colonnes (max:%d) :", TMAX); scanf("%d",&mat->nbc); int i,j; for(i=0;i<mat->nbl;i++) for(j=0;j<mat->nbc;j++) { printf("Veuillez entrer la valeur de l'element %d %d : ",i +1,j+1); scanf("%f",&mat->tab[i][j]); } printf("\n"); } struct matrice addition(struct matrice mat1, struct matrice mat2) { if (!((mat1.nbl==mat2.nbl) && (mat1.nbc==mat2.nbc))) printf("l'addition de matrices n est possible que pour des matrices de meme taille!\n"); else { struct matrice mat3; mat3.nbl=mat1.nbl; mat3.nbc=mat1.nbc; int i,j; for(i=0;i<mat1.nbl;i++) for(j=0;j<mat1.nbc;j++) { mat3.tab[i][j]=mat1.tab[i][j]+mat2.tab[i][j]; } return mat3; } } struct matrice soustraction(struct matrice mat1, struct matrice

mat2) { if (!((mat1.nbl==mat2.nbl) && (mat1.nbc==mat2.nbc))) printf("la soustraction de matrices n est possible que pour des matrices de meme taille!\n"); else { struct matrice mat3; mat3.nbl=mat1.nbl; mat3.nbc=mat1.nbc; int i,j; for(i=0;i<mat1.nbl;i++) for(j=0;j<mat1.nbc;j++) { mat3.tab[i][j]=mat1.tab[i][j]-mat2.tab[i][j]; } return mat3; } } struct matrice transposition(struct matrice mat1) { int i,j; struct matrice mat3; mat3.nbl=mat1.nbc; mat3.nbc=mat1.nbl; for(i=0;i<mat1.nbl;i++) for(j=0;j<mat1.nbc;j++) mat3.tab[j][i]=mat1.tab[i][j]; return mat3; } void affiche(struct matrice mat3) { int i,j; for(i=0;i<mat3.nbl;i++) { for(j=0;j<mat3.nbc;j++) { printf(" %.3f ",mat3.tab[i][j]); } printf("\n"); } } // triangle #include <stdio.h> #include <math.h> struct triangle { float tabx[3]; float taby[3]; };

main() { struct triangle pts; void encode(struct triangle *); float surface(struct triangle); encode(&pts); printf("La surface de ce triangle vaut : %f",surface(pts)); return 0; } void encode (struct triangle *adpts) { int i; for(i=0;i<3;i++) { printf("Veuillez entrer la valeur de x du point %d : ",i+1); scanf("%f",&adpts->tabx[i]); printf("Veuillez entrer la valeur de y du point %d : ",i+1); scanf("%f",&adpts->taby[i]); printf("\n"); } } float surface (struct triangle pts) { float a,b,c,d,h; d=sqrtf((pts.tabx[1]-pts.tabx[0])*(pts.tabx[1]-pts.tabx[0])+ (pts.taby[1]-pts.taby[0])*(pts.taby[1]-pts.taby[0])); a=((pts.taby[1]-pts.taby[0])/(pts.tabx[1]-pts.tabx[0])); b=-1; c=pts.taby[0]-a*pts.tabx[0]; h=fabs(a*pts.tabx[2]+b*pts.taby[2]+c)/sqrtf(a*a+b*b); return ((d*h)/2); }

You might also like