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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// declaration de la structure regle
typedef struct regle
{
char *u1;
char *v1;
char *u2;
char *v2;
} regle ;
// declaration de la structure liste de mots
typedef struct mots
{
char *element;
struct mots *suivant;
}mots;
// question 1 fonction pour recombiner mots entre eux
recombiner_mots (char* mot1, char *mot2, regle r1)
{
char *concat1 = malloc(200 * sizeof(char));
char *concat2 = malloc(200 * sizeof(char));
char *sous_mot1 = malloc(200 * sizeof(char));
char *sous_mot2 = malloc(200 * sizeof(char));
char *coupure1 = malloc(200 * sizeof(char));
char *coupure2 = malloc(200 * sizeof(char));
char * newmot1 = malloc(200*sizeof(char));
char * newmot2 = malloc(200*sizeof(char));
char * recombine1 = malloc(200*sizeof(char));
char * recombine2 = malloc(200*sizeof(char));
r1.u1 = malloc (20 * sizeof(char));
r1.v1 = malloc (20 *sizeof(char));
r1.u2 = malloc (20 * sizeof(char));
r1.v2 = malloc (20 * sizeof(char));
strcpy(r1.u1, "ha");
strcpy(r1.v1, "me");
strcpy(r1.u2, "a");
strcpy(r1.v2, "t");
// concatenation entre deux champs de ma structure regle
concat1=strcat(r1.u1,r1.v1);
concat2=strcat(r1.u2,r1.v2);
// verifier que mes mots continennet bien mes regles concatener
sous_mot1 = strstr(mot1,concat1);
sous_mot2 = strstr(mot2,concat2);
if ((sous_mot1 == NULL)||(sous_mot2 == NULL))
{
printf(" pas de recombinaison possible\n ");
}
else
{ /*coupure de mon premier mot au niveau de mes deux regles */
coupure1 = strstr(mot1,r1.v1);
/* coupure de mon deuxieme mot au niveau de mes deux regles */
coupure2 = strstr(mot2,r1.v2);
/* recopie mon mot jusqu'a la hauteur de ma coupure pour mes deux mots */
strncpy (newmot1,mot1,strlen(mot1)-strlen(coupure1));
strncpy (newmot2,mot2,strlen(mot2)-strlen(coupure2));
/* concatene mon mot obtenu precedemment avec l'autre partie de la coupure de l'autre mot */
recombine1=strcat(newmot1,coupure2);
recombine2=strcat(newmot2,coupure1);
/* affiche les mots recombiner */
printf("les mots recombiner sont : %s %s\n",recombine1,recombine2);
}
free(concat1);
free(concat2);
free(sous_mot1);
free(sous_mot2);
free(coupure1);
free(coupure2);
free(newmot1);
free(newmot2);
free(recombine1);
free(recombine2);
}
// selection d'un mot dans la liste avec un evariable generer aléatoirement
mots* selection(mots *m1,int val)
{
mots *curseur = m1;
int i ;
for (i=0 ; i < val ;i++)
{
curseur = curseur->suivant ;
}
return curseur ;
}
//question 2
void recombiner_aleatoire(char* mot3, char *mot4, regle r3)
{
char *concat3 = malloc(200 * sizeof(char));
char *concat4 = malloc(200 * sizeof(char));
char *sous_mot3 = malloc(200 * sizeof(char));
char *sous_mot4 = malloc (200 * sizeof(char));
char *coupure3 = malloc(200 * sizeof(char));
char *coupure4 = malloc(200 * sizeof(char));
char * newmot3 = malloc(200*sizeof(char));
char * newmot4 = malloc(200*sizeof(char));
char * recombine3 = malloc(200*sizeof(char));
char * recombine4 = malloc(200*sizeof(char));
r3.u1 = malloc (200 * sizeof(char));
r3.v1 = malloc (200 *sizeof(char));
r3.u2 = malloc (200 * sizeof(char));
r3.v2 = malloc (200 * sizeof(char));
/* concatenation entre deux champs de ma structure regle soit donne ab*/
concat3=strcat(r3.u1,r3.v1);
printf(" les deux regles recombiner de aleatoire %s \n ",concat3);
/* concaténation entre deux autres champs de mas trcuture regle soit ba */
concat4=strcat(r3.u2,r3.v2);
printf( " allo\n");
// verifier que mes mots continennet bien mes regles concatener soit possibiltés d'une recombinaison
sous_mot3 = strstr(mot3,concat3);
sous_mot4 = strstr(mot4, concat4);
if ((sous_mot3 != NULL)&&(sous_mot4 != NULL))
{
// coupure de mon premier mot au niveau de mes deux regles
coupure3 = strstr(mot3,r3.v1);
// coupure de mon deuxieme mot au niveau de mes deux regles
coupure4 = strstr (mot4,r3.v2);
printf(" coucou coupure 4");
// recopie mon mot jusqu'a la hauteur de ma coupure pour mes deux mots
strncpy (newmot3,mot3,strlen(mot3)-strlen(coupure3));
strncpy (newmot4,mot4,strlen(mot4)-strlen(coupure4));
// concatene mon mot obtenu precedemment avec l'autre partie de la coupure de l'autre mot
recombine3=strcat(newmot3,coupure4);
printf(" recombine 3");
recombine4=strcat(newmot4,coupure3);
printf(" recombine 4");
strcpy(mot3,recombine3);
strcpy(mot4,recombine4);
//ecraser en reprenant et le mettant dans mot1
// affiche les mots recombiner
printf("les mots recombiner sont : %s %s\n",mot3,mot4);
}
}
void afficherListe(mots *m1)
{
mots *tmp = m1;
while(tmp != NULL)
{
/* On affiche */
printf("%s\n", tmp->element);
/* On avance d'une case */
tmp = tmp->suivant;
}
}
int main ()
{
regle R2;
int nb_hasard = rand()%6;
char *mot1 ;
char *mot2 ;
char *resultat;
mot1 = malloc (200 * sizeof(char));
mot2 = malloc (200 * sizeof(char));
char *mot3 = malloc(200 * sizeof(char));
char *mot4 = malloc (200 * sizeof(char));
mots *lemotfinal;
mots *lemotfinal2;
int i,h,j ;
int nb_iterations = 0;
// printf("veuillez entrer un premier mot :\n");
// scanf("%s",mot1);
// printf("veuillez entrer un deuxieme mot :\n");
// scanf("%s",mot2);
// recombiner_mots(mot1,mot2,R2);
// srand(time(NULL));
// question 2
mots lemot6 = {"baaa",NULL};
mots lemot5 = {"baa",&lemot6};
mots lemot4 = {"ba",&lemot5};
mots lemot3 = {"aaab",&lemot4};
mots lemot2 = {"aab",&lemot3};
mots lemot1 = {"ab",&lemot2};
// declaration d'une nouvelle regle
regle r4 ={"a","b","b","a"};
//mots *lemotfinal;
// mots *lemotfinal2;
// int i,h,j ;
printf("veuillez entrer un premier mot :\n");
scanf("%s",mot1);
printf("veuillez entrer un deuxieme mot :\n");
scanf("%s",mot2);
recombiner_mots(mot1,mot2,R2);
srand(time(NULL));
for ( h= 0; h< 2 ; h++)
{ // fonction qui me prend un mots dans la liste aleatoirement
i= rand()%6;
printf(" le nombre 1 au hasard %d\n ",i);
lemotfinal = selection(&lemot1,i);
printf(" lemotfinal %s\n",lemotfinal->element);
j= rand()%6;
printf(" le nombre 2 au hasard %d\n ",j);
lemotfinal2 = selection(&lemot1,j);
printf(" lemotfinal 2 %s\n",lemotfinal2->element );
recombiner_aleatoire(lemotfinal->element ,lemotfinal2->element ,r4);
afficherListe(&lemot1);
nb_iterations ++;
printf(" nombre d'iterations %d \n ",nb_iterations);
}
return 0;
} |
Partager