Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define taille 20
- typedef struct contact
- {
- char Nom[100];
- char Prenom[100];
- char Sex;
- char Num_tel_fix[100];
- char Num_tel_portable[100];
- struct contact* conjoint; // suivant
- }contact;
- typedef struct carnet{
- contact** contacts;
- int size; // taille effective
- } Carnet;
- void afficher_info_contact(contact* cont){
- 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",
- cont->Nom, cont->Prenom, cont->Sex, cont->Num_tel_fix, cont->Num_tel_portable);
- }
- contact* create_contact(){
- contact* cont = NULL;
- cont = (contact*)malloc(sizeof(contact));
- printf("Nom :");
- scanf(" %s",cont->Nom);
- printf("Prenom :");
- scanf(" %s",cont->Prenom);
- printf("Sex :");
- scanf(" %c",&cont->Sex);
- printf("Numéro de téléphone fixe:");
- scanf(" %s",cont->Num_tel_fix);
- printf("Numéro de téléphone portable:");
- scanf(" %s",cont->Num_tel_portable);
- return cont;
- }
- void ajout_contact(Carnet* c){
- int nb;
- printf("Combien de contact voudriez vous ajouter ? \n");
- scanf(" %d", &nb);
- while ((nb < 0) || (nb > c->size))
- {
- printf("erreur vous ne pouvez pas ajouter plus de %d contact", c->size);
- printf("Combien de contact voudriez vous ajouter ? \n");
- scanf(" %d", &nb);
- }
- for (int i = 0; i < nb; i++)
- {
- c->contacts[i] = create_contact();
- }
- c->size = nb;
- }
- int test_sous_string(char mot1[], char mot2[]){
- for (int i = 0; mot2[i] != '\0'; i++)
- {
- if (mot1[i] != mot2[i])
- {
- return 0;
- }
- }
- return 1;
- }
- int test_string(char mot1[], char mot2[]){
- for (int i = 0; mot2[i] != '\0' || mot1[i] != '\0'; i++)
- {
- if (mot1[i] == '\0')
- {
- return 0;
- }
- if (mot1[i] != mot2[i])
- {
- return 0;
- }
- }
- return 1;
- }
- void recherche_contact(Carnet carnet , char str[]){
- printf("voici les contacts qui commence par %s", str);
- for (int i = 0; i < carnet.size; i++)
- {
- if (test_sous_string(carnet.contacts[i]->Nom , str))
- {
- afficher_info_contact(carnet.contacts[i]);
- }
- }
- }
- void specification_conjoint(Carnet carnet){
- // les conjoint sont sous cette forme: a->b b->a c->d ...
- // le conjoint de a c'est b , le conjoint de b c'est a etc... :) un peut bizarre mas bon !
- for (int i = 1; i <= carnet.size - 1; i+=2)
- {
- carnet.contacts[i]->conjoint = carnet.contacts[i-1];
- carnet.contacts[i-1]->conjoint = carnet.contacts[i];
- }
- }
- void recherche_contact_par_nom_conj(Carnet carnet , char str[] , contact* conjoint){
- for (int i = 0; i < carnet.size; i++)
- {
- if (test_string(carnet.contacts[i]->Nom , str))
- {
- afficher_info_contact(carnet.contacts[i]);
- }
- if (carnet.contacts[i]->conjoint == conjoint)
- {
- afficher_info_contact(carnet.contacts[i]);
- }
- }
- }
- int main(){
- Carnet carnet;
- Carnet* ptr = &carnet;
- carnet.contacts = NULL;
- carnet.contacts = (contact**)malloc(sizeof(contact*) * taille);
- // création du tableau de contact (carnet)
- for (int i = 0; i < taille; i++)
- {
- carnet.contacts[i] = (contact*)malloc(sizeof(contact));
- }
- // ajout contact
- ajout_contact(ptr);
- // recherche contact
- recherche_contact(carnet , "string");
- // spécification de conjoint
- specification_conjoint(carnet);
- int i = 3; // par exemple 3 eme contact
- // recherche contact par nom ou conjoint
- recherche_contact_par_nom_conj(carnet , "string" , carnet.contacts[i]);
- // restituer la mémoire au système
- for (int i = 0; i < taille; i++)
- {
- free(carnet.contacts[i]);
- }
- free(carnet.contacts);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement