Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define SIZE 100
- // create contact
- typedef struct
- {
- char name[100];
- int age;
- char adresse[200];
- char phone_number[20];
- char email[200];
- } contact;
- // create carnet
- typedef struct
- {
- contact cont[SIZE];
- int length;
- } Carnet;
- /*
- a fonction to interact with user
- param : c (carnet)
- */
- Carnet menu_add_contact(Carnet c , int taille /*, contact cont*/){
- int nb;
- //contact cont;
- printf("Combien de contact voudriez vous ajouter ? \n");
- scanf(" %d", &nb);
- if ((nb < 0) || (nb > taille))
- {
- printf("erreur vous ne pouvez pas ajouter plus de %d contac", taille);
- }
- for (int i = 0; i < nb; i++)
- {
- printf("Name :");
- scanf(" %s",c.cont[i].name);
- printf("Age :");
- scanf(" %d",&c.cont[i].age);
- printf("Adress :");
- scanf(" %s",c.cont[i].adresse);
- printf("Phone number :");
- scanf(" %s",c.cont[i].phone_number);
- printf("Email :");
- scanf(" %s",c.cont[i].email);
- c.length += 1;
- }
- printf("Contact added !!!\n");
- return c;
- }
- Carnet add_contact(Carnet c /*, contact cont*/){
- Carnet carnet = menu_add_contact(c , SIZE /*, cont*/);
- return carnet;
- }
- Carnet menu_remove_contact(Carnet c){
- int indice;
- printf("Quelième contact voudriez vous supprimer ? \n");
- scanf(" %d", &indice);
- if ((indice < 0) || (indice > c.length -1))
- {
- printf("erreur vous ne pouvez pas supprimer le contact d'indice %d ", indice);
- }
- for (int i = indice; i < c.length-1; i++)
- {
- c.cont[i] = c.cont[i+1];
- }
- c.length -=1;
- printf("Contact removed !!!\n");
- return c;
- }
- Carnet remove_contact(Carnet c){
- Carnet rem_car = menu_remove_contact(c);
- return rem_car;
- }
- void display_contact(Carnet c){
- printf(" VOICI VOTRE REPERTOIRE :\n");
- printf("\n");
- for (int i = 0; i < c.length; i++)
- {
- printf("Name :%s",c.cont[i].name);
- printf("\n");
- printf("Age :%d ", c.cont[i].age);
- printf("\n");
- printf("Adress :%s", c.cont[i].adresse);
- printf("\n");
- printf("phone number :%s",c.cont[i].phone_number);
- printf("\n");
- printf("Email :%s",c.cont[i].email);
- printf("\n");
- printf("------------------------------------------- \n");
- }
- }
- int search(char str[] , char c){
- for (int i = 0;str[i] != '\0' ; i++)
- {
- if (str[i] == c)
- {
- return 1;
- }
- }
- return 0;
- }
- int test(char mot1[] , char mot2[]){
- for (int i = 0; mot1[i] != '\0'; i++)
- {
- if (!search(mot2 , mot1[i]))
- {
- return 0;
- }
- }
- return 1;
- }
- int indice(Carnet c , char mot[]){
- for (int i = 0; i < c.length; i++)
- {
- if (test(c.cont[i].name , mot))
- {
- return i;
- }
- }
- return c.length;
- }
- void display_contact_info(Carnet c , char name[]){
- int index = indice(c , name);
- if (index >= c.length)
- {
- printf("Ce contact n'existe pas !!! \n");
- }
- else
- {
- printf("Name :%s",c.cont[index].name);
- printf("\n");
- printf("Age :%d ", c.cont[index].age);
- printf("\n");
- printf("Adress :%s", c.cont[index].adresse);
- printf("\n");
- printf("phone number :%s",c.cont[index].phone_number);
- printf("\n");
- printf("Email :%s",c.cont[index].email);
- printf("\n");
- printf("------------------------------------------- \n");
- }
- }
- /*
- parametre : contac tab[] -> carnet
- taille
- indice -> n ème contact à supprimer
- */
- /*void remove_contact(contact tab[], int taille , int indice , int nb){
- for (int i = 0; i < nb; i++)
- {
- }
- }*/
- int main(){
- Carnet notebook1;
- Carnet notebook2;
- Carnet notebook3;
- notebook1.length = 0;
- notebook2 = add_contact(notebook1);
- display_contact(notebook2);
- notebook3 = remove_contact(notebook2);
- display_contact(notebook3);
- display_contact_info(notebook3 , "winner");
- /*printf(" ----- AFFICHAGE DES CONTACT ------ \n");
- for (int i = 0; i < notebook2.length; i++)
- {
- printf("Name :%s",notebook2.cont[i].name);
- printf("\n");
- printf("Age :%d ", notebook2.cont[i].age);
- printf("\n");
- printf("Adress :%s", notebook2.cont[i].adresse);
- printf("\n");
- printf("phone number :%s", notebook2.cont[i].phone_number);
- printf("\n");
- printf("Email :%s",notebook2.cont[i].email);
- printf("\n");
- printf("------------------------------------------- \n");
- }*/
- /*printf(" ----- AFFICHAGE DES CONTACT RESTANT ------ \n");
- for (int i = 0; i < notebook3.length; i++)
- {
- printf("Name :%s",notebook3.cont[i].name);
- printf("\n");
- printf("Age :%d ", notebook3.cont[i].age);
- printf("\n");
- printf("Adress :%s", notebook3.cont[i].adresse);
- printf("\n");
- printf("phone number :%s", notebook3.cont[i].phone_number);
- printf("\n");
- printf("Email :%s",notebook3.cont[i].email);
- printf("\n");
- }*/
- return 0;
- /*int nb = 0;
- printf("how many contacts do you like added ? \n");
- scanf(" %d", &nb);
- while (nb < 0 || nb > SIZE)
- {
- printf("Please your notebook can only contain %d contacts !!! \n",SIZE);
- printf("how many contacts do you like added ? \n");
- scanf(" %d", &nb);
- }
- add_contact(carnet , SIZE , nb);
- display_contact(carnet , SIZE , nb);*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement