Commit 5eb7fe63faf562011ce030645e14bcd3f7797e21

Authored by Vincent Benoist
0 parents

TP de C

Tp1 0 → 100755
No preview for this file type
Tp1.c 0 → 100644
  1 +++ a/Tp1.c
... ... @@ -0,0 +1,42 @@
  1 +#include <stdio.h>
  2 +
  3 +void affichage(float x, float y, float z){
  4 + printf("%f %f %f\n", x, y ,z);
  5 +}
  6 +
  7 +void lecture( float *a, float *b, float *c){
  8 + scanf("%f%f%f",a,b,c);
  9 +}
  10 +
  11 +void permuter (float* a, float* b){
  12 + float tmp;
  13 + tmp = *a;
  14 + *a = *b;
  15 + *b = tmp;
  16 +}
  17 +
  18 +void minMax2(float* a, float* b){
  19 + if( *b < *a){
  20 + permuter(a,b);
  21 + }
  22 +}
  23 +
  24 +void tri (float* a, float*b, float*c){
  25 + minMax2(a,b);
  26 + minMax2(b,c);
  27 + minMax2(a,b);
  28 +
  29 +}
  30 +
  31 +
  32 +
  33 +int main(){
  34 + float a,b,c;
  35 + lecture(&a,&b,&c);
  36 + affichage(a,b,c);
  37 +
  38 + tri(&a,&b,&c);
  39 + affichage(a,b,c);
  40 +
  41 + return 0;
  42 +}
... ...
Tp1_1 0 → 100755
No preview for this file type
Tp1_1.c 0 → 100644
  1 +++ a/Tp1_1.c
... ... @@ -0,0 +1,58 @@
  1 +#include <stdio.h>
  2 +
  3 +
  4 +int chiffre(char ch){
  5 + if ((ch >= '0') && (ch <= '9')){
  6 + return 1;
  7 + }else{
  8 + return 0;
  9 + }
  10 +}
  11 +
  12 +int minuscule(char ch){
  13 + if ((ch >= 'a') && (ch <= 'z')){
  14 + return 1;
  15 + }else{
  16 + return 0;
  17 + }
  18 +}
  19 +
  20 +
  21 +int majuscule(char ch){
  22 + if ((ch >= 'A') && (ch <= 'Z')){
  23 + return 1;
  24 + }else{
  25 + return 0;
  26 + }
  27 +}
  28 +
  29 +
  30 +void analysePhrase(int* nbChar, int* nbChiffre, int* nbMin, int * nbMaj){
  31 +
  32 + char c;
  33 + scanf("%c",&c);
  34 +
  35 +
  36 + while(c!='.'){
  37 + if(chiffre(c)==1){
  38 + *nbChiffre = *nbChiffre+1;
  39 + }else if(minuscule(c)==1){
  40 + *nbMin=*nbMin+1;
  41 + }else if(majuscule(c)==1){
  42 + *nbMaj=*nbMaj+1;
  43 + }
  44 + scanf("%c",&c);
  45 + *nbChar=*nbChar+1;
  46 + }
  47 +
  48 +}
  49 +
  50 +
  51 +
  52 +int main(){
  53 + int nbChar=0,nbChiffre=0,nbMin=0, nbMaj =0;
  54 + printf("Char: %d Chiffre: %d nbMin: %d nbMaj: %d\n",nbChar,nbChiffre,nbMin, nbMaj);
  55 + analysePhrase(&nbChar,&nbChiffre,&nbMin,&nbMaj);
  56 + printf("Char: %d Chiffre: %d nbMin: %d nbMaj: %d\n",nbChar,nbChiffre,nbMin, nbMaj);
  57 + return 0;
  58 +}
... ...
Tp1_2 0 → 100755
No preview for this file type
Tp1_2.c 0 → 100644
  1 +++ a/Tp1_2.c
... ... @@ -0,0 +1,61 @@
  1 +#include <stdio.h>
  2 +
  3 +void lireDate(int* jour, int* mois, int* annee){
  4 + int i=0;
  5 + scanf("%d",&i);
  6 + while(i < 1 || i > 32){
  7 + scanf("%d",&i);
  8 + }
  9 + *jour = i;
  10 + scanf("%d",&i);
  11 + while(i < 1 || i > 12){
  12 + scanf("%d",&i);
  13 + }
  14 + *mois = i;
  15 + scanf("%d",&i); //do while
  16 + while(i < 0000 || i > 9999){
  17 + scanf("%d",&i);
  18 + }
  19 + *annee = i;
  20 +}
  21 +
  22 +void lire2Dates(int* jour1,int* jour2, int* mois1, int* mois2, int* annee1,int* annee2){
  23 + lireDate(jour1,mois1,annee1);
  24 + lireDate(jour2,mois2,annee2);
  25 +}
  26 +
  27 +int compareDate (int jour1,int jour2, int mois1, int mois2, int annee1,int annee2){
  28 + if(annee1 > annee2){
  29 + return 1;
  30 + }else if (annee1 < annee2){
  31 + return -1;
  32 + }else if (mois1 > mois2){
  33 + return 1;
  34 + }else if (mois1 < mois2){
  35 + return -1;
  36 + }else if (jour1 > jour2){
  37 + return 1;
  38 + }else if (jour1 < jour2){
  39 + return -1;
  40 + }else {
  41 + return 0;
  42 + }
  43 +}
  44 +
  45 +int main(){
  46 + int jour=0,mois=0,annee=0;
  47 + int jour2=0,mois2=0,annee2=0;
  48 + int valCmp;
  49 + lire2Dates(&jour, &jour2, &mois, &mois2, &annee, &annee2);
  50 + printf("Date 1 Jour: %d Mois: %d Annee: %d \n",jour, mois, annee);
  51 + printf("Date 2 Jour: %d Mois: %d Annee: %d \n",jour2, mois2, annee2);
  52 + valCmp = compareDate(jour, jour2, mois, mois2, annee, annee2);
  53 + if (valCmp == 1){
  54 + printf("Date1 > Date2\n");
  55 + }else if(valCmp == -1){
  56 + printf("Date2 > Date1\n");
  57 + }else{
  58 + printf("Date égales\n");
  59 + }
  60 + return 0;
  61 +}
... ...
Tp1_3 0 → 100755
No preview for this file type
Tp1_3.c 0 → 100644
  1 +++ a/Tp1_3.c
... ... @@ -0,0 +1,76 @@
  1 +#include <stdio.h>
  2 +
  3 +float somme(float v1, float v2){
  4 + return (v1+v2);
  5 +}
  6 +
  7 +void minMax(float *min, float* max, float a, float b, float c){
  8 + if( a>b && a>c){
  9 + *max = a;
  10 + }else if (b>a && b>c){
  11 + *max = b;
  12 + }else {
  13 + *max =c;
  14 + }
  15 +
  16 + if( a<b && a<c){
  17 + *min = a;
  18 + }else if (b<a && b<c){
  19 + *min = b;
  20 + }else {
  21 + *min =c;
  22 + }
  23 +
  24 +}
  25 +
  26 +void permuter (float* a, float* b){
  27 + float tmp;
  28 + tmp = *a;
  29 + *a = *b;
  30 + *b = tmp;
  31 +}
  32 +
  33 +void minMax2v(float* v1,float* v2){
  34 + if(*v2>*v1){
  35 + permuter(v1,v2);
  36 + }
  37 +}
  38 +
  39 +void traitementSuite( float* min, float* max, float* moyenne, int* existe){
  40 + float tmp=0;
  41 + float res=0;
  42 + int i=0;
  43 + scanf("%f",&tmp);
  44 +
  45 + if(tmp==0){
  46 + *existe =0;
  47 + }else{
  48 +
  49 + *min = *max = tmp;
  50 + res = res + tmp;
  51 + i=1;
  52 + scanf("%f",&tmp);
  53 + res = res + tmp;
  54 + minMax2v(min,&tmp);
  55 + minMax2v(&tmp,max);
  56 +
  57 + while(tmp!=0){
  58 + i++;
  59 + scanf("%f",&tmp);
  60 + res = res + tmp;
  61 + minMax(min,max,*min, tmp,*max);
  62 + }
  63 +
  64 + *moyenne= (res/i);
  65 + *existe=1;
  66 + }
  67 + //printf("somme %f i: %d",res,i);
  68 +}
  69 +
  70 +int main(){
  71 + float min,max,moyenne;
  72 + int existe;
  73 + traitementSuite(&min,&max,&moyenne,&existe);
  74 + printf("min: %f max: %f moyenne: %f, existe: %d",min,max,moyenne,existe);
  75 + return 0;
  76 +}
... ...
Tp1_soundex 0 → 100755
No preview for this file type
Tp1_soundex.c 0 → 100644
  1 +++ a/Tp1_soundex.c
... ... @@ -0,0 +1,50 @@
  1 +#include <stdio.h>
  2 +
  3 +int minuscule(char ch){
  4 + if ((ch >= 'a') && (ch <= 'z')){
  5 + return 1;
  6 + }else{
  7 + return 0;
  8 + }
  9 +}
  10 +
  11 +
  12 +int majuscule(char ch){
  13 + if ((ch >= 'A') && (ch <= 'Z')){
  14 + return 1;
  15 + }else{
  16 + return 0;
  17 + }
  18 +}
  19 +
  20 +int estVoyelle(char ch){
  21 + if(ch == 'a' || ch=='e' || ch=='i'|| ch=='o' || ch=='u' || ch=='y' ||ch== 'A' || ch=='E' ||ch== 'I' ||ch== 'O' || ch=='U' || ch=='Y'){
  22 + return 1;
  23 + }
  24 + return 0;
  25 +}
  26 +
  27 +void soundexcode(){
  28 + char ch;
  29 + char savech;
  30 + int nblettre = 4;
  31 + scanf("%c",&ch);
  32 + savech=ch;
  33 + printf("==> %c",ch);
  34 +
  35 + while(nblettre!=1 && (minuscule(ch) || majuscule(ch))){
  36 + scanf("%c",&ch);
  37 + if (ch != savech && !estVoyelle(ch)){
  38 + savech=ch;
  39 + printf("%c",ch);
  40 +
  41 + nblettre--;
  42 + }
  43 + savech=ch;
  44 + }
  45 +
  46 +}
  47 +
  48 +int main(){
  49 + soundexcode();
  50 +}
... ...
Tp2/Tp2_Kaprekar 0 → 100755
No preview for this file type
Tp2/Tp2_Kaprekar.c 0 → 100644
  1 +++ a/Tp2/Tp2_Kaprekar.c
... ... @@ -0,0 +1,95 @@
  1 +#include <stdio.h>
  2 +
  3 +void decomp3(int val, int* centaine, int* dizaine,int* unite){
  4 + *centaine = val/100;
  5 + val = val - ((*centaine)*100);
  6 + *dizaine = val/10;
  7 + val = val - ((*dizaine)*10);
  8 + *unite = val;
  9 +}
  10 +
  11 +int recomp3( int centaine, int dizaine,int unite){
  12 + return centaine*100+dizaine*10+unite;
  13 +}
  14 +
  15 +void permuter (int* a, int* b){
  16 + int tmp;
  17 + tmp = *a;
  18 + *a = *b;
  19 + *b = tmp;
  20 +}
  21 +
  22 +void MaxMin(int* v1,int* v2){
  23 + if(*v2>*v1){
  24 + permuter(v1,v2);
  25 + }
  26 +}
  27 +
  28 +void ordonne3 (int*a, int*b, int*c){
  29 + MaxMin(a,b);
  30 + MaxMin(b,c);
  31 + MaxMin(a,b);
  32 +}
  33 +
  34 +void ordonne3croissant (int*a, int*b, int*c){
  35 + MaxMin(a,b);
  36 + MaxMin(b,c);
  37 + MaxMin(a,b);
  38 + permuter(a,c);
  39 +}
  40 +
  41 +int calcNbr(int val){
  42 +
  43 + int centaine, dizaine, unite;
  44 + int N1, N2, NBR;
  45 +
  46 + decomp3(val,&centaine,&dizaine,&unite);
  47 +
  48 + ordonne3(&centaine,&dizaine,&unite);
  49 + N1=recomp3(centaine, dizaine, unite);
  50 +
  51 + ordonne3croissant(&centaine,&dizaine,&unite);
  52 + N2=recomp3(centaine, dizaine, unite);
  53 +
  54 + NBR=N1-N2;
  55 + return NBR;
  56 +}
  57 +
  58 +
  59 +int convergence(int val){
  60 + int valSave=val;
  61 + int NBR;
  62 + NBR = calcNbr(val);
  63 +
  64 + while(valSave != NBR){
  65 + valSave = NBR;
  66 + NBR = calcNbr(NBR);
  67 +
  68 +
  69 + //printf("%d\n",NBR);
  70 + }
  71 + return NBR;
  72 +}
  73 +
  74 +
  75 +int main(){
  76 + int val,NBR;
  77 + scanf("%d",&val);
  78 + NBR = convergence(val);
  79 + printf("Nombre: %d Convergence: %d \n",val, NBR);
  80 +
  81 + /*int N1, N2, NBR;
  82 + decomp3(val,&centaine,&dizaine,&unite);
  83 +
  84 + ordonne3(&centaine,&dizaine,&unite);
  85 + N1=recomp3(centaine, dizaine, unite);
  86 +
  87 + ordonne3croissant(&centaine,&dizaine,&unite);
  88 + N2=recomp3(centaine, dizaine, unite);
  89 +
  90 + NBR=N1-N2;
  91 +
  92 + //printf("Valeur initiale: %d \n N1: %d \n N2: %d \n NBR: %d\n",val,N1,N2,NBR);
  93 + */
  94 + return 0;
  95 +}
... ...
Tp3/Tp3_JeuNbSecret 0 → 100755
No preview for this file type
Tp3/Tp3_JeuNbSecret.c 0 → 100644
  1 +++ a/Tp3/Tp3_JeuNbSecret.c
... ... @@ -0,0 +1,116 @@
  1 +#include <stdio.h>
  2 +
  3 +float somme(float v1, float v2){
  4 + return v1+v2;
  5 +}
  6 +
  7 +float soustraction(float v1, float v2){
  8 + return v1-v2;
  9 +}
  10 +
  11 +float produit(float v1, float v2){
  12 + return v1*v2;
  13 +}
  14 +
  15 +float division(float v1, float v2){
  16 + if(v2 != 0){
  17 + return v1/v2;
  18 + }
  19 + return -404;
  20 +}
  21 +
  22 +int chiffre(char ch){
  23 + if ((ch >= '0') && (ch <= '9')){
  24 + return 1;
  25 + }else{
  26 + return 0;
  27 + }
  28 +}
  29 +
  30 + int estOperateur(char c){
  31 + if( c == '+' || c == '-' || c == '*' ){
  32 + return 1;
  33 + }
  34 + return 0;
  35 +}
  36 +
  37 +
  38 +void lireCar( char * car){
  39 + *car = ' ';
  40 + while( *car == ' ' || *car == '\n'){
  41 + scanf("%c",car);
  42 + }
  43 +}
  44 +
  45 +
  46 +int estEgal(char c){
  47 + if( c == '='){
  48 + return 1;
  49 + }
  50 + return 0;
  51 +}
  52 +
  53 +
  54 +
  55 +
  56 +float faireCalc ( float a, char signe, float b){
  57 + if(signe == '*'){
  58 + return produit(a,b);
  59 + } else if(signe == '-'){
  60 + return soustraction(a,b);
  61 + } else if(signe == '+'){
  62 + return somme(a,b);
  63 + }
  64 + return -1;
  65 +
  66 +}
  67 +
  68 +int main(){
  69 + char c, signe, v2;
  70 + float f1=0, f2=0;
  71 + int cpt = 0;
  72 + lireCar(&c);
  73 + while(chiffre(c)!=1){
  74 + lireCar(&c);
  75 + }
  76 + f1 = c - '0';
  77 + printf("%f\n\n",f1);
  78 +
  79 + lireCar(&c);
  80 + while(estEgal(c)!=1){
  81 + cpt++;
  82 + if(estOperateur(c)){
  83 + signe = c;
  84 + } else if (chiffre(c)){
  85 + v2 = c;
  86 + f2 = v2 - '0';
  87 + }
  88 + if( cpt % 2 == 1 && cpt !=1){
  89 + f1 = faireCalc(f1,signe,f2);
  90 +
  91 + printf("f1:%f signe: %c f2: %f = %f\n",f1,signe,f2, faireCalc(f1,signe,f2));
  92 +
  93 + }
  94 +
  95 +
  96 + lireCar (&c);
  97 +
  98 +
  99 + /*
  100 + while(estOperateur(signe)!=1 ){
  101 + lireCar(&signe);
  102 + }
  103 + lireCar(&v2);
  104 + while(chiffre(v2)!=1){
  105 + lireCar(&v2);
  106 + }
  107 + f2 = v2 - '0';
  108 +
  109 + f1 = faireCalc(f1,signe,f2);
  110 + printf("%f %c %f = %f",f1,signe,f2, faireCalc(f1,signe,f2));*/
  111 + }
  112 +
  113 +
  114 + return 0;
  115 +
  116 +}
... ...
Tp4/Tp4_1 0 → 100755
No preview for this file type
Tp4/tp4.c 0 → 100644
  1 +++ a/Tp4/tp4.c
... ... @@ -0,0 +1,125 @@
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +
  4 +#define N 4
  5 +
  6 +void printVector (float vect[], int n){
  7 + int i ;
  8 + for(i=0; i <= n-1; i++){
  9 + printf("V[%d]:%f ",i, vect[i]);
  10 +
  11 + }
  12 + printf("\n\n");
  13 +}
  14 +
  15 +void setVector (float vect[], int n){
  16 + int i ;
  17 + scanf("%f", &vect[0]);
  18 +
  19 + for(i=1; i <= n-1; i++){
  20 + scanf("%f", &vect[i]);
  21 + while (vect[i] < vect[i-1]){
  22 + printf("001- Suite non croissante \nVecteurActuel:");
  23 + printVector(vect, i);
  24 +
  25 + scanf("%f", &vect[i]);
  26 + }
  27 + }
  28 +}
  29 +
  30 +
  31 +void calcSommeFloat (float* sommeFloatNeg, float* sommeFloatPos, float vect[], int n){
  32 + int i ;
  33 + float resNeg=0;
  34 + float resPos=0;
  35 +
  36 + for(i=0; i <= n-1; i++){
  37 + if(vect[i]>=0){
  38 + resPos += vect[i];
  39 + }else {
  40 + resNeg += vect[i];
  41 + }
  42 + }
  43 +
  44 + *sommeFloatPos = resPos;
  45 + *sommeFloatNeg = resNeg;
  46 +}
  47 +
  48 +void fusion ( float vect1[],float vect2[],float vectres[], int n){
  49 + int indV1=0;
  50 + int indV2=0;
  51 + int i;
  52 + while( indV1 <= n-1 && indV2 <= n-1){
  53 + if( vect1[indV1] <= vect2[indV2]){
  54 + vectres[indV1+indV2] = vect1[indV1] ;
  55 + indV1++;
  56 + }else {
  57 + vectres[indV1+indV2] = vect2[indV2] ;
  58 + indV2++;
  59 + }
  60 +
  61 + }
  62 +
  63 + if( indV1 == n ){
  64 + for( i = indV2; i <= n-1; i++){
  65 + vectres[indV1+i] = vect2[i];
  66 + }
  67 + }else {
  68 + for( i = indV1; i <= n-1; i++){
  69 + vectres[indV2+i] = vect1[i];
  70 + }
  71 + }
  72 +}
  73 +
  74 +void intersection ( float vect1[],float vect2[],float vectres[], int n, int * nbValV3){
  75 +
  76 + int indV1=0;
  77 + int indV2=0;
  78 +
  79 + *nbValV3 = 0;
  80 + while( indV1 <= n-1 && indV2 <= n-1){
  81 + if( vect1[indV1] < vect2[indV2]){
  82 +
  83 + indV1++;
  84 + } else if(vect1[indV1] == vect2[indV2]){
  85 + vectres[*nbValV3]= vect1[indV1];
  86 + (*nbValV3)++;
  87 + indV1++;
  88 + indV2++;
  89 + } else {
  90 + indV2++;
  91 + }
  92 +
  93 + }
  94 +
  95 +}
  96 +
  97 +
  98 +
  99 +int main (){
  100 + /*float vector[N];
  101 + float sommeFloatNeg;
  102 + float sommeFloatPos;
  103 +
  104 + setVector(vector, N);
  105 + printVector(vector, N);
  106 +
  107 + calcSommeFloat ( &sommeFloatNeg, & sommeFloatPos, vector, N);
  108 + printf("Pos %f ; Neg %f", sommeFloatPos, sommeFloatNeg);
  109 +
  110 + */
  111 + float vector1[N];
  112 + float vector2[N];
  113 + setVector(vector1, N);
  114 + setVector(vector2, N);
  115 + float vector3[N*2];
  116 + fusion( vector1, vector2, vector3, N);
  117 + printVector( vector3, 2*N);
  118 + float vector4[N];
  119 + int nbVal=0;
  120 + intersection(vector1, vector2, vector4, N, &nbVal);
  121 + printVector( vector4, nbVal);
  122 +
  123 +
  124 + return 1;
  125 +}
... ...
Tp5_Chaines/Tp5_1 0 → 100755
No preview for this file type
Tp5_Chaines/tp5.c 0 → 100644
  1 +++ a/Tp5_Chaines/tp5.c
... ... @@ -0,0 +1,128 @@
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +
  4 +#define N 50
  5 +
  6 +
  7 +void setChaine (char *vect){
  8 + scanf("%s", vect);
  9 +}
  10 +
  11 +void afficheCar (char car, char nb){
  12 +
  13 + int nba;
  14 + nba = nb - '0';
  15 + int i ;
  16 + for(i=1; i <= nba; i++){
  17 + printf("%c", car);
  18 + }
  19 +}
  20 +
  21 +void insererCar (char * ch, char car, int* ind, char nb){
  22 + int i ;
  23 + int nba;
  24 + nba = nb - '0';
  25 + for(i=(*ind); i < (*ind)+nba; i++){
  26 + ch[i] = car;
  27 + }
  28 + *ind = *ind + nba;
  29 +}
  30 +
  31 +
  32 +
  33 +int chiffre(char ch){
  34 + if ((ch >= '0') && (ch <= '9')){
  35 + return 1;
  36 + }else{
  37 + return 0;
  38 + }
  39 +}
  40 +
  41 +int minuscule(char ch){
  42 + if ((ch >= 'a') && (ch <= 'z')){
  43 + return 1;
  44 + }else{
  45 + return 0;
  46 + }
  47 +}
  48 +
  49 +
  50 +int majuscule(char ch){
  51 + if ((ch >= 'A') && (ch <= 'Z')){
  52 + return 1;
  53 + }else{
  54 + return 0;
  55 + }
  56 +}
  57 +
  58 +void traiterChain ( char v[], char res[]){
  59 + int i;
  60 +
  61 + i = 0;
  62 + int ind = 0;
  63 + while(v[i] != '\0'){
  64 + if(v[i+1]=='\0'){
  65 + afficheCar(v[i],'1');
  66 + insererCar(res,v[i],&ind,'1');
  67 + i +=1;
  68 + }else if(chiffre(v[i])){
  69 + afficheCar(v[i+1],((v[i])+1));
  70 + insererCar(res,v[i+1],&ind,((v[i])+1));
  71 + i += 2;
  72 + } else if(minuscule(v[i]) || majuscule(v[i])){
  73 +
  74 + afficheCar(v[i],'1');
  75 + insererCar(res,v[i],&ind,'1');
  76 + i += 1;
  77 + }
  78 +
  79 + }
  80 + res[ind] = '\0';
  81 + printf("---->%s",res);
  82 +}
  83 +
  84 +/*void traiterChaine ( char v[]){
  85 + int i;
  86 +
  87 + i = 0;
  88 + while(v[i] != '\0'){
  89 + if(v[i+1]=='\0'){
  90 + afficheCar(v[i],'1');
  91 + i +=1;
  92 + }else if(chiffre(v[i])){
  93 + afficheCar(v[i+1],((v[i])+1));
  94 + i += 2;
  95 + } else if(minuscule(v[i]) || majuscule(v[i])){
  96 +
  97 + afficheCar(v[i],'1');
  98 + i += 1;
  99 + }
  100 + }
  101 +}*/
  102 +
  103 +
  104 +void EncodChain ( char v[], char encode[]){
  105 + int i;
  106 + i = 0;
  107 + char cCourant;
  108 +
  109 + while(v[i] != '\0'){
  110 + if(v[i+1]=='\0'){
  111 + insererCar(encode,v[i+1],&i,1);
  112 + i +=1;
  113 +
  114 +
  115 + }
  116 + }
  117 +}
  118 +
  119 +
  120 +
  121 +int main (){
  122 + char vect[N];
  123 + char decode[N];
  124 + setChaine(vect);
  125 + traiterChain(vect,decode);
  126 +
  127 + return 1;
  128 +}
... ...
Tp6_Matrices/.Rhistory 0 → 100644
  1 +++ a/Tp6_Matrices/.Rhistory
... ... @@ -0,0 +1,2 @@
  1 +getwd
  2 +q()
... ...
Tp6_Matrices/Tp6_1 0 → 100755
No preview for this file type
Tp6_Matrices/Tp6_1.c 0 → 100644
  1 +++ a/Tp6_Matrices/Tp6_1.c
... ... @@ -0,0 +1,108 @@
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +
  4 +#define N 3
  5 +
  6 +//lecture mat taille N*N
  7 +void lireMatrice (int mat[][N], int n){
  8 +
  9 + int i,j;
  10 + for(i=0; i <= n-1 ; i++){
  11 +
  12 + for(j=0; j <= n-1; j++){
  13 + scanf("%d", &mat[i][j]);
  14 + }
  15 + }
  16 +}
  17 +
  18 +// affiche matrice carre
  19 +void afficheMat (int mat[][N], int n){
  20 + int i,j;
  21 + for(i=0; i <= n-1 ; i++){
  22 +
  23 + for(j=0; j <= n-1; j++){
  24 + printf(" %d ", mat[i][j]);
  25 + }
  26 + printf("\n");
  27 + }
  28 +}
  29 +
  30 +//calcul somme ligne donnee
  31 +int calcSommeLigne (int mat[][N], int n, int ligne){
  32 + int j;
  33 + int somme = mat [ligne][0];
  34 +
  35 + for(j=1; j <= n-1; j++){
  36 + somme += mat[ligne][j];
  37 + }
  38 + //printf("%d",somme);
  39 + return somme;
  40 +}
  41 +
  42 +//calcul somme col donnee
  43 +int calcSommeCol (int mat[][N], int n, int col){
  44 + int i;
  45 + int somme = mat [0][col];
  46 +
  47 + for(i=1; i <= n-1; i++){
  48 + somme += mat[i][col];
  49 + }
  50 + //printf("%d",somme);
  51 + return somme;
  52 +}
  53 +
  54 +//calcul somme col donnee
  55 +void calcSommeDiag (int mat[][N], int n, int * sommeHB, int * sommeBH){
  56 + int i;
  57 + *sommeHB = mat [0][0];
  58 + *sommeBH = mat [n-1][0];
  59 + for(i=1; i <= n-1; i++){
  60 + *sommeHB += mat[i][i];
  61 + *sommeBH += mat[n-1-i][i];
  62 +
  63 + }
  64 + //printf("%d et %d",*sommeHB, *sommeBH);
  65 +
  66 +}
  67 +
  68 +int estMagique(int mat[][N], int n){
  69 + int i=0;
  70 + int estMagique=1;
  71 +
  72 + int sommeHB, sommeBH;
  73 + calcSommeDiag(mat, N, &sommeHB, &sommeBH);
  74 +
  75 + if(sommeHB != sommeBH){
  76 + estMagique = 1;
  77 + }
  78 +
  79 + while (i < n && estMagique ==1){
  80 + if((calcSommeLigne(mat, N,i) != calcSommeCol(mat, N,i)) || (calcSommeLigne(mat, N,i) != sommeHB)){
  81 + estMagique = 0;
  82 + }
  83 +
  84 + i++;
  85 + }
  86 + return estMagique;
  87 +}
  88 +
  89 +int main (){
  90 +
  91 + int mat [N][N];
  92 + lireMatrice(mat, N);
  93 + afficheMat(mat, N);
  94 + calcSommeLigne(mat, N,1);
  95 + calcSommeCol(mat, N,1);
  96 +
  97 + int sommeHB, sommeBH;
  98 + calcSommeDiag(mat, N, &sommeHB, &sommeBH);
  99 +
  100 + if( estMagique(mat,N) == 1){
  101 + printf("MAGIQUE");
  102 + }else{
  103 + printf("Non");
  104 +
  105 + }
  106 + return 1;
  107 +
  108 +}
... ...
Tp6_Matrices/Tp6_2 0 → 100755
No preview for this file type
Tp6_Matrices/Tp6_2.c 0 → 100644
  1 +++ a/Tp6_Matrices/Tp6_2.c
... ... @@ -0,0 +1,176 @@
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +
  4 +#define N 3
  5 +
  6 +//lecture mat taille N*N
  7 +void lireMatrice (int mat[][N], int n){
  8 +
  9 + int i,j;
  10 + for(i=0; i <= n-1 ; i++){
  11 +
  12 + for(j=0; j <= n-1; j++){
  13 + scanf("%d", &mat[i][j]);
  14 + }
  15 + }
  16 +}
  17 +
  18 +// affiche matrice carre
  19 +void afficheMat (int mat[][N], int n){
  20 + int i,j;
  21 + for(i=0; i <= n-1 ; i++){
  22 +
  23 + for(j=0; j <= n-1; j++){
  24 + printf(" %d ", mat[i][j]);
  25 + }
  26 + printf("\n");
  27 + }
  28 +}
  29 +// affiche vecteur
  30 +void afficheVect (int mat[], int n){
  31 + int i;
  32 + for(i=0; i <= n-1 ; i++){
  33 +
  34 +
  35 + printf(" %d ", mat[i]);
  36 +
  37 +
  38 + }
  39 + printf("\n");
  40 +}
  41 +
  42 +
  43 +
  44 +void setMatriceVals (int mat[], int n){
  45 +
  46 + int i;
  47 +
  48 + for(i=0; i <= n-1 ; i++){
  49 + mat[i]=0;
  50 +
  51 + }
  52 + afficheVect(mat,n);
  53 +}
  54 +
  55 +//calcul somme ligne donnee
  56 +int calcSommeLigne (int mat[][N], int n, int ligne){
  57 + int j;
  58 + int somme = mat [ligne][0];
  59 +
  60 + for(j=1; j <= n-1; j++){
  61 + somme += mat[ligne][j];
  62 + }
  63 + //printf("%d",somme);
  64 + return somme;
  65 +}
  66 +
  67 +
  68 +void sommeLigneEtVerifNormal(int mat[][N], int val[], int * estNormal, int * sommeLigne, int n){
  69 + *sommeLigne = calcSommeLigne(mat, n, 0);
  70 + int sommeTempo;
  71 + int i =0;
  72 + int j=0;
  73 + while (*estNormal == 1 && i<n){
  74 + sommeTempo = 0;
  75 + while (*estNormal == 1 && j<n ){
  76 + if(val[((mat[i][j])-1)] == 0){
  77 +
  78 + sommeTempo+= mat[i][j];
  79 + val[((mat[i][j])-1)]=1;
  80 +
  81 +
  82 + afficheVect(val,2*n);
  83 +
  84 + } else {
  85 + *estNormal = 0;
  86 + }
  87 + j++;
  88 +
  89 + }
  90 + if(sommeTempo != *sommeLigne){
  91 + *estNormal =0;
  92 + }
  93 + i++;
  94 + }
  95 +}
  96 +
  97 +
  98 +
  99 +
  100 +//calcul somme col donnee
  101 +int calcSommeCol (int mat[][N], int n, int col){
  102 + int i;
  103 + int somme = mat [0][col];
  104 +
  105 + for(i=1; i <= n-1; i++){
  106 + somme += mat[i][col];
  107 + }
  108 + //printf("%d",somme);
  109 + return somme;
  110 +}
  111 +
  112 +//calcul somme col donnee
  113 +void calcSommeDiag (int mat[][N], int n, int * sommeHB, int * sommeBH){
  114 + int i;
  115 + *sommeHB = mat [0][0];
  116 + *sommeBH = mat [n-1][0];
  117 + for(i=1; i <= n-1; i++){
  118 + *sommeHB += mat[i][i];
  119 + *sommeBH += mat[n-1-i][i];
  120 +
  121 + }
  122 + //printf("%d et %d",*sommeHB, *sommeBH);
  123 +
  124 +}
  125 +
  126 +int estMagiqueNormal(int mat[][N], int n){
  127 + int matVals [2*n];
  128 + setMatriceVals(matVals, 2*n);
  129 +
  130 +
  131 +
  132 + int i=0;
  133 + int estMagique=1;
  134 + int sumLigne;
  135 + sommeLigneEtVerifNormal(mat,matVals, &estMagique, &sumLigne,n);
  136 + printf("%d",sumLigne);
  137 + int sommeHB, sommeBH;
  138 + calcSommeDiag(mat, N, &sommeHB, &sommeBH);
  139 +
  140 + if(sommeHB != sommeBH){
  141 + estMagique = 1;
  142 + }
  143 +
  144 + while (i < n && estMagique ==1){
  145 +
  146 +
  147 + if((sumLigne != calcSommeCol(mat, N,i)) || (sumLigne != sommeHB)){
  148 + estMagique = 0;
  149 + }
  150 +
  151 + i++;
  152 + }
  153 + return estMagique;
  154 +}
  155 +
  156 +int main (){
  157 +
  158 + int mat [N][N];
  159 + lireMatrice(mat, N);
  160 +
  161 + afficheMat(mat, N);
  162 + calcSommeLigne(mat, N,1);
  163 + calcSommeCol(mat, N,1);
  164 +
  165 + int sommeHB, sommeBH;
  166 + calcSommeDiag(mat, N, &sommeHB, &sommeBH);
  167 +
  168 + if( estMagiqueNormal(mat,N) == 1){
  169 + printf("MAGIQUE NORMAL");
  170 + }else{
  171 + printf("Non");
  172 +
  173 + }
  174 + return 1;
  175 +
  176 +}
... ...
Tp6_Matrices/donnee 0 → 100644
  1 +++ a/Tp6_Matrices/donnee
... ... @@ -0,0 +1,2 @@
  1 +2 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0
  2 +
... ...
Tp6_Matrices/donnee2 0 → 100644
  1 +++ a/Tp6_Matrices/donnee2
... ... @@ -0,0 +1 @@
  1 +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
... ...
Tp6_Matrices/donnee3 0 → 100644
  1 +++ a/Tp6_Matrices/donnee3
... ... @@ -0,0 +1 @@
  1 +2 7 6 9 5 1 4 3 8
... ...