-
Notifications
You must be signed in to change notification settings - Fork 0
/
C10.c
148 lines (112 loc) · 2.51 KB
/
C10.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
//VertexCover2Sat using Minisat
//??%Loading....
int fact(int nb)
{
if ( nb == 0 )
return 1;
else return(nb * fact(nb - 1));
}
int nb_sommet(){
return 10;}
int est_adjacent(int u, int v){
if(0< u && 0<v && u<11 && v<11){
return (((10+u-v)%10==1)||((10+v-u)%10==1));
}
else return 0;
}
int comb_cal(int n)
{
return fact(nb_sommet()) / ( ( fact(n) ) * ( fact(nb_sommet() - n) ) );
}
void
ouvrirFichier (char *nom, FILE **desc, char *mode)
{
*desc = fopen (nom, mode);
if (*desc == NULL)
{
fprintf (stderr,
"\"%s\": nom de fichier incorrect",
nom);
if (mode [0] == 'w')
fprintf (stderr, " ou ouverture en ecriture impossible.");
exit (EXIT_FAILURE);
}
}
void
ecrireEnTete (FILE *descFic, char *mode, int k)
{
fprintf (descFic, "%s", mode);
fprintf (descFic, "%d %d \n", nb_sommet(), nb_sommet()+comb_cal(k));
}
void
ecrireGraph (char *nom, int k)
{
FILE *descFic = NULL;
ouvrirFichier (nom, &descFic, "w");
ecrireEnTete (descFic, "p cnf ", k);
for(int x=1; x<=nb_sommet(); x++)
{ fprintf(descFic, " %d ", x );
for(int y=0; y<=nb_sommet(); y++)
{
if( (est_adjacent(x,y)==1) )
{
fprintf(descFic, " %d ", y);
}
}
fprintf(descFic, " 0\n");
}
fclose (descFic);
}
void ecrireComb(char* nom, int m, int n)
{
FILE *descFic = NULL;
ouvrirFichier (nom, &descFic, "w");
int i;
unsigned char c[100];
for (i = 0; i < n; i++) c[i] = n - i;
while (1) {
for (i = n; i--;)
{
fprintf(descFic, "-%d", c[i]);
if(i |= i)
fprintf(descFic, " ");
else
fprintf(descFic, " 0\n");
}
/* this check is not strictly necessary, but if m is not close to n,
it makes the whole thing quite a bit faster */
if (c[i]++ < m) continue;
for (i = 0; c[i] >= m - i;) if (++i >= n) return;
for (c[i]++; i; i--) c[i-1] = c[i] + 1;
}
}
int main(int argc, char* argv[])
{
int status;
pid_t fork_return;
fork_return = fork();
if (fork_return == 0) /* child process */
{
int k;
printf("Choose K : \n");
scanf("%d", &k);
ecrireGraph("Graphe.in", k) ;
ecrireComb("Combinaison.in", nb_sommet(), k);
exit(0);
}
else /* parent process */
{
wait(&status);
char command[50];
strcpy( command, "cat Graphe.in Combinaison.in > Final.in" );
system(command);
}
}