Advertisement
Guest User

TP7.c

a guest
Apr 1st, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define SIZE 100
  5.  
  6.  
  7. // create contact
  8. typedef struct
  9. {
  10.     char name[100];
  11.     int age;
  12.     char adresse[200];
  13.     char phone_number[20];
  14.     char email[200];
  15. } contact;
  16.  
  17.  
  18. // create carnet
  19. typedef struct
  20. {
  21.     contact cont[SIZE];
  22.     int length;
  23. } Carnet;
  24.  
  25.  
  26. /*
  27.     a fonction to interact with user
  28.     param : c (carnet)
  29. */
  30. Carnet menu_add_contact(Carnet c , int taille  /*, contact cont*/){
  31.     int nb;
  32.     //contact cont;
  33.     printf("Combien de contact voudriez vous ajouter ? \n");
  34.     scanf(" %d", &nb);
  35.     if ((nb < 0) || (nb > taille))
  36.     {
  37.         printf("erreur vous ne pouvez pas ajouter plus de %d contac", taille);
  38.     }
  39.  
  40.     for (int i = 0; i < nb; i++)
  41.     {  
  42.         printf("Name :");
  43.         scanf(" %s",c.cont[i].name);
  44.         printf("Age :");
  45.         scanf(" %d",&c.cont[i].age);
  46.         printf("Adress :");
  47.         scanf(" %s",c.cont[i].adresse);
  48.         printf("Phone number :");
  49.         scanf(" %s",c.cont[i].phone_number);
  50.         printf("Email :");
  51.         scanf(" %s",c.cont[i].email);
  52.  
  53.         c.length += 1;
  54.     }
  55.  
  56.     printf("Contact added !!!\n");
  57.     return c;
  58.  
  59. }
  60.  
  61.  
  62. Carnet add_contact(Carnet c /*, contact cont*/){
  63.   Carnet carnet = menu_add_contact(c , SIZE /*, cont*/);
  64.   return carnet;
  65. }
  66.  
  67. Carnet menu_remove_contact(Carnet c){
  68.     int indice;
  69.     printf("Quelième contact voudriez vous supprimer ? \n");
  70.     scanf(" %d", &indice);
  71.     if ((indice < 0) || (indice > c.length -1))
  72.     {
  73.         printf("erreur vous ne pouvez pas supprimer le contact d'indice %d ", indice);
  74.     }
  75.     for (int i = indice; i < c.length-1; i++)
  76.     {  
  77.         c.cont[i] = c.cont[i+1];
  78.     }
  79.     c.length -=1;
  80.     printf("Contact removed !!!\n");
  81.     return c;
  82.  
  83. }
  84.  
  85. Carnet remove_contact(Carnet c){
  86.     Carnet rem_car = menu_remove_contact(c);
  87.     return rem_car;
  88. }
  89.  
  90.  
  91.  
  92.  
  93. void display_contact(Carnet c){
  94.     printf(" VOICI VOTRE REPERTOIRE :\n");
  95.     printf("\n");
  96.     for (int i = 0; i < c.length; i++)
  97.     {
  98.         printf("Name :%s",c.cont[i].name);
  99.         printf("\n");
  100.         printf("Age :%d ", c.cont[i].age);
  101.         printf("\n");
  102.         printf("Adress :%s", c.cont[i].adresse);
  103.         printf("\n");
  104.         printf("phone number :%s",c.cont[i].phone_number);
  105.         printf("\n");
  106.         printf("Email :%s",c.cont[i].email);
  107.         printf("\n");
  108.         printf("------------------------------------------- \n");
  109.     }
  110.    
  111. }
  112.  
  113.  
  114.  
  115. int search(char str[] , char c){
  116.     for (int i = 0;str[i] != '\0' ; i++)
  117.     {
  118.         if (str[i] == c)
  119.         {
  120.             return 1;
  121.         }
  122.     }
  123.     return 0;
  124. }
  125.  
  126. int test(char mot1[] , char mot2[]){
  127.     for (int i = 0; mot1[i] != '\0'; i++)
  128.     {
  129.         if (!search(mot2 , mot1[i]))
  130.         {
  131.             return 0;
  132.         }
  133.            
  134.     }
  135.     return 1;
  136. }
  137.  
  138. int indice(Carnet c , char mot[]){
  139.     for (int i = 0; i < c.length; i++)
  140.     {
  141.         if (test(c.cont[i].name , mot))
  142.         {
  143.             return i;
  144.         }
  145.     }
  146.     return c.length;
  147. }
  148.  
  149.  
  150. void display_contact_info(Carnet c , char name[]){
  151.     int index = indice(c , name);
  152.     if (index >= c.length)
  153.     {
  154.         printf("Ce contact n'existe pas !!! \n");
  155.     }
  156.     else
  157.     {
  158.         printf("Name :%s",c.cont[index].name);
  159.         printf("\n");
  160.         printf("Age :%d ", c.cont[index].age);
  161.         printf("\n");
  162.         printf("Adress :%s", c.cont[index].adresse);
  163.         printf("\n");
  164.         printf("phone number :%s",c.cont[index].phone_number);
  165.         printf("\n");
  166.         printf("Email :%s",c.cont[index].email);
  167.         printf("\n");
  168.         printf("------------------------------------------- \n");
  169.     }
  170. }
  171.  
  172.  
  173. /*
  174.     parametre : contac tab[] -> carnet
  175.                 taille
  176.                 indice  -> n ème contact à supprimer
  177. */
  178.  
  179. /*void remove_contact(contact tab[], int taille , int indice , int nb){
  180.    for (int i = 0; i < nb; i++)
  181.    {
  182.        
  183.        
  184.    }
  185.    
  186.    
  187. }*/
  188.  
  189.  
  190. int main(){
  191.     Carnet notebook1;
  192.     Carnet notebook2;
  193.     Carnet notebook3;
  194.     notebook1.length = 0;
  195.     notebook2 = add_contact(notebook1);
  196.     display_contact(notebook2);
  197.     notebook3 = remove_contact(notebook2);
  198.     display_contact(notebook3);
  199.     display_contact_info(notebook3 , "winner");
  200.     /*printf(" -----     AFFICHAGE DES CONTACT ------ \n");
  201.  
  202.     for (int i = 0; i < notebook2.length; i++)
  203.     {
  204.         printf("Name :%s",notebook2.cont[i].name);
  205.         printf("\n");
  206.         printf("Age :%d ", notebook2.cont[i].age);
  207.         printf("\n");
  208.         printf("Adress :%s", notebook2.cont[i].adresse);
  209.         printf("\n");
  210.         printf("phone number :%s", notebook2.cont[i].phone_number);
  211.         printf("\n");
  212.         printf("Email :%s",notebook2.cont[i].email);
  213.         printf("\n");
  214.         printf("------------------------------------------- \n");
  215.     }*/
  216.  
  217.  
  218.     /*printf(" -----     AFFICHAGE DES CONTACT RESTANT ------ \n");
  219.  
  220.     for (int i = 0; i < notebook3.length; i++)
  221.     {
  222.         printf("Name :%s",notebook3.cont[i].name);
  223.         printf("\n");
  224.         printf("Age :%d ", notebook3.cont[i].age);
  225.         printf("\n");
  226.         printf("Adress :%s", notebook3.cont[i].adresse);
  227.         printf("\n");
  228.         printf("phone number :%s", notebook3.cont[i].phone_number);
  229.         printf("\n");
  230.         printf("Email :%s",notebook3.cont[i].email);
  231.         printf("\n");
  232.     }*/
  233.     return 0;
  234.     /*int nb = 0;
  235.     printf("how many contacts do you like added  ? \n");
  236.     scanf(" %d", &nb);
  237.     while (nb < 0 || nb > SIZE)
  238.     {
  239.         printf("Please your notebook can only contain %d contacts !!! \n",SIZE);
  240.         printf("how many contacts do you like added  ? \n");
  241.         scanf(" %d", &nb);
  242.     }
  243.     add_contact(carnet , SIZE , nb);
  244.     display_contact(carnet , SIZE , nb);*/
  245.    
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement