Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct _contact {
  5.     const char *prenom;
  6.     const char *nom;
  7.     const char *adresse;
  8. } contact;
  9.  
  10. typedef struct _carnet {
  11.     contact *contacts;
  12.     size_t capacity;
  13.     int count;
  14. } carnet;
  15.  
  16. void extend_carnet (carnet *crt, unsigned int s);
  17.  
  18. carnet new_carnet (void) {
  19.     carnet crt;
  20.     crt.contacts = NULL;
  21.     crt.capacity = 0;
  22.     crt.count = 0;
  23.     extend_carnet (&crt, 3);
  24.     return crt;
  25. }
  26.  
  27. void delete_carnet (carnet *crt) {
  28.     if (crt->contacts != NULL) {
  29.         free (crt->contacts);
  30.         crt->contacts = NULL;
  31.     }
  32.  
  33.     crt->capacity = 0;
  34.     crt->count = 0;
  35. }
  36.  
  37. void extend_carnet (carnet *crt, unsigned int s) {
  38.     crt->capacity += s;
  39.     crt->contacts = realloc (crt->contacts,
  40.         crt->capacity*sizeof (*crt->contacts));
  41. }
  42.  
  43. void show_carnet (const carnet *crt) {
  44.     int i;
  45.     for (i=0; i < crt->count; i++) {
  46.         printf ("Contact nĀ°%i : %s\n", i+1,
  47.             crt->contacts[i].prenom);  
  48.     }
  49. }
  50.  
  51. void add_contact (carnet *crt, contact cnt) {
  52.     if (crt->capacity == crt->count) {
  53.         extend_carnet (crt, 3);
  54.     }
  55.    
  56.     crt->contacts[crt->count] = cnt;
  57.     crt->count++;      
  58. }
  59.  
  60. int main (int argc, char **argv) {
  61.     carnet crt = new_carnet ();
  62.     contact steven;
  63.     steven.prenom = "StevenDeo";
  64.     add_contact (&crt, steven);
  65.     add_contact (&crt, steven);
  66.     add_contact (&crt, steven);
  67.     add_contact (&crt, steven);
  68.     add_contact (&crt, steven);
  69.     add_contact (&crt, steven);
  70.     add_contact (&crt, steven);
  71.     add_contact (&crt, steven);
  72.     show_carnet (&crt);
  73.     delete_carnet (&crt);
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement