1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| #include <stdio.h>
#include <stdlib.h>
#include <math.h>
void TStructure();
void IndiceVecteur();
void IndiceAllVecteur();
int NbrDimension, i, j;
int *D, *d;
main() {
int Choix;
do {
printf("\n\n Menu\n\n");
printf("-1- Definir les tailles de la structure multidimensionnee\n");
printf("-2- Calculer l'indice dans le vecteur a partir des indices de chaque dimension\n");
printf("-3- Obtenir les indices de chaque dimension pour un indice du vecteur\n");
printf("-0- Fin\n");
printf("\n Choix ? ");
scanf("%d",&Choix);
switch(Choix) {
case 0:
printf("\n Bye ...\n\n");
break;
case 1:
TStructure();
break;
case 2:
IndiceVecteur();
break;
case 3:
IndiceAllVecteur();
break;
}
} while(Choix!=0 || Choix>1);
system("pause");
return 0;
}
void TStructure(){
printf("\n Nombre de dimension ? ");
scanf("%d", &NbrDimension);
D=malloc(NbrDimension*sizeof(int));
d=malloc(NbrDimension*sizeof(int));
for(i=NbrDimension-1; i>=0; i--) {
printf("\n Taille de la dimension %d ? ", i+1);
scanf("%d", &D[i]);
}
return;
}
void IndiceVecteur() {
int indiced;
int indiceD=1, indice=0;
for(i=NbrDimension-1; i>=0; i--) {
printf("\n Indice dans la dimension %d ? ", i+1);
scanf("%d", &d[i]);
}
for(i=NbrDimension-1; i>=0; i--) {
for(j=NbrDimension-2; j>=0; j--) {
indiceD *= *(D+j);
}
indiced = *(d+i) * indiceD;
indice+=indice+indiced;
}
printf("\nIndice = %d", indice);
return;
}
void IndiceAllVecteur() {
int indiceV, indice, y, k;
printf("\n Indice dans le vecteur ? ");
scanf("%d", &indice);
for(i=NbrDimension-1; i>=0; i--) {
int x=1;
for(j=i-2; j>=0; j--) {
x*= *(D+j);
}
y=i/x;
indiceV=y% *(D+i);
printf("\n Indice dans la dimension %d = % d", i+1, y);
}
return;
} |
Partager