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
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static char *filtrer(char *chaine, int car)
{
size_t i;
for (i = 0; chaine[i] && chaine[i] != car; i++)
{
}
for (; chaine[i]; i++)
{
chaine[i] = chaine[i + 1];
}
return chaine;
}
static long recursive (char *pile
, char *reserve
, size_t taille
, size_t taillei
, long combinaisons)
{
if (pile[0] != 0)
{
printf("%s\n", pile);
}
if (taille != 0)
{
size_t i;
for (i = 0; i < taille ; i++)
{
char *reserves = malloc (taille + 1);
if (reserves != NULL)
{
memcpy(reserves, reserve, taille + 1);
pile[taillei - taille] = reserve[i];
pile[taillei - taille + 1] = 0;
combinaisons = recursive(pile
, filtrer(reserves, reserves[i])
, taille - 1
, taillei
, combinaisons + 1);
free(reserves);
}
else
{
combinaisons = -1;
}
}
}
return combinaisons;
}
int main (void)
{
char atraiter[100];
printf ("\nSaisir une chaine de caractere : ");
fgets (atraiter, sizeof atraiter, stdin);
{
char *p = strchr(atraiter, '\n');
{
if (p != NULL)
{
*p = 0;
}
}
}
{
size_t metre = strlen(atraiter);
if (metre != 0)
{
char *magasin = malloc(metre + 1);
if (magasin == NULL)
{
fprintf(stderr, "memory error: magasin\n");
}
else
{
long combinaisons = 0;
magasin[0] = 0;
printf("Les combinaisons possibles sont : \n");
combinaisons = recursive(magasin, atraiter, metre, metre, combinaisons);
if (combinaisons != -1)
{
printf ("\nLe nbre de combinaisons est %lu\n", (unsigned long) combinaisons);
}
free(magasin), magasin = NULL ;
}
}
}
return 0;
} |
Partager