Advertisement
Guest User

TP9.c

a guest
Apr 9th, 2020
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define taille 20
  5. typedef struct contact
  6. {
  7.     char Nom[100];
  8.     char Prenom[100];
  9.     char Sex;
  10.     char Num_tel_fix[100];
  11.     char Num_tel_portable[100];
  12.     struct contact* conjoint; // suivant
  13. }contact;
  14.  
  15. typedef struct carnet{
  16.     contact** contacts;
  17.     int size;       // taille effective
  18. } carnet;
  19.  
  20.  
  21.  
  22. void afficher_info_contact(contact* cont){
  23.     printf(" Nom: %s\n  Prenom: %s\n  Sex: %c\n  Numéro de téléphone fixe: %s\n  Numéro de téléphone portable: %s\n",
  24.             cont->Nom, cont->Prenom, cont->Sex, cont->Num_tel_fix, cont->Num_tel_portable);
  25. }
  26.  
  27. void create_contact(contact* cont ){
  28.     printf("Nom :");
  29.     scanf(" %s",cont->Nom);
  30.     printf("Prenom :");
  31.     scanf(" %s",cont->Prenom);
  32.     printf("Sex :");
  33.     scanf(" %c",&cont->Sex);
  34.     printf("Numéro de téléphone fixe:");
  35.     scanf(" %s",cont->Num_tel_fix);
  36.     printf("Numéro de téléphone portable:");
  37.     scanf(" %s",cont->Num_tel_portable);
  38.     return;
  39. }
  40.  
  41. void ajout_contact(carnet carnet){
  42.     int nb;
  43.     printf("Combien de contact voudriez vous ajouter ? \n");
  44.     scanf(" %d", &nb);
  45.     while ((nb < 0) || (nb > carnet.size))
  46.     {
  47.         printf("erreur vous ne pouvez pas ajouter plus de %d contact", carnet.size);
  48.         printf("Combien de contact voudriez vous ajouter ? \n");
  49.         scanf(" %d", &nb);
  50.     }
  51.     for (int i = 0; i < nb; i++)
  52.     {  
  53.         create_contact(carnet.contacts[i]);
  54.     }
  55.     carnet.size = nb;
  56. }
  57.  
  58. int test_sous_string(char mot1[], char mot2[]){
  59.     for (int i = 0; mot2[i] != '\0'; i++)
  60.     {  
  61.         if (mot1[i] != mot2[i])
  62.         {
  63.             return 0;
  64.         }
  65.     }
  66.     return 1;
  67. }
  68.  
  69. int test_string(char mot1[], char mot2[]){
  70.     for (int i = 0; mot2[i] != '\0' || mot1[i] != '\0'; i++)
  71.     {
  72.         if (mot1[i] == '\0')
  73.         {
  74.             return 0;
  75.         }
  76.        
  77.         if (mot1[i] != mot2[i])
  78.         {
  79.             return 0;
  80.         }
  81.     }
  82.     return 1;
  83. }
  84.  
  85. void recherche_contact(carnet carnet , char str[]){
  86.  
  87.     printf("voici les contacts qui commence par %s", str);
  88.     for (int i = 0; i < carnet.size; i++)
  89.     {
  90.         if (test_sous_string(carnet.contacts[i]->Nom , str))
  91.         {
  92.             afficher_info_contact(carnet.contacts[i]);
  93.         }
  94.     }  
  95. }
  96.  
  97. void specification_conjoint(carnet carnet){
  98. // les conjoint sont sous cette forme: a->b b->a c->d ...
  99. // le conjoint de a c'est b , le conjoint de b c'est a etc...  :) un peut bizarre mas bon !
  100.     for (int i = 1; i <= carnet.size - 1; i+=2)
  101.     {
  102.         carnet.contacts[i]->conjoint = carnet.contacts[i-1];
  103.         carnet.contacts[i-1]->conjoint = carnet.contacts[i];
  104.     }
  105.    
  106. }
  107.  
  108. void recherche_contact_par_nom_conj(carnet carnet , char str[] , contact* conjoint){
  109.     for (int i = 0; i < carnet.size; i++)
  110.     {
  111.         if (test_string(carnet.contacts[i]->Nom , str))
  112.         {
  113.             afficher_info_contact(carnet.contacts[i]);
  114.         }
  115.        
  116.         if (carnet.contacts[i]->conjoint == conjoint)
  117.         {
  118.             afficher_info_contact(carnet.contacts[i]);
  119.         }
  120.    
  121.     }
  122. }
  123.  
  124.  
  125.  
  126. int main(){
  127.     carnet carnet;
  128.     carnet.contacts = NULL;
  129.     carnet.contacts = (contact**)malloc(sizeof(contact*) * taille);
  130.  
  131.     // création du tableau de contact (carnet)
  132.     for (int i = 0; i < taille; i++)
  133.     {
  134.         carnet.contacts[i] = (contact*)malloc(sizeof(contact));
  135.     }
  136.  
  137.     // ajout contact
  138.     ajout_contact(carnet);
  139.  
  140.     // recherche contact
  141.     recherche_contact(carnet , "string");
  142.  
  143.     // spécification de conjoint
  144.     specification_conjoint(carnet);
  145.  
  146.     int i = 3; // par exemple
  147.  
  148.     // recherche contact par nom ou conjoint
  149.     recherche_contact_par_nom_conj(carnet , "string" , carnet.contacts[i]);
  150.  
  151.  
  152.     // restituer la mémoire au système
  153.     for (int i = 0; i < taille; i++)
  154.     {
  155.         free(carnet.contacts[i]);
  156.     }
  157.     free(carnet.contacts);
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement