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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
//prototypes
int initialisation (int *nbtires);
int calculuser(int cible, int *tab, int taille);
int operation(int n1, int n2, int operateur);
int main()
{
int nbc; //nb cible
int *nbtir; //pointeur car tableau
nbtir = (int *)malloc(sizeof(*nbtir) * 6); //allocation mémoire pour tableau tirage. Malloc car tableau
if (nbtir == NULL)
{
printf("Erreur lors de l'allocation mémoire\n"); //si allocation pas réussie, exit
return (EXIT_FAILURE);
}
nbc=initialisation(nbtir);
free(nbtir);
calculuser(nbc, nbtir, 6);
return EXIT_SUCCESS;
}
//Initialisation du jeu : 6 nbs aléatoires tirés + 1 nb à trouver
int initialisation (int *nbtires)
{
int indice,i,j;
int nbcible;
int nbpossible[14]={1,2,3,4,5,6,7,8,9,10,25,50,75,100}; //valeurs départ possibles
srand(time(0));
printf("Tirage : ");
for (i=0;i<6;i++)
{
indice= rand()%14;
if (nbpossible[indice] <= 10) //pour faire apparaitre 2 fois un nombre <10
{
nbtires[i] = nbpossible[indice]; //nombres tirés au hasard
printf("%i \t",nbpossible[indice]);
fflush(NULL);
}
else
{
for(j=0;j<6;j++)
{
if (nbtires[j] == nbpossible[indice]) //si 2 fois mm nb, retour au début du for(i)
continue;
if (j == 5) //si boucle finie, on stocke le nb tiré
{
nbtires[i] = nbpossible[indice]; //nombres tirés au hasard
printf("%i \t",nbpossible[indice]);
fflush(NULL);
}
}
}
}
nbcible=(rand()%900)+100;//nombre à trouver enter 100 et 999
printf("\n\nVous devez trouver : %i\n\n",nbcible);
return (nbcible);
}
//calcul de la solution du nombre à trouver
int calculuser(int cible, int *tab, int taille)
{
int i,j,o; //boucles sur nb1, nb2, operation
int resultat;
char operateurs[4]={'+','-','*','/'}; //les 4 opérations
int backup[6]; //tableau auxiliaire pr stocker nbs utilisés
//int prochediff,procheres;
int a,b;
for (i=0;i<5;i++)
{
for (j=1;j<6;j++)
{
for(o=0;o<4;o++)
{
resultat=operation(*(tab+i),*(tab+j),o);
if (resultat==cible) continue;
if (resultat!=cible)
{
backup[a]=*(tab+i);
backup[b]=*(tab+j);
*(tab+i)=resultat;
*(tab+j)=*(tab+(taille-1));
printf("%d %c %d = %d\n", *(tab+i),operateurs[o],*(tab+j),resultat);
calculuser(cible,tab, 6); //appel récursif pr recommencer
}
}
}
}
return 0;
}
//fonction qui calcule les opérations entre 2 nb tirés
int operation(int n1, int n2, int operateur)
{
switch (operateur)
{
case 0:
{
return (n1+n2);
break;
}
case 1:
{
return (n1-n2);
break;
}
case 2:
{
return (n1*n2);
break;
}
case 3:
{
if (0== n1%n2) return (n1/n2);
break;
}
default:
{
printf("ERROR!!!\n");
break;
}
}
return 0;
} |
Partager