Ajouter fin-exo1/main.c
This commit is contained in:
parent
2d410ba50a
commit
1f3703e2ab
142
fin-exo1/main.c
Normal file
142
fin-exo1/main.c
Normal file
@ -0,0 +1,142 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_CONTACTS 100
|
||||
|
||||
typedef struct {
|
||||
char rue[100];
|
||||
char cp[10];
|
||||
char ville[50];
|
||||
char pays[50];
|
||||
} adresse;
|
||||
|
||||
// Structure d'un contact
|
||||
typedef struct {
|
||||
char nom[50];
|
||||
char prenom[50];
|
||||
char telephone;
|
||||
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");
|
||||
|
||||
int choix=0;
|
||||
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("Codepostal : ");
|
||||
fgets(contacts[*nbContacts].lieu.cp, 10, stdin);
|
||||
strtok(contacts[*nbContacts].lieu.cp, "\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 - adresse : %s %s %s %s\n", i + 1,
|
||||
contacts[i].nom, contacts[i].prenom, contacts[i].telephone,
|
||||
contacts[i].lieu.rue,contacts[i].lieu.cp,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.cp,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.cp, contacts[*nbContacts].lieu.ville,contacts[*nbContacts].lieu.pays)
|
||||
// == 7) {
|
||||
// (*nbContacts)++;
|
||||
// }
|
||||
fclose(file);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user