Problème dans la fonction readDicoFromFile

parents
Pipeline #819 failed with stages
This diff is collapsed.
Case #1: 0
Case #2: 1
Case #3: 0
Case #4: 1
Case #5: 6
Case #6: 7
Case #7: 1
Case #8: 8
Case #9: 1
Case #10: 8
10 25 10
nwlrbbmqbh
cdarzowkky
hiddqscdxr
jmowfrxsjy
bldbefsarc
bynecdyggx
xpklorelln
mpapqfwkho
pkmcoqhnwn
kuewhsqmgb
buqcljjivs
wmdkqtbxix
mvtrrbljpt
nsnfwzqfjm
afadrrwsof
sbcnuvqhff
bsaqxwpqca
cehchzvfrk
mlnozjkpqp
xrjxkitzyx
acbhhkicqc
oendtomfgd
wdwfcgpxiq
vkuytdlcgd
ewhtacioho
rdtqkvwcsg
nwlr(nqxb)bm(dgqw)bh
(gu)(umo)(cdq)(bru)(ote)(xyk)(oah)(hwc)(vdm)(xr)
(aknw)(glmw)(jlue)(kruw)(bci)(xbu)(mue)(qemn)(tyab)(hmrd)
(ijklmopqrstuvyzabcefgh)(qrtuvwxyzabcdefghijklmnop)(ijmnopqsuvwxyabcdefgh)(fghijklmopqrstuvxyzbcde)(pqrstuvwxyzbcdefghijkno)(ghijlpqsuvwxyzabcdef)(yzbcdefgijlmnopqrstuvwx)(nopqrstuvwxyzabcdefghijklm)(klmnopqrsuvwxyzabcdefghj)(rstuvwxbcdefgijklmnopq)
(mnopqrstuvwxyzbcefgikl)(efghjkmnopqrstuvwxyzabcd)(tuvwxyzbcdefghijklmnopq)(pqrstuvwxyabcdefghijklmn)(cdefghijklmnopqstvxzab)(klmnoqrstuvwxyabcdeghi)(jlopqrsuvwxyzabdefghi)(cdefghijklmnopqruxzab)(abdefghijklmnpqrstuvwxz)(zabcdfgijklmnoprstuvwxy)
(rtwd)(dgmy)(sdqr)(kzc)(nqyb)(qtv)(qbc)(xijt)(vcil)(bnvx)
(ghjklmnpstuvwyzabcdef)(xyzabdegjklmnopqstuv)(abcdefghiklmnopqrstuwxz)(cdefghijklmnopqrstuvwxyab)(hjmnopqrstuvwxyzabcdefg)(rtvwyzabcdefghijklnop)(yzcdefhjklmnpqrstuvwx)(opqrstvwxyzabcdefghijklm)(qruvwxyzacdefgijlmno)(xyzabcegiklmnopqstuvw)
(almq)(fot)(pqah)(kovd)(ira)(mqr)(mvwx)(svbj)(sao)(zfi)
(opqrsvwxyzabcdefghijklmn)(yzabcdefgijklmnpqrstuvwx)(ijklmopqrstuvwxzabcefgh)(stuvwxyzabcdefhijklmnopq)(mnopqrstuvwxyzabdefghijkl)(ijklmnopqrstuvwxzbcdefh)(stuvwzabcdefgijklmnopq)(efghijklmnopqrsuvwxyacd)(rstuvxyzadefghijklmnopq)(noqrsuwxyzabdfghjlm)
// gcc -W -Wall -std=c99 correctAlienLanguage.c -g
// valgrind --leak-check=yes --leak-check=full --show-leak-kinds=all --show-reachable=no ./a.out
// Input
// 3 5 4 // nbLettersInAWord nbWords nbTestCases
// abc
// bca
// dac
// dbc
// cba
// (ab)(bc)(ca)
// abc
// (abc)(abc)(abc)
// (zyx)bc
//
// Output
// Case #1: 2
// Case #2: 1
// Case #3: 3
// Case #4: 0
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
typedef char* Word;
typedef struct {
Word *words;
int size;
} Dictionnaire;
typedef struct {
Word *tokens;
int size;
} Motif;
typedef struct {
Motif *motifs;
int size;
} MotifTableau;
void afficherMotif(Motif *motif) {
int i;
for(i = 0; i < motif->size; i++) {
printf("%s ", motif->tokens[i]);
}
}
void afficherMotifTab(MotifTableau *motifTab) {
int i;
for(i = 0; i < motifTab->size; i++) {
afficherMotif(&motifTab->motifs[i]);
printf("\n");
}
}
void afficherDico(Dictionnaire *dico) {
int i;
for(i = 0; i < dico->size; i++) {
printf("%s\n", dico->words[i]);
}
}
char* concatenate(const char *s1, const char *s2) {
char *result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
//test d'erreur du malloc
strcpy(result, s1);
strcat(result, s2);
return result;
}
//La fonction prend un caractère en paramètre et renvoie une chaine de caractère correspondante
char* char2str(char caractere) {
char *pChar = malloc(2*sizeof(char));
if(pChar != NULL) {
pChar[0] = caractere;
pChar[1] = '\0';
}
return pChar;
}
int nbCaracteresChaine(Word chaine) {
int i = 0;
while(chaine[i] != '\0') {
i++;
}
return i;
}
Motif* formaterMotif(Word chaineMotif, int longueurMot) {
//nwlr(nqxb)bm(dgqw)bh
Motif *motif = (Motif *) malloc(sizeof(Motif));
motif->tokens = (Word *) malloc(longueurMot*sizeof(Word*));
motif->size = longueurMot;
int i, j = 0;
int lengthChaineMotif = nbCaracteresChaine(chaineMotif);
bool concat = false;
Word caracteresPossibles = "";
for(i = 0; i < lengthChaineMotif; i++) {
if(chaineMotif[i] == '(') {
printf("start concat\n");
concat = true;
caracteresPossibles = "";
}
else if(chaineMotif[i] == ')') {
printf("end concat\n");
concat = false;
motif->tokens[j] = caracteresPossibles;
j++;
}
else {
if(concat) {
//concaténation
//stockage de la chaine générée dans une case token du motif
printf("concat en cours\n");
caracteresPossibles = concatenate(caracteresPossibles, char2str(chaineMotif[i]));
}
else {
//conversion du caractère en chaine de caractère
printf("%s\n", char2str(chaineMotif[i]));
motif->tokens[j] = char2str(chaineMotif[i]);
//incrémentation du compteur lié au motif
j++;
}
}
}
//printf("%i\n", motif->size);
return motif;
}
void copierChaine(Word chaine, Word chaineTransfert) {
int length = strlen(chaineTransfert);
int i;
for (i = 0; i < length; i++) {
chaine[i] = chaineTransfert[i];
}
}
// Lecture du fichier et création du dictionnaire
// La fonction fait un malloc pour allouer l'espace memoire
Dictionnaire* readDicoFromFile(FILE* dataFile, unsigned int nbWords, unsigned int wordSize) {
//allocation mémoire
Dictionnaire *dico = (Dictionnaire *) malloc(sizeof(Dictionnaire));
dico->words = (Word *) malloc(nbWords*sizeof(Word*));
dico->size = nbWords;
char mot[wordSize];
//lecture fichier
//remplissage du dictionnaire
printf("starting\n");
int i;
for (i = 0; i < dico->size; i++) {
//printf("entree boucle");
fscanf(dataFile,"%s\n", mot);
dico->words[i] = mot;
copierChaine(dico->words[i], mot);
}
printf("sortie boucle\n");
afficherDico(dico);
//printf("%s\n", dico->words[1]);
return dico;
}
// Déallocation du dictionnaire
void detruire_dico(Dictionnaire* dico) {}
// Allocation en mémoire d'un tableau contenant les motifs de test
MotifTableau* readMotifTableauFromFile(FILE* dataFile, unsigned int nbTestCases, unsigned int wordSize) {
//allocation mémoire
MotifTableau *motifsTab = (MotifTableau *) malloc(sizeof(MotifTableau));
motifsTab->size = nbTestCases;
//lecture fichier
//remplissage du tableau motif
Word chaineMotif;
int i;
for (i = 0; i < motifsTab->size; i++) {
fscanf(dataFile,"%s\n", chaineMotif);
//formatage du motif
//motif est un tableau de chaine de caractère
Motif *motif = formaterMotif(chaineMotif, wordSize);
motifsTab->motifs[i] = *motif;
}
return motifsTab;
}
// Déallocation du tableau de motif
void detruire_motifs(MotifTableau* motifsTab) {}
//La fonction renvoie true si la chaine de caractere contient le caractere passé en paramètre
bool chaineContientCaractere(Word chaine, int length, char caractere) {
bool contient = false;
int i;
for(i = 0; i < length; i++) {
if(chaine[i] == caractere) {
return true;
}
}
return contient;
}
//La fonction renvoie true si il y a une correspondance entre le mot et le motif
bool correspondance(Word mot, Motif motif) {
bool correspondance = true;
int i;
for(i = 0; i < motif.size; i++) {
if(!chaineContientCaractere(motif.tokens[i], motif.size, mot[i])) {
correspondance = false;
return correspondance;
}
}
return correspondance;
}
//affichage du nombre de mot du dictionnaire qui correspondent au motif pour chaque test
void afficherNbMotsCorrespondantAuxMotifs(Dictionnaire* dico, MotifTableau* motifTab) {
int nbMotsCorrespondants, i, j;
//Pour chaque mot du dico on regarde si il y a une correspondance entre ce mot et des tests
for(i = 0; i < motifTab->size; i++) {
nbMotsCorrespondants = 0;
for(j = 0; j < dico->size; j++) {
if(correspondance(dico->words[j], motifTab->motifs[i])) {
nbMotsCorrespondants++;
}
}
printf("Case #%i: %i", i, nbMotsCorrespondants);
}
}
int main(int argc, char *argv[]){
char *filename = "A-small-practice.in";
FILE* dataFile;
if( argc > 1)
filename = argv[1];
// open file
dataFile = fopen(filename,"r");
if( dataFile == NULL ) {
printf("Error opening file %s : %d (%s)\n",filename,errno,strerror(errno));
exit(1);
}
// read file header
unsigned int wordSize, nbWords, nbTestCases;
fscanf(dataFile,"%u %u %u\n", &wordSize, &nbWords, &nbTestCases);
printf("%u %u %u\n", wordSize, nbWords, nbTestCases);
//Word chaineMotif = "nwlr(nqxb)bm(dgqw)bh";
//Motif *motif = formaterMotif(chaineMotif, 10);
//afficherMotif(motif);
//création du dictionnaire
Dictionnaire *dico = readDicoFromFile(dataFile, nbWords, wordSize);
//afficherDico(dico);
printf("%s", dico->words[1]);
//création d'un tableau de motif
//MotifTableau *motifTab = readMotifTableauFromFile(dataFile, nbTestCases, wordSize);
//affichage du nombre de mot du dictionnaire qui correspondent au motif pour chaque test
//afficherNbMotsCorrespondantAuxMotifs(dico, motifTab);
//detruire_dico(dico);
//detruire_motifs(motifTab);
// close file
fclose(dataFile);
return EXIT_SUCCESS;
}
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment