Ajouter solution_exo1/main.c

This commit is contained in:
bastien 2025-04-11 07:50:44 +00:00
parent b7c327edb1
commit 98d14e3d4a

141
solution_exo1/main.c Normal file
View File

@ -0,0 +1,141 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CONTACTS 100
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];
char telephone[15];
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("Téléphone : ");
fgets(contacts[*nbContacts].telephone, 15, stdin);
strtok(contacts[*nbContacts].telephone, "\n");
printf("Rue: ");
fgets(contacts[*nbContacts].lieu.rue, 100, stdin);
strtok(contacts[*nbContacts].lieu.rue, "\n");
printf("Code Postal: ");
fgets(contacts[*nbContacts].lieu.codePostal, 10, stdin);
strtok(contacts[*nbContacts].lieu.codePostal, "\n");
printf("ville: ");
fgets(contacts[*nbContacts].lieu.ville, 50, stdin);
strtok(contacts[*nbContacts].lieu.ville, "\n");
printf("Pays: ");
fgets(contacts[*nbContacts].lieu.pays, 50, stdin);
strtok(contacts[*nbContacts].lieu.pays, "\n");
(*nbContacts)++;
}
// Fonction pour afficher tous les contacts
void afficherContacts(Contact *contacts, int nbContacts) {
if (nbContacts == 0) {
printf("Aucun contact enregistré.\n");
return;
}
for (int i = 0; i < nbContacts; i++) {
printf("%d. %s %s - %s - %s %s %s %s\n", i + 1, contacts[i].nom, contacts[i].prenom, contacts[i].telephone,
contacts[i].lieu.rue,contacts[i].lieu.codePostal,contacts[i].lieu.ville,contacts[i].lieu.pays);
}
}
// Fonction pour sauvegarder les contacts dans un fichier
void sauvegarderContacts(Contact *contacts, int nbContacts, const char *filename) {
FILE *file = fopen(filename, "w");
if (!file) {
printf("Erreur d'ouverture du fichier.\n");
return;
}
for (int i = 0; i < nbContacts; i++) {
fprintf(file, "%s;%s;%s;%s;%s;%s;%s\n", contacts[i].nom, contacts[i].prenom, contacts[i].telephone,
contacts[i].lieu.rue,contacts[i].lieu.codePostal,contacts[i].lieu.ville,contacts[i].lieu.pays);
}
fclose(file);
}
// Fonction pour charger les contacts depuis un fichier
void chargerContacts(Contact *contacts, int *nbContacts, const char *filename) {
FILE *file = fopen(filename, "r");
if (!file) return;
while (fscanf(file, "%49[^;];%49[^;];%14[^;];%99[^;]%9[^;]%49[^;]%49[^\n]\n", contacts[*nbContacts].nom, contacts[*nbContacts].prenom, contacts[*nbContacts].telephone, contacts[*nbContacts].lieu.rue, contacts[*nbContacts].lieu.codePostal, contacts[*nbContacts].lieu.ville, contacts[*nbContacts].lieu.pays) == 7) {
(*nbContacts)++;
}
fclose(file);
}