From fd7d760995da8e1c66b9ef928d87862130a2d724 Mon Sep 17 00:00:00 2001 From: bastien Date: Thu, 17 Apr 2025 06:21:54 +0000 Subject: [PATCH] Ajouter solution_jusque_exo3/main.c --- solution_jusque_exo3/main.c | 311 ++++++++++++++++++++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100644 solution_jusque_exo3/main.c diff --git a/solution_jusque_exo3/main.c b/solution_jusque_exo3/main.c new file mode 100644 index 0000000..d1b5d06 --- /dev/null +++ b/solution_jusque_exo3/main.c @@ -0,0 +1,311 @@ +#include +#include +#include + +#define MAX_CONTACTS 100 +#define MAX_NOM 50 +#define MAX_PRENOM 50 +#define MAX_TELEPHONE 15 +#define MAX_RUE 100 +#define MAX_CODE_POSTAL 10 +#define MAX_VILLE 50 +#define MAX_PAYS 50 +#define MAX_TYPE_TEL 20 +#define MAX_TELEPHONES 3 + +#define CHOIX_AJOUTER 27 +#define CHOIX_AFFICHER 32 +#define CHOIX_QUITTER 48 + +typedef struct{ + char type[MAX_TYPE_TEL]; + char numero[MAX_TELEPHONE]; +}Telephone; + + +typedef struct{ + char rue[MAX_RUE]; + char codePostal[MAX_CODE_POSTAL]; + char ville[MAX_VILLE]; + char pays[MAX_PAYS] +}adresse; + +// Structure d'un contact +typedef struct { + char nom[MAX_NOM]; + char prenom[MAX_PRENOM]; + int nbtel; + Telephone tel[MAX_TELEPHONES]; + adresse lieu; +} Contact; + + + +// Prototypes des fonctions +void ajouterContact(Contact *contacts, int *nbContacts); +void afficherContacts(Contact *contacts, int nbContacts); +void sauvegarderContacts(Contact *contacts, int nbContacts, const char *filename); +void chargerContacts(Contact *contacts, int *nbContacts, const char *filename); + +int main() { + Contact contacts[MAX_CONTACTS]; + int nbContacts = 0; + chargerContacts(contacts, &nbContacts, "contacts.txt"); + printf ("%d",nbContacts); + int choix; + do { + printf("\n%d. Ajouter un contact\n",CHOIX_AJOUTER); + printf("%d. Afficher les contacts\n",CHOIX_AFFICHER); + printf("%d. Quitter\n",CHOIX_QUITTER); + printf("Votre choix : "); + scanf("%d", &choix); + getchar(); // Pour éviter le problème de buffer avec scanf + + switch (choix) { + case CHOIX_AJOUTER: + ajouterContact(contacts, &nbContacts); + sauvegarderContacts(contacts, nbContacts, "contacts.txt"); + break; + case CHOIX_AFFICHER: + afficherContacts(contacts, nbContacts); + break; + case CHOIX_QUITTER: + printf("Au revoir !\n"); + break; + default: + printf("Choix invalide, veuillez réessayer.\n"); + } + } while (choix != CHOIX_QUITTER); + + return 0; +} + +// Fonction pour ajouter un contact +void ajouterContact(Contact *contacts, int *nbContacts) { + if (*nbContacts >= MAX_CONTACTS) { + printf("Le carnet d'adresses est plein !\n"); + return; + } + + printf("Nom : "); + fgets(contacts[*nbContacts].nom, MAX_NOM, stdin); + strtok(contacts[*nbContacts].nom, "\n"); + + printf("Prénom : "); + fgets(contacts[*nbContacts].prenom, MAX_PRENOM, stdin); + strtok(contacts[*nbContacts].prenom, "\n"); + + + + printf("Combien de numéro à enreistré pour ce contact (min 1 ; max %d): ",MAX_TELEPHONES); + scanf("%d",&contacts[*nbContacts].nbtel); + getchar(); + + while (contacts[*nbContacts].nbtel<1 || contacts[*nbContacts].nbtel>MAX_TELEPHONES){ + printf("Combien de numéro à enreistré pour ce contact (min 1 ; max %d): ",MAX_TELEPHONES); + scanf("%d",&contacts[*nbContacts].nbtel); + getchar();} + + for (int i=0;i