Ajouter solution_jusque_exo3/main.c
This commit is contained in:
parent
fb60e51b24
commit
fd7d760995
311
solution_jusque_exo3/main.c
Normal file
311
solution_jusque_exo3/main.c
Normal file
@ -0,0 +1,311 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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<contacts[*nbContacts].nbtel;i++){
|
||||
|
||||
printf("Numero de Telephone n° %d: ",i+1);
|
||||
fgets(contacts[*nbContacts].tel[i].numero, MAX_TELEPHONE, stdin);
|
||||
strtok(contacts[*nbContacts].tel[i].numero, "\n");
|
||||
|
||||
printf("Type du Telephone n° %d: ",i+1);
|
||||
fgets(contacts[*nbContacts].tel[i].type, MAX_TYPE_TEL, stdin);
|
||||
strtok(contacts[*nbContacts].tel[i].type, "\n");
|
||||
|
||||
}
|
||||
|
||||
printf("Rue : ");
|
||||
fgets(contacts[*nbContacts].lieu.rue, MAX_RUE, stdin);
|
||||
strtok(contacts[*nbContacts].lieu.rue, "\n");
|
||||
|
||||
printf("Code Postal : ");
|
||||
fgets(contacts[*nbContacts].lieu.codePostal, MAX_CODE_POSTAL, stdin);
|
||||
strtok(contacts[*nbContacts].lieu.codePostal, "\n");
|
||||
|
||||
printf("Ville : ");
|
||||
fgets(contacts[*nbContacts].lieu.ville, MAX_VILLE, stdin);
|
||||
strtok(contacts[*nbContacts].lieu.ville, "\n");
|
||||
|
||||
printf("Pays : ");
|
||||
fgets(contacts[*nbContacts].lieu.pays, MAX_PAYS, 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++) {
|
||||
|
||||
switch (contacts[i].nbtel) {
|
||||
case 1:
|
||||
printf("%d. nom: %s prenom: %s - %s : %s - rue: %s CP: %s ville: %s pays: %s\n", i + 1, contacts[i].nom, contacts[i].prenom, contacts[i].tel[0].type,contacts[i].tel[0].numero,
|
||||
contacts[i].lieu.rue, contacts[i].lieu.codePostal, contacts[i].lieu.ville, contacts[i].lieu.pays);
|
||||
break;
|
||||
case 2:
|
||||
printf("%d. nom: %s prenom: %s - %s : %s - %s : %s - rue: %s CP: %s ville: %s pays: %s\n", i + 1,
|
||||
contacts[i].nom,
|
||||
contacts[i].prenom,
|
||||
contacts[i].tel[0].type,
|
||||
contacts[i].tel[0].numero,
|
||||
contacts[i].tel[1].type,
|
||||
contacts[i].tel[1].numero,
|
||||
contacts[i].lieu.rue,
|
||||
contacts[i].lieu.codePostal,
|
||||
contacts[i].lieu.ville,
|
||||
contacts[i].lieu.pays);
|
||||
break;
|
||||
case 3:
|
||||
printf("%d. nom: %s prenom: %s - %s : %s - %s : %s - %s : %s - rue: %s CP: %s ville: %s pays: %s\n", i + 1,
|
||||
contacts[i].nom,
|
||||
contacts[i].prenom,
|
||||
contacts[i].tel[0].type,
|
||||
contacts[i].tel[0].numero,
|
||||
contacts[i].tel[1].type,
|
||||
contacts[i].tel[1].numero,
|
||||
contacts[i].tel[2].type,
|
||||
contacts[i].tel[2].numero,
|
||||
contacts[i].lieu.rue,
|
||||
contacts[i].lieu.codePostal,
|
||||
contacts[i].lieu.ville,
|
||||
contacts[i].lieu.pays);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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++) {
|
||||
|
||||
|
||||
switch (contacts[i].nbtel) {
|
||||
case 1:
|
||||
fprintf(file, "%s;%s;%s;%s;;;;;%s;%s;%s;%s\n",
|
||||
contacts[i].nom,
|
||||
contacts[i].prenom,
|
||||
contacts[i].tel[0].type,
|
||||
contacts[i].tel[0].numero,
|
||||
contacts[i].lieu.rue,
|
||||
contacts[i].lieu.codePostal,
|
||||
contacts[i].lieu.ville,
|
||||
contacts[i].lieu.pays);
|
||||
break;
|
||||
case 2:
|
||||
fprintf(file, "%s;%s;%s;%s;%s;%s;;;%s;%s;%s;%s\n",
|
||||
contacts[i].nom,
|
||||
contacts[i].prenom,
|
||||
contacts[i].tel[0].type,
|
||||
contacts[i].tel[0].numero,
|
||||
contacts[i].tel[1].type,
|
||||
contacts[i].tel[1].numero,
|
||||
contacts[i].lieu.rue,
|
||||
contacts[i].lieu.codePostal,
|
||||
contacts[i].lieu.ville,
|
||||
contacts[i].lieu.pays);
|
||||
break;
|
||||
case 3:
|
||||
fprintf(file, "%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s\n",
|
||||
contacts[i].nom,
|
||||
contacts[i].prenom,
|
||||
contacts[i].tel[0].type,
|
||||
contacts[i].tel[0].numero,
|
||||
contacts[i].tel[1].type,
|
||||
contacts[i].tel[1].numero,
|
||||
contacts[i].tel[2].type,
|
||||
contacts[i].tel[2].numero,
|
||||
contacts[i].lieu.rue,
|
||||
contacts[i].lieu.codePostal,
|
||||
contacts[i].lieu.ville,
|
||||
contacts[i].lieu.pays);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
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;
|
||||
|
||||
|
||||
*nbContacts=0;
|
||||
char ligne[500];
|
||||
|
||||
while (*nbContacts<MAX_CONTACTS && fgets(ligne,sizeof(ligne),file)){
|
||||
|
||||
if (sscanf(ligne, "%49[^;];%49[^;];%19[^;];%14[^;];;;;;%99[^;];%9[^;];%49[^;];%49[^\n]",
|
||||
contacts[*nbContacts].nom,
|
||||
contacts[*nbContacts].prenom,
|
||||
contacts[*nbContacts].tel[0].type ,
|
||||
contacts[*nbContacts].tel[0].numero ,
|
||||
contacts[*nbContacts].lieu.rue,
|
||||
contacts[*nbContacts].lieu.codePostal,
|
||||
contacts[*nbContacts].lieu.ville,
|
||||
contacts[*nbContacts].lieu.pays)==8)
|
||||
|
||||
{
|
||||
contacts[*nbContacts].nbtel=1;
|
||||
(*nbContacts)++;
|
||||
}
|
||||
else{
|
||||
if (sscanf(ligne, "%49[^;];%49[^;];%19[^;];%14[^;];%19[^;];%14[^;];;;%99[^;];%9[^;];%49[^;];%49[^\n]",
|
||||
contacts[*nbContacts].nom,
|
||||
contacts[*nbContacts].prenom,
|
||||
contacts[*nbContacts].tel[0].type ,
|
||||
contacts[*nbContacts].tel[0].numero ,
|
||||
contacts[*nbContacts].tel[1].type ,
|
||||
contacts[*nbContacts].tel[1].numero ,
|
||||
contacts[*nbContacts].lieu.rue,
|
||||
contacts[*nbContacts].lieu.codePostal,
|
||||
contacts[*nbContacts].lieu.ville,
|
||||
contacts[*nbContacts].lieu.pays)==10)
|
||||
|
||||
{
|
||||
contacts[*nbContacts].nbtel=2;
|
||||
(*nbContacts)++;
|
||||
}
|
||||
|
||||
else{
|
||||
if (sscanf(ligne, "%49[^;];%49[^;];%19[^;];%14[^;];%19[^;];%14[^;];%19[^;];%14[^;];%99[^;];%9[^;];%49[^;];%49[^\n]",
|
||||
contacts[*nbContacts].nom,
|
||||
contacts[*nbContacts].prenom,
|
||||
contacts[*nbContacts].tel[0].type ,
|
||||
contacts[*nbContacts].tel[0].numero ,
|
||||
contacts[*nbContacts].tel[1].type ,
|
||||
contacts[*nbContacts].tel[1].numero ,
|
||||
contacts[*nbContacts].tel[2].type ,
|
||||
contacts[*nbContacts].tel[2].numero ,
|
||||
contacts[*nbContacts].lieu.rue,
|
||||
contacts[*nbContacts].lieu.codePostal,
|
||||
contacts[*nbContacts].lieu.ville,
|
||||
contacts[*nbContacts].lieu.pays)==12)
|
||||
|
||||
{
|
||||
contacts[*nbContacts].nbtel=3;
|
||||
(*nbContacts)++;
|
||||
}
|
||||
else{}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
fclose(file);
|
||||
printf("%d contacts charges avec succes.\n",*nbContacts);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user