Advertisement
Guest User

TP9.c

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