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 258 259 260 261 262 263 264 265 266 267 268 269 270 271
| #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "..\..\Modules\Lecture\lecture.h"
void LibereMemoireTableau(char** tableau, int taille);
void DebutChaine(char chaine[], char retour[]);
void ExtraireInfoLigne(char chaine[], char** info);
void PermutationAleatoire(int n, int tab[]);
char** ConversionFichier(const char* fichier, int* nblignes);
int main() {
int nbrquestions;
char** fichier;
// Lecture du fichier
fichier = ConversionFichier("ExempleQCM.csv", &nbrquestions);
if (fichier == NULL) {
printf("Erreur d'allocation ou de lecture du fichier!\n");
return 1;
}
printf("Nombre de questions lues: %d\n", nbrquestions);
// Génération d'un ordre aléatoire pour les questions
int* ordre = (int*)malloc(nbrquestions * sizeof(int));
if (ordre == NULL) {
printf("Erreur d'allocation de mémoire pour l'ordre des questions!\n");
LibereMemoireTableau(fichier, nbrquestions);
return 1;
}
for (int t = 0; t < nbrquestions; t++)
ordre[t] = t;
PermutationAleatoire(nbrquestions, ordre);
float scorefinal = 0;
float scoremax = 0;
// Boucle pour chaque question
for (int i = 0; i < 8; i++) {
int n = ordre[i];
printf("Traitement de la question %d (index %d)\n", i + 1, n); //!!!!!!!!!!!!!temporaire
// Déterminez la taille nécessaire pour info
int tailleInfo = 0;
char* ligne = fichier[n];
for (int pos = 0; ligne[pos] != '\0'; pos++) {
if (ligne[pos] == ';') {
tailleInfo++;
}
}
tailleInfo++; // Compte pour le dernier élément après le dernier séparateur
printf("Taille d'info pour la question %d : %d\n", i + 1, tailleInfo);//!!!!!!!!!!!!!!!!!!!!temporaire
char** info = (char**)malloc(tailleInfo * sizeof(char*));
if (info == NULL) {
printf("Erreur d'allocation de mémoire pour info!\n");
free(ordre);
LibereMemoireTableau(fichier, nbrquestions);
return 1;
}
for (int j = 0; j < tailleInfo; j++) {
info[j] = (char*)malloc(strlen(ligne) + 1);
if (info[j] == NULL) {
printf("Erreur d'allocation de mémoire pour info[%d]!\n", j);
LibereMemoireTableau(info, j);
free(info);
free(ordre);
LibereMemoireTableau(fichier, nbrquestions);
return 1;
}
}
printf("Extraction des infos pour la ligne %d\n", i + 1); //!!!!!!!!!!!!!temporaire
ExtraireInfoLigne(fichier[n], info);
// Allocation de mémoire pour question et autres éléments
char* question = (char*)malloc(strlen(info[0]) + 1);
if (question == NULL) {
printf("Erreur d'allocation de mémoire pour question!\n");
LibereMemoireTableau(info, tailleInfo);
free(info);
free(ordre);
LibereMemoireTableau(fichier, nbrquestions);
return 1;
}
strcpy(question, info[0]);
int nbrprop = atoi(info[1]);
printf("Nombre de propositions pour la question %d: %d\n", i + 1, nbrprop); //!!!!!!!!!!!!!!!!!!temporaire
float* points = (float*)malloc(nbrprop * sizeof(float));
if (points == NULL) {
printf("Erreur d'allocation de mémoire pour points!\n");
free(question);
LibereMemoireTableau(info, tailleInfo);
free(info);
free(ordre);
LibereMemoireTableau(fichier, nbrquestions);
return 1;
}
char** propositions = (char**)malloc(nbrprop * sizeof(char*));
if (propositions == NULL) {
printf("Erreur d'allocation de mémoire pour propositions!\n");
free(points);
free(question);
LibereMemoireTableau(info, tailleInfo);
free(info);
free(ordre);
LibereMemoireTableau(fichier, nbrquestions);
return 1;
}
for (int j = 0; j < nbrprop; j++) {
propositions[j] = (char*)malloc(strlen(info[j + 2]) + 1);
if (propositions[j] == NULL) {
printf("Erreur d'allocation de mémoire pour propositions[%d]!\n", j);
LibereMemoireTableau(propositions, j);
free(propositions);
free(points);
free(question);
LibereMemoireTableau(info, tailleInfo);
free(info);
free(ordre);
LibereMemoireTableau(fichier, nbrquestions);
return 1;
}
strcpy(propositions[j], info[j + 2]);
points[j] = atof(info[nbrprop + j + 2]);
}
float maxpoints = 0;
for (int j = 0; j < nbrprop; j++) {
if (points[j] > 0) {
maxpoints += points[j];
}
}
scoremax += maxpoints;
int* ordreprop = (int*)malloc(nbrprop * sizeof(int));
if (ordreprop == NULL) {
printf("Erreur d'allocation de mémoire pour ordreprop!\n");
LibereMemoireTableau(propositions, nbrprop);
free(propositions);
free(points);
free(question);
LibereMemoireTableau(info, tailleInfo);
free(info);
free(ordre);
LibereMemoireTableau(fichier, nbrquestions);
return 1;
}
for (int t = 0; t < nbrprop; t++)
ordreprop[t] = t;
PermutationAleatoire(nbrprop, ordreprop);
printf("Question %d: %s\n", i + 1, question);
for (int j = 0; j < nbrprop; j++)
printf("%d: %s\n", j + 1, propositions[ordreprop[j]]);
float totalpoints = 0;
int rep=1;
do {
printf("Entrez vos réponses (entrez 0 pour passer à la question suivante) : ");
scanf("%d", &rep);
if (rep >= 1 && rep <= nbrprop)
totalpoints += points[ordreprop[rep - 1]];
} while (rep != 0);
scorefinal += totalpoints;
// Libération de la mémoire pour chaque question
for (int j = 0; j < nbrprop; j++) {
free(propositions[j]);
}
free(propositions);
free(points);
free(ordreprop);
free(question);
LibereMemoireTableau(info, tailleInfo);
}
printf("Votre score final est %f sur %f\n", scorefinal, scoremax);
// Libération de la mémoire
LibereMemoireTableau(fichier, nbrquestions);
free(fichier);
free(ordre);
return 0;
}
// Fonction pour libérer la mémoire allouée pour un tableau de chaînes de caractères
void LibereMemoireTableau(char** tableau, int taille) {
if (tableau != NULL) {
for (int i = 0; i < taille; i++) {
free(tableau[i]);
}
free(tableau);
}
}
void DebutChaine(char chaine[], char retour[]) {
int i = 0;
while (chaine[i] != '\0' && chaine[i] != ';') {
retour[i] = chaine[i];
i++;
}
retour[i] = '\0';
}
void ExtraireInfoLigne(char chaine[], char** info) {
int pos = 0;
int i = 0;
while (chaine[pos] != '\0') {
DebutChaine(&chaine[pos], info[i]);
pos = pos + strlen(info[i]) + 1;
i++;
}
}
void PermutationAleatoire(int n, int tab[]) {
srand((unsigned)time(NULL));
for (int i = 0; i < n - 1; i++) {
int j = i + rand() % (n - i);
int k = tab[i];
tab[i] = tab[j];
tab[j] = k;
}
}
char** ConversionFichier(const char* fichier, int* nblignes) {
FILE* QCM;
char ligne[512 + 1];
QCM = fopen(fichier, "r");
if (QCM == NULL)
return NULL;
*nblignes = 0;
while (fgets(ligne, sizeof(ligne), QCM) != NULL)
(*nblignes)++;
rewind(QCM);
char** tlignes = (char**)malloc(*nblignes * sizeof(char*));
if (tlignes == NULL) {
fclose(QCM);
return NULL;
}
for (int i = 0; i < *nblignes; i++) {
if (fgets(ligne, sizeof(ligne), QCM) == NULL) {
for (int j = 0; j < i; j++)
free(tlignes[j]);
free(tlignes);
fclose(QCM);
return NULL;
}
tlignes[i] = (char*)malloc(strlen(ligne) + 1);
if (tlignes[i] == NULL) {
for (int j = 0; j < i; j++)
free(tlignes[j]);
free(tlignes);
fclose(QCM);
return NULL;
}
strcpy(tlignes[i], ligne);
}
fclose(QCM);
return tlignes;
} |