-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprobleme.c
122 lines (113 loc) · 2.95 KB
/
probleme.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
#include "probleme.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void afficherProbleme(prob_t prob) {
unsigned int i, j;
printf("\n");
if (prob.typeOpt)
printf("Maximiser z = ");
else
printf("Minimiser z = ");
for (j = 0; j < prob.nVar; j++) {
if (prob.fonc[j] < 0)
printf("- ");
else
printf("+ ");
printf("%6.2lf ", fabs(prob.fonc[j]));
printf("x%d ", j + 1);
}
printf("\n\n");
printf("Sous les contraintes\n\n");
for (i = 0; i < prob.nCont; i++) {
for (j = 0; j < prob.nVar; j++) {
if (prob.cont[i][j] < 0)
printf("- ");
else
printf("+ ");
printf("%6.2lf ", fabs(prob.cont[i][j]));
printf("x%d ", j + 1);
}
if (prob.signeCont[i] == 0)
printf("<= ");
else if (prob.signeCont[i] == 1)
printf(">= ");
if (prob.valCont[i] < 0)
printf("- ");
else
printf("+ ");
printf("%6.2lf\n", fabs(prob.valCont[i]));
}
printf("\n");
for (j = 0; j < prob.nVar; j++) {
printf("x%d >= 0", j + 1);
if (j < prob.nVar - 1)
printf(", ");
}
printf("\n");
}
int lireProbleme(char* nomFichier, prob_t* prob) {
/*Lit les donnees d'un probleme a partir d'un fichier specifie*/
/*et les place dans la structure prob*/
FILE* fichier;
char chTemp[20], signe[3];
unsigned int i, j;
if ((fichier = fopen(nomFichier, "r"))) {
fscanf(fichier, "%s", chTemp);
fscanf(fichier, "%ud", &prob->nVar);
fscanf(fichier, "%s", chTemp);
fscanf(fichier, "%ud", &prob->nCont);
fscanf(fichier, "%s", chTemp);
if (!strcmp(chTemp, "max"))
prob->typeOpt = 1;
else
prob->typeOpt = 0;
prob->fonc = (double*) malloc(sizeof(double) * prob->nVar);
for (j = 0; j < prob->nVar; j++) {
fscanf(fichier, "%s", signe);
fscanf(fichier, "%lf", &prob->fonc[j]);
if (!strcmp(signe, "-"))
prob->fonc[j] = -prob->fonc[j];
fscanf(fichier, "%s", chTemp);
}
prob->cont = (double**) malloc(sizeof(double*) * prob->nCont);
prob->signeCont = (int*) malloc(sizeof(int) * prob->nCont);
prob->valCont = (double*) malloc(sizeof(double) * prob->nCont);
for (i = 0; i < prob->nCont; i++) {
prob->cont[i] = (double*) malloc(sizeof(double) * prob->nVar);
for (j = 0; j < prob->nVar; j++) {
fscanf(fichier, "%s", signe);
fscanf(fichier, "%lf", &prob->cont[i][j]);
if (!strcmp(signe, "-"))
prob->cont[i][j] = -prob->cont[i][j];
fscanf(fichier, "%s", chTemp);
}
fscanf(fichier, "%s", signe);
if (!strcmp(signe, "<="))
prob->signeCont[i] = 0;
else if (!strcmp(signe, ">="))
prob->signeCont[i] = 1;
fscanf(fichier, "%lf", &prob->valCont[i]);
}
fclose(fichier);
} else {
printf("Probleme lecture fichier\n");
return -1;
}
return 0;
}
void libererMemoireProbleme(prob_t prob) {
unsigned int i;
if (prob.fonc != NULL)
free(prob.fonc);
for (i = 0; i < prob.nCont; i++)
if (prob.cont[i] != NULL)
free(prob.cont[i]);
if (prob.cont != NULL)
free(prob.cont);
if (prob.signeCont != NULL)
free(prob.signeCont);
if (prob.valCont != NULL)
free(prob.valCont);
}