add compNP + sort

This commit is contained in:
Adrien Van 2017-03-31 09:08:50 +02:00
parent 554928ac0f
commit 188ed1c4d7
1 changed files with 653 additions and 618 deletions

View File

@ -35,6 +35,7 @@ typedef struct{
} Contact; } Contact;
typedef struct{ typedef struct{
char nom[__STRLEN__]; char nom[__STRLEN__];
char prenom[__STRLEN__];
Adresse adresse; Adresse adresse;
Contact contact; Contact contact;
Date anniversaire; Date anniversaire;
@ -63,22 +64,25 @@ Bool clean_keyboard();
int ageClient(Client *, Date *); int ageClient(Client *, Date *);
int pointsFidelite(float); int pointsFidelite(float);
void gestionFacture(Client *, Date *); void gestionFacture(Client *, Date *);
Client * rechercheClient(char *, FILE *, Date *); Client * rechercheClient(char *, char *, FILE *, Date *);
Bool gestionClient(Client *, Date *); Bool gestionClient(Client *, Date *);
FILE * localiserFichierClients(char *); FILE * localiserFichierClients(char *);
int ajouterClientBin(FILE *, Client * client); int ajouterClientBin(FILE *, Client *);
int compNP(char *, char *,char *, char *);
void qSortClientBinByNP(FILE *);
int main() int main()
{ {
//printf("%d\n", pointsFidelite(52.23));
FILE * clientF; FILE * clientF;
Date date; Date date;
char tmp; char tmp;
Bool w; Bool w;
time_t curTimeStamp = time(NULL); time_t curTimeStamp = time(NULL);
struct tm *locTime = localtime(&curTimeStamp); struct tm *locTime = localtime(&curTimeStamp);
locTime->tm_mon += 1; locTime->tm_mon += 1;
locTime->tm_year += 1900; locTime->tm_year += 1900;
printf("Voulez-vous utilisez la date système %d/%d/%d (Y|n)?\n", locTime->tm_mday,locTime->tm_mon,locTime->tm_year); printf("Voulez-vous utilisez la date système %d/%d/%d (Y|n)?\n", locTime->tm_mday,locTime->tm_mon,locTime->tm_year);
scanf("%c", &tmp); scanf("%c", &tmp);
if((tmp|0b00100000) != 'y') if((tmp|0b00100000) != 'y')
@ -88,11 +92,11 @@ int main()
date.jour = locTime->tm_mday; date.jour = locTime->tm_mday;
date.moi = locTime->tm_mon; date.moi = locTime->tm_mon;
date.annee = locTime->tm_year; date.annee = locTime->tm_year;
// printf("%d/%d/%d\n", date->jour, date->moi, date->annee );
} }
Client * client; Client * client;
char nomClient[__STRLEN__]; char nomClient[__STRLEN__];
char prenomClient[__STRLEN__];
char fichierClient[__STRLEN_FILE__]; char fichierClient[__STRLEN_FILE__];
int choixMenu; int choixMenu;
clientF = localiserFichierClients(fichierClient); clientF = localiserFichierClients(fichierClient);
@ -115,7 +119,9 @@ int main()
case 2: case 2:
printf("Nom du client (sensible a la casse) : "); printf("Nom du client (sensible a la casse) : ");
clean_keyboard();scanf("%s", nomClient); clean_keyboard();scanf("%s", nomClient);
client = rechercheClient(nomClient, clientF, &date); printf("\nPrenom du client (sensible a la casse) : ");
clean_keyboard();scanf("%s", prenomClient);
client = rechercheClient(nomClient, prenomClient clientF, &date);
if(client != NULL) if(client != NULL)
{ {
w = gestionClient(client, &date); w = gestionClient(client, &date);
@ -145,60 +151,19 @@ Bool dateValide(Date * date)
int j = date->jour; int j = date->jour;
int m = date->moi; int m = date->moi;
int a = date->annee; int a = date->annee;
int bis; int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(m != 2)
if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))//Si bis février = 29
daysinmonth[1]=29;
if (m<13)
{ {
if(m >= 1 && m <= 7) if( d <= daysinmonth[m-1] )
{ return 1;
if(m %2 == 0) else
{ return 0;
if(j >= 1 && j <= 30) return TRUE;
else return FALSE;
}
else
{
if(j >= 1 && j <= 31) return TRUE;
else return FALSE;
}
}
else if(m > 7 && m <= 12)
{
if(m %2 == 0)
{
if(j >= 1 && j <= 31) return TRUE;
else return FALSE;
}
else
{
if(j >= 1 && j <= 30) return TRUE;
else return FALSE;
}
}
else return FALSE;
} }
else else
{ return 0;
if(a %100 == 0)
{
if(a %400 == 0) bis = 1;
else bis = FALSE;
}
else
{
if(a %4 == 0)bis = 1;
else bis = FALSE;
}
if(bis == 1)
{
if(j >= 1 && j <= 29) return TRUE;
else return FALSE;
}
else
{
if(j >= 1 && j <= 28) return TRUE;
else return FALSE;
}
}
} }
void encoderArticle(Article * article) void encoderArticle(Article * article)
@ -216,19 +181,20 @@ void encoderArticle(Article * article)
void encoderClient(Client * client) void encoderClient(Client * client)
{ {
printf("Encoage Client\n"); printf("Encodage Client\n");
client->fidelite = 0; client->fidelite = 0;
if(!(printf("Entrez la rue :") && clean_keyboard() && fgets(client->adresse.rue,__STRLEN__,stdin) != NULL && \ if(!(printf("Entrez la rue :") && clean_keyboard() && fgets(client->adresse.rue,__STRLEN__,stdin) != NULL && \
printf("\nEntrez le numero :" ) && clean_keyboard() && fgets(client->adresse.numero,__STRLEN__,stdin) != NULL && \ printf("\nEntrez le numero :" ) && clean_keyboard() && fgets(client->adresse.numero,__STRLEN__,stdin) != NULL && \
printf("\nEntrez la localité :") && clean_keyboard() && fgets(client->adresse.localite,__STRLEN__,stdin) != NULL && \ printf("\nEntrez la localité :") && clean_keyboard() && fgets(client->adresse.localite,__STRLEN__,stdin) != NULL && \
printf("\nEntrez le code postal :") && clean_keyboard() && scanf("%d", &(client->adresse.codePostal)) && \ printf("\nEntrez le code postal :") && clean_keyboard() && scanf("%d", &(client->adresse.codePostal)) && \
printf("\nNom client :") && clean_keyboard() && fgets(client->nom,__STRLEN__,stdin) != NULL)) printf("\nNom client :") && clean_keyboard() && fgets(client->nom,__STRLEN__,stdin) != NULL) && \
printf("\nPrenom client :") && clean_keyboard() && fgets(client->prenom,__STRLEN__,stdin) != NULL))
{ {
printf("\nErreur lors de l'encodage de l'adresse"); printf("\nErreur lors de l'encodage de l'adresse");
exit(-1); exit(-1);
} }
cleanFgets(client->adresse.rue);cleanFgets(client->adresse.numero);cleanFgets(client->adresse.localite); cleanFgets(client->adresse.rue);cleanFgets(client->adresse.numero);cleanFgets(client->adresse.localite);
cleanFgets(client->nom); cleanFgets(client->nom);cleanFgets(client->prenom);
if(!(printf("\n Entrez le numéro de tél. :") && clean_keyboard() &&fgets(client->contact.telephone,13,stdin) && \ if(!(printf("\n Entrez le numéro de tél. :") && clean_keyboard() &&fgets(client->contact.telephone,13,stdin) && \
printf("\nEntrez l'adresse email :") && clean_keyboard() &&fgets(client->contact.email,__STRLEN__,stdin))) printf("\nEntrez l'adresse email :") && clean_keyboard() &&fgets(client->contact.email,__STRLEN__,stdin)))
{ {
@ -240,7 +206,7 @@ void encoderClient(Client * client)
} }
void afficherClient(Client *client, Date *date) void afficherClient(Client *client, Date *date)
{ {
printf("Client : %s (%d)\n", client->nom, ageClient(client, date)); printf("Client : %s %s (%d)\n", client->nom, client->prenom, ageClient(client, date));
printf("rue : %s\n",client->adresse.rue); printf("rue : %s\n",client->adresse.rue);
printf("numero : %s\n",client->adresse.numero); printf("numero : %s\n",client->adresse.numero);
printf("localité : %s\n",client->adresse.localite); printf("localité : %s\n",client->adresse.localite);
@ -251,7 +217,7 @@ void afficherClient(Client *client, Date *date)
} }
void sauvegarderClient(Client *client, FILE * file, Date *date) void sauvegarderClient(Client *client, FILE * file, Date *date)
{ {
fprintf(file, "Client : %s (%d)\n", client->nom, ageClient(client, date)); fprintf(file, "Client : %s %s (%d)\n", client->nom, client->prenom, ageClient(client, date));
fprintf(file, "rue : %s\n",client->adresse.rue); fprintf(file, "rue : %s\n",client->adresse.rue);
fprintf(file, "numero : %s\n",client->adresse.numero); fprintf(file, "numero : %s\n",client->adresse.numero);
fprintf(file, "localité : %s\n",client->adresse.localite); fprintf(file, "localité : %s\n",client->adresse.localite);
@ -533,16 +499,16 @@ void gestionFacture(Client * client, Date * date)
free(client->panier.article); free(client->panier.article);
} }
Client * rechercheClient(char nom[__STRLEN__], FILE * clientF, Date * date) Client * rechercheClient(char nom[__STRLEN__], char prenom[__STRLEN__], FILE * clientF, Date * date)
{ {
Client * client = NULL; Client * client = NULL;
client = malloc(sizeof(Client)); client = malloc(sizeof(Client));
fseek(clientF, 0, SEEK_SET); fseek(clientF, sizeof(unsigned long int)+sizeof(Bool), SEEK_SET); //On se possitionne apprès le nbr de client
clearerr(clientF); //Clear EOF clearerr(clientF); //Clear EOF
do do
{ {
fread(client, sizeof(Client), 1, clientF); fread(client, sizeof(Client), 1, clientF);
}while(!feof(clientF) && strcmp(client->nom, nom)); }while(!feof(clientF) && compNP(client->nom, client->prenom, nom, prenom));
if(feof(clientF)) if(feof(clientF))
return NULL; return NULL;
else else
@ -592,15 +558,26 @@ FILE * localiserFichierClients(char fichierClient[__STRLEN_FILE__])
clientF = fopen(repTmp, "r"); clientF = fopen(repTmp, "r");
if(clientF == NULL) if(clientF == NULL)
{ {
char c;
fclose(clientF); fclose(clientF);
clientF = fopen(repTmp, "w"); printf("\nLe fichier n'existe pas. Voulez-vous le creer (C) ou changer de repertoir (R) (C|R)?");
fclose(clientF); clean_keyboard();scanf("%c", &c);
if(c&0b00100000 == 'c')
{
clientF = fopen(repTmp, "wb");
fwrite(0, sizeof(Bool), 1, clientF); //Les 1e bits stockent si fichier trier et le nbr de client
fwrite(0, sizeof(unsigned long int), 1, clientF);
fclose(clientF);
}
else
return localiserFichierClients(fichierClient);
} }
fclose(clientF); else
fclose(clientF);
clientF = fopen(repTmp, "r+b"); clientF = fopen(repTmp, "r+b");
if(clientF == NULL) if(clientF == NULL)
{ {
printf("Erreur ouverture fichier"); printf("\nErreur ouverture fichier");
return localiserFichierClients(fichierClient); return localiserFichierClients(fichierClient);
} }
strcpy(fichierClient, repTmp); strcpy(fichierClient, repTmp);
@ -610,9 +587,67 @@ FILE * localiserFichierClients(char fichierClient[__STRLEN_FILE__])
int ajouterClientBin(FILE * clientF, Client * client) int ajouterClientBin(FILE * clientF, Client * client)
{ {
int posi; int posi;
unsigned long int nbClient;
fseek(clientF,0,SEEK_SET);
fread(&nbClient,sizeof(unsigned long int),1,clientF);
fseek(clientF,0,SEEK_SET);
nbClient++;
fwrite(nbClient, sizeof(unsigned long int),1,clientF);
fseek(clientF, 0, SEEK_END); fseek(clientF, 0, SEEK_END);
posi = ftell(clientF); posi = ftell(clientF);
fwrite(client, sizeof(Client), 1, clientF); fwrite(client, sizeof(Client), 1, clientF);
fseek(clientF, 0, SEEK_END);
fflush(clientF); fflush(clientF);
return posi; return posi;
} }
int compNP(char nom1[__STRLEN__], char prenom1[__STRLEN__],char nom2[__STRLEN__], char prenom2[__STRLEN__])
{
char np1[__STRLEN__*2];
char np2[__STRLEN__*2];
sprinf(np1,"%s%s", nom1, prenom1);
sprinf(np2,"%s%s", nom2, prenom2);
return strcmp(np1,np2);
}
void qSortClientBinByNP(FILE * clientF)
{
Client client1, client2;
size_t sizeClient = sizeof(Client);
Bool trier;
fseek(clientF, 0, SEEK_SET);
clearerr(clientF);
unsigned long int nbrClients = 0;
fread(&nbrClients,sizeof(nbrClients),1, clientF);
fread(&trier, sizeof(Bool),1,clientF);
if(!trier)
{
const unsigned int start = ftell(clientF);
while(!feof(clientF))
{
fread(&client1, sizeClient, 1, clientF);
if(feof(clientF)) //On est a la fin du fichier inutile d'aller plus loin
break;
fread(&client2, sizeClient, 1, clientF);
switch (compNP(client1.nom,client1.prenom,client2.nom,client2.prenom))
{
case -1:
fseek(clientF,-sizeClient);
break;
case 1:
fseek(clientF,-(2*sizeClient));
fwrite(client2,sizeClient,1,clientF);
fwrite(client1,sizeClient,1,clientF);
if((ftell(clientF)-(2*sizeClient))>start) //On est au début du fichier, inutile de comparer l'élem précédent
fseek(clientF,-sizeClient);
else
fseek(clientF,-(3*sizeClient);
clearerr(clientF);
break;
case 0:
fseek(clientF,-sizeClient);
break;
}
fflush(clientF);
}
}
}