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;
typedef struct{
char nom[__STRLEN__];
char prenom[__STRLEN__];
Adresse adresse;
Contact contact;
Date anniversaire;
@ -63,22 +64,25 @@ Bool clean_keyboard();
int ageClient(Client *, Date *);
int pointsFidelite(float);
void gestionFacture(Client *, Date *);
Client * rechercheClient(char *, FILE *, Date *);
Client * rechercheClient(char *, char *, FILE *, Date *);
Bool gestionClient(Client *, Date *);
FILE * localiserFichierClients(char *);
int ajouterClientBin(FILE *, Client * client);
int ajouterClientBin(FILE *, Client *);
int compNP(char *, char *,char *, char *);
void qSortClientBinByNP(FILE *);
int main()
{
//printf("%d\n", pointsFidelite(52.23));
FILE * clientF;
Date date;
char tmp;
Bool w;
time_t curTimeStamp = time(NULL);
struct tm *locTime = localtime(&curTimeStamp);
locTime->tm_mon += 1;
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);
scanf("%c", &tmp);
if((tmp|0b00100000) != 'y')
@ -88,11 +92,11 @@ int main()
date.jour = locTime->tm_mday;
date.moi = locTime->tm_mon;
date.annee = locTime->tm_year;
// printf("%d/%d/%d\n", date->jour, date->moi, date->annee );
}
Client * client;
char nomClient[__STRLEN__];
char prenomClient[__STRLEN__];
char fichierClient[__STRLEN_FILE__];
int choixMenu;
clientF = localiserFichierClients(fichierClient);
@ -115,7 +119,9 @@ int main()
case 2:
printf("Nom du client (sensible a la casse) : ");
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)
{
w = gestionClient(client, &date);
@ -145,60 +151,19 @@ Bool dateValide(Date * date)
int j = date->jour;
int m = date->moi;
int a = date->annee;
int bis;
if(m != 2)
int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
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(m %2 == 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;
if( d <= daysinmonth[m-1] )
return 1;
else
return 0;
}
else
{
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;
}
}
return 0;
}
void encoderArticle(Article * article)
@ -216,19 +181,20 @@ void encoderArticle(Article * article)
void encoderClient(Client * client)
{
printf("Encoage Client\n");
printf("Encodage Client\n");
client->fidelite = 0;
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 la localité :") && clean_keyboard() && fgets(client->adresse.localite,__STRLEN__,stdin) != NULL && \
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");
exit(-1);
}
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) && \
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)
{
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("numero : %s\n",client->adresse.numero);
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)
{
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, "numero : %s\n",client->adresse.numero);
fprintf(file, "localité : %s\n",client->adresse.localite);
@ -533,16 +499,16 @@ void gestionFacture(Client * client, Date * date)
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 = 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
do
{
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))
return NULL;
else
@ -592,15 +558,26 @@ FILE * localiserFichierClients(char fichierClient[__STRLEN_FILE__])
clientF = fopen(repTmp, "r");
if(clientF == NULL)
{
char c;
fclose(clientF);
clientF = fopen(repTmp, "w");
fclose(clientF);
printf("\nLe fichier n'existe pas. Voulez-vous le creer (C) ou changer de repertoir (R) (C|R)?");
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");
if(clientF == NULL)
{
printf("Erreur ouverture fichier");
printf("\nErreur ouverture fichier");
return localiserFichierClients(fichierClient);
}
strcpy(fichierClient, repTmp);
@ -610,9 +587,67 @@ FILE * localiserFichierClients(char fichierClient[__STRLEN_FILE__])
int ajouterClientBin(FILE * clientF, Client * client)
{
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);
posi = ftell(clientF);
fwrite(client, sizeof(Client), 1, clientF);
fseek(clientF, 0, SEEK_END);
fflush(clientF);
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);
}
}
}