Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> /* pour les entrées-sorties */
- #include <string.h> /* pour les manipulations de chaînes de caractères */
- #include <conio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include "rep.h"
- #define VERSION 2.2
- #define SQUELETTE
- #define nomFichier "rep.txt"
- /**************************************************************************/
- /* Compléter votre nom ici */
- /* Nom : HUTIN Prénom : Florian */
- /**************************************************************************/
- extern bool modif;
- /**********************************************************************/
- /* Ajout d'un contact dans le répertoire stocké en mémoire */
- /**********************************************************************/
- int ajouter_un_contact_dans_rep(Repertoire *rep, Enregistrement enr)
- {
- #ifdef IMPL_TAB
- int idx;
- if (rep->nb_elts < MAX_ENREG)
- {
- idx = rep->nb_elts;
- // ajouter code ici pour tableau
- // **** début ajout ****
- rep->tab[idx] = enr;
- rep->nb_elts++;
- // **** fin ajout ****
- modif = true;
- rep->est_trie = false;
- }
- else {
- return(ERROR);
- }
- #else
- #ifdef IMPL_LIST
- // ajouter code ici pour Liste
- bool inserted = false;
- if (rep->nb_elts == 0) {
- if (InsertElementAt(rep->liste, rep->liste->size, enr) != 0) {
- rep->nb_elts += 1;
- modif = true;
- rep->est_trie = true;
- return(OK);
- }
- }
- else {
- SingleLinkedListElem *current = GetElementAt(rep->liste, 0);
- int pos = 0;
- // on cherche un point d'insertion
- while (!inserted && current != NULL) {
- // si l'élément qu'on veut insérer est plus petit que l'élément courant,
- // on l'insère dans la liste chaînée
- if (!est_sup(enr, current->pers)) {
- InsertElementAt(rep->liste, pos, enr);
- inserted = true;
- rep->nb_elts++;
- rep->est_trie = true;
- }
- else
- {
- // sinon, on passe au suivant
- current = current->next;
- pos++;
- }
- }
- // c'est le plus grand, on le place à la fin
- if (!inserted) {
- // **** début ajout ****
- // **** fin ajout ****
- }
- }
- #endif
- #endif
- return(OK);
- } /* fin ajout */
- /**********************************************************************/
- /* supprime du répertoire l'enregistrement dont l'indice est donné en */
- /* paramètre et place modif = true */
- /**********************************************************************/
- #ifdef IMPL_TAB
- void supprimer_un_contact_dans_rep(Repertoire *rep, int indice) {
- // ajouter code ici pour tableau
- // **** début ajout ****
- for (int i = indice; i < MAX_ENREG; i++) {
- rep->tab[i] = rep->tab[i + 1];
- }
- rep->nb_elts--;
- modif = true;
- // **** fin ajout ****
- return;
- } /* fin supprimer */
- #else
- #ifdef IMPL_LIST
- /************************************************************************/
- /* supprime du répertoire l'enregistrement contenu dans le maillon elem */
- /* et fixe modif à vrai */
- /************************************************************************/
- // ajouter code ici pour Liste
- int supprimer_un_contact_dans_rep_liste(Repertoire *rep, SingleLinkedListElem *elem) {
- // **** début ajout ****
- // **** fin ajout ****
- return (0);
- }
- #endif
- #endif
- /**********************************************************************/
- /* fonction d'affichage d'un enregistrement sur une ligne à l'écran */
- /* ex Dupont, Jean 0320304050 */
- /**********************************************************************/
- void affichage_enreg(Enregistrement enr)
- {
- // **** début ajout ****
- printf("%-30s,%-30s\t\t %-20s\n\n", enr.nom, enr.prenom, enr.tel);
- // **** fin ajout ****
- } /* fin affichage_enreg */
- /**********************************************************************/
- /* fonction d'affichage d'un enregistrement avec alignement */
- /* pour les listes */
- /* ex | Dupont |Jean |0320304050 */
- /**********************************************************************/
- void affichage_enreg_frmt(Enregistrement enr)
- {
- // **** début ajout ****
- // **** Affichage du nom ****
- printf("|");
- printf("%-30s", enr.nom);
- // **** affichage prenom ****
- printf("|");
- printf("%-30s", enr.prenom);
- // **** affichage telephone ****
- printf("|");
- printf("%-20s", enr.tel);
- printf("\n");
- // **** fin ajout ****
- } /* fin affichage_enreg */
- /**********************************************************************/
- /* test si dans l'ordre alphabetique, un enregistrement est apres */
- /* un autre */
- /**********************************************************************/
- bool est_sup(Enregistrement enr1, Enregistrement enr2)
- {
- int test;
- test = _stricmp(enr1.nom, enr2.nom);
- // **** début ajout ****
- if (test > 0) {
- return(true);
- }
- // **** fin ajout ****
- return(false);
- }
- /*********************************************************************/
- /* Tri Alphabetique du tableau d'enregistrements */
- /*********************************************************************/
- void trier(Repertoire *rep)
- {
- #ifdef IMPL_TAB
- // ajouter code ici pour tableau
- //int idx1;
- //int idx2;
- bool permute = true;
- Enregistrement tmp_enr;
- if (rep->est_trie) return; /* on suppose que c'est vrai ! */
- // **** début ajout ****
- else {
- for (int i = 0; i < MAX_ENREG; i++) {
- if (est_sup(rep->tab[i], rep->tab[i + 1])) {
- tmp_enr = rep->tab[i];
- rep->tab[i] = rep->tab[i + 1];
- rep->tab[i + 1] = tmp_enr;
- }
- }
- }
- // **** fin ajout ****
- #else
- #ifdef IMPL_LIST
- // ajouter code ici pour Liste
- // rien à faire !
- // la liste est toujours triée
- #endif
- #endif
- rep->est_trie = true;
- } /* fin trier */
- /***********************************************************************/
- /* recherche dans le répertoire un enregistrement correspondant au */
- /* nom, à partir de l'indice ind */
- /* retourne l'indice de l'enregistrement correspondant au critère ou */
- /* un entier négatif si la recherche est négative */
- /***********************************************************************/
- int rechercher_nom(Repertoire *rep, char nom[], int ind)
- {
- int i = ind; /* position (indice) de début de recherche dans tableau/liste rep */
- int ind_fin; /* position (indice) de fin de tableau/liste rep */
- char tmp_nom[MAX_NOM]; /* 2 variables temporaires dans lesquelles */
- char tmp_nom2[MAX_NOM]; /* on place la chaine recherchée et la chaine lue dans le */
- /* tableau, afin de les convertir en majuscules et les comparer */
- bool trouve = false;
- #ifdef IMPL_TAB
- // ajouter code ici pour tableau
- ind_fin = rep->nb_elts - 1;
- strcpy_s(tmp_nom, _countof(tmp_nom), nom);
- _strupr_s(tmp_nom, _countof(tmp_nom));
- // **** début ajout ****
- for (i; i <= ind_fin; i++) {
- strcpy_s(tmp_nom2, _countof(tmp_nom2), rep->tab[i].nom);
- _strupr_s(tmp_nom2, _countof(tmp_nom2));
- if (_stricmp(tmp_nom2, tmp_nom) == 0) {
- return i;
- }
- }
- return -1;
- // **** fin ajout ****
- #else
- #ifdef IMPL_LIST
- // ajouter code ici pour Liste
- // on se place sur l'élément en ième position s'il existe
- SingleLinkedListElem *currentElement = GetElementAt(rep->liste, i);
- // conversion du nom recherché en majuscule pour faciliter la comparaison
- strcpy_s(tmp_nom, _countof(tmp_nom), nom);
- _strupr_s(tmp_nom, _countof(tmp_nom));
- // **** début ajout ****
- // **** fin ajout ****
- #endif
- #endif
- return((trouve) ? i : -1);
- } /* fin rechercher_nom */
- /*********************************************************************/
- /* Supprimer tous les caracteres non numériques de la chaines */
- /*********************************************************************/
- void compact(char *s)
- {
- for (int i = 0; i < MAX_TEL; i++) {
- if (*(s + i) == ' ' || *(s + i) == '-') {
- for (int j = i; j < MAX_TEL; j++) {
- *(s + j) = *(s + j + 1);
- }
- }
- }
- return;
- }
- /**********************************************************************/
- /* sauvegarde le répertoire dans le fichier dont le nom est passé en */
- /* argument */
- /* retourne OK si la sauvegarde a fonctionné ou ERROR sinon */
- /**********************************************************************/
- int sauvegarder(Repertoire *rep, char nom_fichier[])
- {
- FILE *fic_rep; /* le fichier */
- #ifdef IMPL_TAB
- // ajouter code ici pour tableau
- // **** début ajout ****
- int openSucces = fopen_s(&fic_rep, nom_fichier, "w");
- if (fic_rep == NULL) {
- printf("Erreur lors de l'ouverture");
- system("pause");
- }
- char *tmp = (char*)malloc(sizeof(Enregistrement));
- for (int i = 0; i < rep->nb_elts; i++)
- {
- sprintf_s(tmp, MAX_NOM + MAX_NOM + MAX_TEL + sizeof(char) * 2, "%s%c%s%c%s%c\n",
- rep->tab[i].nom,
- SEPARATEUR,
- rep->tab[i].prenom,
- SEPARATEUR,
- rep->tab[i].tel,
- SEPARATEUR);
- fputs(tmp, fic_rep);
- }
- fclose(fic_rep);
- // **** fin ajout ****
- #else
- #ifdef IMPL_LIST
- // ajouter code ici pour Liste
- #endif
- #endif
- return(OK);
- } /* fin sauvegarder */
- /**********************************************************************/
- /* charge dans le répertoire le contenu du fichier dont le nom est */
- /* passé en argument */
- /* retourne OK si le chargement a fonctionné et ERROR sinon */
- /**********************************************************************/
- int charger(Repertoire *rep, char nom_fichier[])
- {
- FILE *fic_rep; /* le fichier */
- errno_t err;
- int num_rec = 0; /* index sur enregistrements */
- int long_max_rec = sizeof(Enregistrement);
- char buffer[sizeof(Enregistrement) + 1];
- int idx = 0;
- char *char_nw_line;
- _set_errno(0);
- if ((err = fopen_s(&fic_rep, nom_fichier, "r")) != 0)
- {
- return(err);
- }
- else
- {
- while (!feof(fic_rep) && (rep->nb_elts < MAX_ENREG))
- {
- if (fgets(buffer, long_max_rec, fic_rep) != NULL)
- {
- /* memorisation de l'enregistrement lu dans le tableau */
- buffer[long_max_rec] = 0; /* en principe il y a deja un fin_de_chaine, cf fgets */
- if ((char_nw_line = strchr(buffer, '\n')) != NULL)
- *char_nw_line = '\0'; /* suppression du fin_de_ligne eventuel */
- idx = 0; /* analyse depuis le debut de la ligne */
- #ifdef IMPL_TAB
- if (lire_champ_suivant(buffer, &idx, rep->tab[num_rec].nom, MAX_NOM, SEPARATEUR) == OK)
- {
- idx++; /* on saute le separateur */
- if (lire_champ_suivant(buffer, &idx, rep->tab[num_rec].prenom, MAX_NOM, SEPARATEUR) == OK)
- {
- idx++;
- if (lire_champ_suivant(buffer, &idx, rep->tab[num_rec].tel, MAX_TEL, SEPARATEUR) == OK)
- num_rec++; /* element à priori correct, on le comptabilise */
- }
- }
- #else
- #ifdef IMPL_LIST
- // ajouter code implemention liste
- // **** début ajout ****
- // **** fin ajout ****
- #endif
- #endif
- }
- }
- rep->nb_elts = num_rec;
- fclose(fic_rep);
- return(OK);
- }
- } /* fin charger */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement