Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.55 KB | None | 0 0
  1. #include <stdio.h> /* pour les entrées-sorties */
  2. #include <string.h> /* pour les manipulations de chaînes de caractères */
  3. #include <conio.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. #include "rep.h"
  7. #define VERSION 2.2
  8. #define SQUELETTE
  9. #define nomFichier "rep.txt"
  10. /**************************************************************************/
  11. /* Compléter votre nom ici */
  12. /* Nom : HUTIN Prénom : Florian */
  13. /**************************************************************************/
  14.  
  15. extern bool modif;
  16.  
  17.  
  18. /**********************************************************************/
  19. /* Ajout d'un contact dans le répertoire stocké en mémoire */
  20. /**********************************************************************/
  21.  
  22. int ajouter_un_contact_dans_rep(Repertoire *rep, Enregistrement enr)
  23. {
  24. #ifdef IMPL_TAB
  25.  
  26. int idx;
  27.  
  28. if (rep->nb_elts < MAX_ENREG)
  29. {
  30. idx = rep->nb_elts;
  31. // ajouter code ici pour tableau
  32. // **** début ajout ****
  33. rep->tab[idx] = enr;
  34. rep->nb_elts++;
  35.  
  36.  
  37.  
  38. // **** fin ajout ****
  39. modif = true;
  40. rep->est_trie = false;
  41. }
  42. else {
  43. return(ERROR);
  44. }
  45.  
  46.  
  47. #else
  48. #ifdef IMPL_LIST
  49. // ajouter code ici pour Liste
  50. bool inserted = false;
  51. if (rep->nb_elts == 0) {
  52. if (InsertElementAt(rep->liste, rep->liste->size, enr) != 0) {
  53. rep->nb_elts += 1;
  54. modif = true;
  55. rep->est_trie = true;
  56. return(OK);
  57. }
  58.  
  59. }
  60. else {
  61. SingleLinkedListElem *current = GetElementAt(rep->liste, 0);
  62. int pos = 0;
  63. // on cherche un point d'insertion
  64. while (!inserted && current != NULL) {
  65. // si l'élément qu'on veut insérer est plus petit que l'élément courant,
  66. // on l'insère dans la liste chaînée
  67. if (!est_sup(enr, current->pers)) {
  68. InsertElementAt(rep->liste, pos, enr);
  69. inserted = true;
  70. rep->nb_elts++;
  71. rep->est_trie = true;
  72. }
  73. else
  74. {
  75. // sinon, on passe au suivant
  76. current = current->next;
  77. pos++;
  78.  
  79. }
  80.  
  81. }
  82. // c'est le plus grand, on le place à la fin
  83. if (!inserted) {
  84. // **** début ajout ****
  85.  
  86.  
  87.  
  88. // **** fin ajout ****
  89.  
  90.  
  91. }
  92. }
  93.  
  94.  
  95. #endif
  96.  
  97. #endif
  98.  
  99.  
  100. return(OK);
  101.  
  102. } /* fin ajout */
  103. /**********************************************************************/
  104. /* supprime du répertoire l'enregistrement dont l'indice est donné en */
  105. /* paramètre et place modif = true */
  106. /**********************************************************************/
  107. #ifdef IMPL_TAB
  108. void supprimer_un_contact_dans_rep(Repertoire *rep, int indice) {
  109.  
  110. // ajouter code ici pour tableau
  111. // **** début ajout ****
  112. for (int i = indice; i < MAX_ENREG; i++) {
  113. rep->tab[i] = rep->tab[i + 1];
  114. }
  115. rep->nb_elts--;
  116. modif = true;
  117.  
  118. // **** fin ajout ****
  119.  
  120. return;
  121. } /* fin supprimer */
  122.  
  123. #else
  124. #ifdef IMPL_LIST
  125. /************************************************************************/
  126. /* supprime du répertoire l'enregistrement contenu dans le maillon elem */
  127. /* et fixe modif à vrai */
  128. /************************************************************************/
  129. // ajouter code ici pour Liste
  130.  
  131. int supprimer_un_contact_dans_rep_liste(Repertoire *rep, SingleLinkedListElem *elem) {
  132.  
  133. // **** début ajout ****
  134.  
  135.  
  136.  
  137. // **** fin ajout ****
  138.  
  139. return (0);
  140. }
  141. #endif
  142. #endif
  143.  
  144.  
  145. /**********************************************************************/
  146. /* fonction d'affichage d'un enregistrement sur une ligne à l'écran */
  147. /* ex Dupont, Jean 0320304050 */
  148. /**********************************************************************/
  149. void affichage_enreg(Enregistrement enr)
  150. {
  151. // **** début ajout ****
  152. printf("%-30s,%-30s\t\t %-20s\n\n", enr.nom, enr.prenom, enr.tel);
  153. // **** fin ajout ****
  154.  
  155. } /* fin affichage_enreg */
  156. /**********************************************************************/
  157. /* fonction d'affichage d'un enregistrement avec alignement */
  158. /* pour les listes */
  159. /* ex | Dupont |Jean |0320304050 */
  160. /**********************************************************************/
  161. void affichage_enreg_frmt(Enregistrement enr)
  162. {
  163. // **** début ajout ****
  164. // **** Affichage du nom ****
  165. printf("|");
  166. printf("%-30s", enr.nom);
  167. // **** affichage prenom ****
  168. printf("|");
  169. printf("%-30s", enr.prenom);
  170. // **** affichage telephone ****
  171. printf("|");
  172. printf("%-20s", enr.tel);
  173.  
  174. printf("\n");
  175. // **** fin ajout ****
  176.  
  177. } /* fin affichage_enreg */
  178.  
  179.  
  180. /**********************************************************************/
  181. /* test si dans l'ordre alphabetique, un enregistrement est apres */
  182. /* un autre */
  183. /**********************************************************************/
  184. bool est_sup(Enregistrement enr1, Enregistrement enr2)
  185. {
  186.  
  187. int test;
  188.  
  189. test = _stricmp(enr1.nom, enr2.nom);
  190. // **** début ajout ****
  191. if (test > 0) {
  192. return(true);
  193. }
  194.  
  195.  
  196. // **** fin ajout ****
  197.  
  198. return(false);
  199.  
  200. }
  201.  
  202. /*********************************************************************/
  203. /* Tri Alphabetique du tableau d'enregistrements */
  204. /*********************************************************************/
  205.  
  206. void trier(Repertoire *rep)
  207. {
  208.  
  209. #ifdef IMPL_TAB
  210. // ajouter code ici pour tableau
  211. //int idx1;
  212. //int idx2;
  213. bool permute = true;
  214. Enregistrement tmp_enr;
  215.  
  216.  
  217. if (rep->est_trie) return; /* on suppose que c'est vrai ! */
  218.  
  219. // **** début ajout ****
  220. else {
  221. for (int i = 0; i < MAX_ENREG; i++) {
  222. if (est_sup(rep->tab[i], rep->tab[i + 1])) {
  223. tmp_enr = rep->tab[i];
  224. rep->tab[i] = rep->tab[i + 1];
  225. rep->tab[i + 1] = tmp_enr;
  226. }
  227. }
  228. }
  229.  
  230.  
  231. // **** fin ajout ****
  232.  
  233. #else
  234. #ifdef IMPL_LIST
  235. // ajouter code ici pour Liste
  236. // rien à faire !
  237. // la liste est toujours triée
  238. #endif
  239. #endif
  240.  
  241.  
  242. rep->est_trie = true;
  243.  
  244. } /* fin trier */
  245.  
  246. /***********************************************************************/
  247. /* recherche dans le répertoire un enregistrement correspondant au */
  248. /* nom, à partir de l'indice ind */
  249. /* retourne l'indice de l'enregistrement correspondant au critère ou */
  250. /* un entier négatif si la recherche est négative */
  251. /***********************************************************************/
  252.  
  253. int rechercher_nom(Repertoire *rep, char nom[], int ind)
  254. {
  255. int i = ind; /* position (indice) de début de recherche dans tableau/liste rep */
  256. int ind_fin; /* position (indice) de fin de tableau/liste rep */
  257.  
  258. char tmp_nom[MAX_NOM]; /* 2 variables temporaires dans lesquelles */
  259. char tmp_nom2[MAX_NOM]; /* on place la chaine recherchée et la chaine lue dans le */
  260. /* tableau, afin de les convertir en majuscules et les comparer */
  261. bool trouve = false;
  262.  
  263.  
  264. #ifdef IMPL_TAB
  265. // ajouter code ici pour tableau
  266. ind_fin = rep->nb_elts - 1;
  267. strcpy_s(tmp_nom, _countof(tmp_nom), nom);
  268. _strupr_s(tmp_nom, _countof(tmp_nom));
  269.  
  270. // **** début ajout ****
  271. for (i; i <= ind_fin; i++) {
  272. strcpy_s(tmp_nom2, _countof(tmp_nom2), rep->tab[i].nom);
  273. _strupr_s(tmp_nom2, _countof(tmp_nom2));
  274. if (_stricmp(tmp_nom2, tmp_nom) == 0) {
  275. return i;
  276. }
  277. }
  278. return -1;
  279.  
  280.  
  281. // **** fin ajout ****
  282. #else
  283. #ifdef IMPL_LIST
  284. // ajouter code ici pour Liste
  285. // on se place sur l'élément en ième position s'il existe
  286.  
  287. SingleLinkedListElem *currentElement = GetElementAt(rep->liste, i);
  288.  
  289. // conversion du nom recherché en majuscule pour faciliter la comparaison
  290. strcpy_s(tmp_nom, _countof(tmp_nom), nom);
  291. _strupr_s(tmp_nom, _countof(tmp_nom));
  292.  
  293. // **** début ajout ****
  294.  
  295.  
  296.  
  297. // **** fin ajout ****
  298.  
  299. #endif
  300. #endif
  301.  
  302. return((trouve) ? i : -1);
  303. } /* fin rechercher_nom */
  304.  
  305. /*********************************************************************/
  306. /* Supprimer tous les caracteres non numériques de la chaines */
  307. /*********************************************************************/
  308. void compact(char *s)
  309. {
  310. for (int i = 0; i < MAX_TEL; i++) {
  311. if (*(s + i) == ' ' || *(s + i) == '-') {
  312. for (int j = i; j < MAX_TEL; j++) {
  313. *(s + j) = *(s + j + 1);
  314. }
  315. }
  316. }
  317.  
  318. return;
  319. }
  320.  
  321. /**********************************************************************/
  322. /* sauvegarde le répertoire dans le fichier dont le nom est passé en */
  323. /* argument */
  324. /* retourne OK si la sauvegarde a fonctionné ou ERROR sinon */
  325. /**********************************************************************/
  326. int sauvegarder(Repertoire *rep, char nom_fichier[])
  327. {
  328. FILE *fic_rep; /* le fichier */
  329. #ifdef IMPL_TAB
  330. // ajouter code ici pour tableau
  331. // **** début ajout ****
  332. int openSucces = fopen_s(&fic_rep, nom_fichier, "w");
  333. if (fic_rep == NULL) {
  334. printf("Erreur lors de l'ouverture");
  335. system("pause");
  336. }
  337. char *tmp = (char*)malloc(sizeof(Enregistrement));
  338. for (int i = 0; i < rep->nb_elts; i++)
  339. {
  340. sprintf_s(tmp, MAX_NOM + MAX_NOM + MAX_TEL + sizeof(char) * 2, "%s%c%s%c%s%c\n",
  341. rep->tab[i].nom,
  342. SEPARATEUR,
  343. rep->tab[i].prenom,
  344. SEPARATEUR,
  345. rep->tab[i].tel,
  346. SEPARATEUR);
  347. fputs(tmp, fic_rep);
  348.  
  349. }
  350. fclose(fic_rep);
  351.  
  352. // **** fin ajout ****
  353. #else
  354. #ifdef IMPL_LIST
  355. // ajouter code ici pour Liste
  356. #endif
  357. #endif
  358.  
  359. return(OK);
  360. } /* fin sauvegarder */
  361.  
  362.  
  363. /**********************************************************************/
  364. /* charge dans le répertoire le contenu du fichier dont le nom est */
  365. /* passé en argument */
  366. /* retourne OK si le chargement a fonctionné et ERROR sinon */
  367. /**********************************************************************/
  368.  
  369. int charger(Repertoire *rep, char nom_fichier[])
  370. {
  371. FILE *fic_rep; /* le fichier */
  372. errno_t err;
  373. int num_rec = 0; /* index sur enregistrements */
  374. int long_max_rec = sizeof(Enregistrement);
  375. char buffer[sizeof(Enregistrement) + 1];
  376. int idx = 0;
  377.  
  378. char *char_nw_line;
  379.  
  380. _set_errno(0);
  381. if ((err = fopen_s(&fic_rep, nom_fichier, "r")) != 0)
  382. {
  383. return(err);
  384. }
  385. else
  386. {
  387. while (!feof(fic_rep) && (rep->nb_elts < MAX_ENREG))
  388. {
  389. if (fgets(buffer, long_max_rec, fic_rep) != NULL)
  390. {
  391. /* memorisation de l'enregistrement lu dans le tableau */
  392. buffer[long_max_rec] = 0; /* en principe il y a deja un fin_de_chaine, cf fgets */
  393.  
  394. if ((char_nw_line = strchr(buffer, '\n')) != NULL)
  395. *char_nw_line = '\0'; /* suppression du fin_de_ligne eventuel */
  396.  
  397. idx = 0; /* analyse depuis le debut de la ligne */
  398. #ifdef IMPL_TAB
  399. if (lire_champ_suivant(buffer, &idx, rep->tab[num_rec].nom, MAX_NOM, SEPARATEUR) == OK)
  400. {
  401. idx++; /* on saute le separateur */
  402. if (lire_champ_suivant(buffer, &idx, rep->tab[num_rec].prenom, MAX_NOM, SEPARATEUR) == OK)
  403. {
  404. idx++;
  405. if (lire_champ_suivant(buffer, &idx, rep->tab[num_rec].tel, MAX_TEL, SEPARATEUR) == OK)
  406. num_rec++; /* element à priori correct, on le comptabilise */
  407. }
  408. }
  409. #else
  410. #ifdef IMPL_LIST
  411. // ajouter code implemention liste
  412. // **** début ajout ****
  413.  
  414.  
  415.  
  416. // **** fin ajout ****
  417. #endif
  418. #endif
  419.  
  420.  
  421.  
  422.  
  423. }
  424.  
  425. }
  426. rep->nb_elts = num_rec;
  427. fclose(fic_rep);
  428. return(OK);
  429. }
  430.  
  431.  
  432. } /* fin charger */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement