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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE * CombinationsFile;
void combinaisons(int *ens, int *combinaison, int n, int p, int i, int t, void (*func)(int*,int) ) {
if (i<p) {
int k;
for (k=t; k<n; k++) {
combinaison[i] = ens[k];
combinaisons(ens,combinaison,n,p,i+1,k+1,func);
}
}
else {
func(combinaison,p);
}
}
void afficher( int *p, int n) {
int i;
for (i = 0; i < n; i++) printf("%d ", p[i]);
printf("\n");
}
void sauvegarderResultat( FILE* Combinaisons, int *p, int n) {
int i;
CombinationsFile=fopen("Combinations.txt","w");
if(CombinationsFile==NULL)
{
printf("Failed to open Combinations file, quitting!\n");
exit(1);
}
p= malloc(n*sizeof(int));
for (i = 0; i < n; i++)
fprintf(CombinationsFile,"%d\n", p[i]);
fclose(CombinationsFile);
}
int main() {
int ens[] = {1,2,3,4,5,6};
int combi[4];
combinaisons(ens,combi,6,4,0,0,afficher);
sauvegarderResultat(CombinationsFile,6,combi);
} |
Partager