Supprimer sol_exo2/main.c
This commit is contained in:
parent
62f7da3497
commit
fb60e51b24
319
sol_exo2/main.c
319
sol_exo2/main.c
@ -1,319 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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<contacts[*nbContacts].nbtel;i++)
|
||||
{
|
||||
printf("Telephone n° %d: ",i+1);
|
||||
fgets(contacts[*nbContacts].tel[i].numero, 15, stdin);
|
||||
strtok(contacts[*nbContacts].tel[i].numero, "\n");
|
||||
|
||||
printf("Type du telephone n° %d: ",i+1);
|
||||
fgets(contacts[*nbContacts].tel[i].type, 20, stdin);
|
||||
strtok(contacts[*nbContacts].tel[i].type, "\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++) {
|
||||
|
||||
switch (contacts[i].nbtel) {
|
||||
case 1:
|
||||
printf("%d. %s %s - %s:%s - %s %s %s %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. %s %s - %s:%s - %s:%s - %s %s %s %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. %s %s - %s:%s - %s:%s- %s:%s - %s %s %s %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) {
|
||||
printf("fichier introuvable");
|
||||
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)++;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
fclose(file);
|
||||
printf("%d contacts charges avec succes.\n",*nbContacts);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user