diff --git a/sol_exo2/main.c b/sol_exo2/main.c new file mode 100644 index 0000000..ad5334b --- /dev/null +++ b/sol_exo2/main.c @@ -0,0 +1,319 @@ +#include +#include +#include + +#define MAX_CONTACTS 100 + + +typedef struct{ + char type[20]; + char numero[15]; +}Telephone; + + + +typedef struct{ + char rue[100]; + char codePostal[10]; + char ville[50]; + char pays[50]; +}adresse; + +// Structure d'un contact +typedef struct { + char nom[50]; + char prenom[50]; + int nbtel; + Telephone tel[3]; + adresse lieu; +} Contact; + +/*int a; +Contact truc; +truc.prenom = "valentin"; +truc.lieu.ville="limoges"; +adresse mon_adresse; +truc.lieu=mon_adresse;*/ + +// 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"); + + int choix; + do { + printf("\n1. Ajouter un contact\n"); + printf("2. Afficher les contacts\n"); + printf("3. Quitter\n"); + printf("Votre choix : "); + scanf("%d", &choix); + getchar(); // Pour éviter le problème de buffer avec scanf + + switch (choix) { + case 1: + ajouterContact(contacts, &nbContacts); + sauvegarderContacts(contacts, nbContacts, "contacts.txt"); + break; + case 2: + afficherContacts(contacts, nbContacts); + break; + case 3: + printf("Au revoir !\n"); + break; + default: + printf("Choix invalide, veuillez réessayer.\n"); + } + } while (choix != 3); + + 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, 50, stdin); + strtok(contacts[*nbContacts].nom, "\n"); + + printf("Prénom : "); + fgets(contacts[*nbContacts].prenom, 50, stdin); + strtok(contacts[*nbContacts].prenom, "\n"); + + + printf("Combien de numero de téléphone à ajouter ? (min 1 max 3)"); + scanf("%d",&contacts[*nbContacts].nbtel); + getchar(); + + while (contacts[*nbContacts].nbtel<1 || contacts[*nbContacts].nbtel>3 ){ + printf("Combien de numero de téléphone à ajouter ? (min 1 max 3)"); + scanf("%d",&contacts[*nbContacts].nbtel); + getchar(); + } + + + for (int i=0;i