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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NB 10
int GenAlea(int bornemin, int bornemax);
void wait(double seconds);
void ImpAlea(int nb, int *bornemin, int *bornemax, double seconds, int tot);
void SetBorne(int *bornemin, int *bornemax, int num, int d, int cible);
/******************************************************************************/
/* Main */
/******************************************************************************/
int main() {
int i, j, alea, tot;
int bornemin[NB]={0}, bornemax[NB]={0}, cible[NB]={0};
double seconds;
srand((unsigned) time(NULL));
// Definition de nombre a atteindre (serie de NB nombres de 4 chiffres)
printf("on veut atteindre :");
for (i=0;i<NB;i++) {
cible[i]=GenAlea(1000,9999);
printf(" %d",cible[i]);
}
printf("\n");
tot=50;
seconds=0.005;
for (i=0;i<NB;i++) {
SetBorne(bornemin,bornemax,i,0,0);
}
ImpAlea(NB,bornemin,bornemax,seconds,tot);
for (i=0;i<NB;i++) {
for (j=1;j<=4;j++) {
SetBorne(bornemin,bornemax,i,j,cible[i]);
ImpAlea(NB,bornemin,bornemax,seconds,tot);
}
}
return EXIT_SUCCESS;
}
/******************************************************************************/
/* Fixe les bornes */
/******************************************************************************/
void SetBorne(int *bornemin, int *bornemax, int num, int u, int cible) {
int m, c, d;
m = cible / 1000;
c = (cible - (m * 1000)) / 100;
d = (cible - (m * 1000) - (c * 100)) / 10;
switch (u) {
case 0:
bornemin[num] = 0;
bornemax[num] = 9999;
break;
case 1:
bornemin[num] = m * 1000;
bornemax[num] = m * 1000 + 999;
break;
case 2:
bornemin[num] = m * 1000 + c * 100;
bornemax[num] = m * 1000 + c * 100 + 99;
break;
case 3:
bornemin[num] = m * 1000 + c * 100 + d * 10;
bornemax[num] = m * 1000 + c * 100 + d * 10 + 9;
break;
case 4:
bornemin[num] = cible;
bornemax[num] = cible;
break;
}
}
/******************************************************************************/
/* Affiche tot nombres compris entre bornemin et bornemax */
/******************************************************************************/
void ImpAlea(int nb, int *bornemin, int *bornemax, double seconds, int tot) {
int i, j, alea, n;
char *retourne=NULL;
n = 5 * nb;
retourne=malloc(sizeof(char)*n);
if ( retourne==NULL) {
fprintf(stderr,"Erreur allocation memoire\n");
exit(EXIT_FAILURE);
}
for (i=0;i<n-1;i++) {
retourne[i] = '\b';
}
retourne[n-1] = 0;
for (i=0;i<tot;i++) {
for (j=0;j<nb;j++) {
alea=GenAlea(bornemin[j],bornemax[j]);
if ( j == 0 ) {
printf("%s%04d",retourne,alea);
} else {
printf(" %04d",alea);
}
}
wait(seconds);
}
free(retourne);
}
/******************************************************************************/
/* Génère un nombre entier aléatoire compris entre bornemin et bornemax */
/******************************************************************************/
int GenAlea(int bornemin, int bornemax) {
double alea;
alea = (1.0 * rand()) / RAND_MAX;
alea = bornemin + (bornemax - bornemin) * alea;
return((int)alea);
}
/******************************************************************************/
/* Attend un certain nombre de seconde */
/******************************************************************************/
void wait(double seconds) {
double endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC;
while (clock() < endwait) {}
} |
Partager