This commit is contained in:
Adrien VAN DAMME 2017-04-02 17:02:15 +02:00
parent 2a99d7a734
commit a52ee5d95e
1 changed files with 155 additions and 124 deletions

View File

@ -8,9 +8,6 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
@ -18,6 +15,7 @@
#include <sys/stat.h>
#include <time.h>
#include <string.h>
#include <stdarg.h>
#define __TVA__ 1.21
#define __REDUC__ 0.9
@ -26,6 +24,9 @@
#define __STRLEN__ 34
#define __STRLEN_FILE__ 500
#define __TAB_CLIENTDAT_FILE__ 0
#define __TAB_CLIENTTXT_FILE__ 1
typedef enum{FALSE,TRUE} Bool;
typedef struct {
char deisgnation[__STRLEN__];
@ -63,40 +64,38 @@ void encoderDate(Date *, char *);
Bool dateValide (Date *);
void encoderArticle(Article *);
void encoderClient(Client *);
void sauvegarderClient(Client *, FILE *, Date*);
void afficherClient(Client *, Date *);
void encoderFacture(Panier *);
FILE * creerFichier(Date *, Client *);
Bool fichierExistant(char *);
void sauvergarderFacture(FILE *, Date *, Client *);
void afficherFacture(Date *, Client *);
int pointsFidelite(float);
void gestionFacture(Client *, Date *);
void cleanFgets(char*);
int itoaY(int i, char*);
int itoaDM(int i, char*);
Bool clean_keyboard();
int ageClient(Client *, Date *);
int pointsFidelite(float);
void gestionFacture(Client *, Date *);
Client * rechercheClient(char *, char*, FILE *, Date *);
Bool gestionClient(Client *, Date *);
FILE * localiserFichierClients(char *);
int ajouterClientBin(FILE *, Client *);
int compNP(char *, char *,char *, char *);
void qSortClientBinByNP(FILE *);
void clientBinToTXT(FILE * bin, FILE* txt);
void localiserFichiersClients(char *, FILE **);
int sauvegarderClientFile(FILE **, Client * client);
FILE * creerFichier(Date *, Client *);
Bool fichierExistant(char *);
void sauvergarderFacture(FILE *, Date *, Client *);
void sauvegarderClientTXT(Client *, FILE *, Bool,...); //EcrireDonneesClient
void clientBinToTXT(FILE *, FILE*);
int main()
{
FILE * clientF;
//printf("%d\n", pointsFidelite(52.23));
FILE * clientF[2];//0=DAT;1=TXT
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')
@ -106,14 +105,14 @@ 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__];
char nomClient[__STRLEN__],prenomClient[__STRLEN__];
char fichierClientDat[__STRLEN_FILE__];
int choixMenu;
clientF = localiserFichierClients(fichierClient);
localiserFichiersClients(fichierClientDat, clientF);
while(w)
{
printf("1. Nouveau client\n");
@ -127,15 +126,15 @@ int main()
client = malloc(sizeof(Client));
encoderClient(client);
afficherClient(client, &date);
ajouterClientBin(clientF, client);
sauvegarderClientFile(clientF, client);
w = gestionClient(client, &date);
break;
case 2:
printf("Nom du client (sensible a la casse) : ");
clean_keyboard();scanf("%s", nomClient);
printf("\nPrenom du client (sensible a la casse) : ");
printf("\npRENOM du client (sensible a la casse) : ");
clean_keyboard();scanf("%s", prenomClient);
client = rechercheClient(nomClient, prenomClient clientF, &date);
client = rechercheClient(nomClient,prenomClient, clientF[__TAB_CLIENTDAT_FILE__], &date);
if(client != NULL)
{
w = gestionClient(client, &date);
@ -148,7 +147,7 @@ int main()
break;
}
}
fclose(clientF);
fclose(clientF[__TAB_CLIENTDAT_FILE__]);
return 0;
}
void encoderDate(Date * date, char invite[__STRLEN__])
@ -165,19 +164,60 @@ Bool dateValide(Date * date)
int j = date->jour;
int m = date->moi;
int a = date->annee;
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)
int bis;
if(m != 2)
{
if( d <= daysinmonth[m-1] )
return 1;
else
return 0;
if(m >= 1 && m <= 7)
{
if(m %2 == 0)
{
if(j >= 1 && j <= 30) return TRUE;
else return FALSE;
}
else
return 0;
{
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
{
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)
@ -195,13 +235,13 @@ void encoderArticle(Article * article)
void encoderClient(Client * client)
{
printf("Encodage Client\n");
printf("Encoage 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");
@ -229,15 +269,27 @@ void afficherClient(Client *client, Date *date)
printf("adresse email : %s\n",client->contact.email);
printf("\nPoints de fidelite aqui : %d\n", client->fidelite);
}
void sauvegarderClient(Client *client, FILE * file, Date *date)
void sauvegarderClientTXT(Client *client, FILE * file, Bool ageAff, ...) //EcrireDonneesClient
{
fprintf(file, "Client : %s %s (%d)\n", client->nom, client->prenom, ageClient(client, date));
va_list ap;
int age = 0;
va_start(ap, ageAff);
fprintf(file, "Client : %s %s", client->nom, client->prenom);
if(ageAff)
{
age = va_arg(ap,int);
fprintf(file," (%d)\n",age);
}
else
fprintf(file,"\n");
//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);
fprintf(file, "code postal : %d\n",client->adresse.codePostal);
fprintf(file, "numéro de tél. : %s\n",client->contact.telephone);
fprintf(file, "adresse email : %s\n",client->contact.email);
if(ageAff)
fprintf(file, "\nPoints de fidelite aqui : %d\n", client->fidelite);
}
void encoderFacture(Panier * panier)
@ -333,7 +385,7 @@ void sauvergarderFacture(FILE * file, Date * date, Client * client)
fprintf(file,"Date de facturation :");
fprintf(file,"%02d/%02d/%02d", date->jour, date->moi, date->annee);
fprintf(file, "\n Info client :");
sauvegarderClient(client, file, date);
sauvegarderClientTXT(client, file, 1,ageClient(client,date));
fprintf(file,"\n|---------------------------------------------------------------------------------------------|\
\n| Designation | prixHTVA | Quantite | 10%% | prixTVAC |\
\n|---------------------------------------------------------------------------------------------|");
@ -517,7 +569,7 @@ Client * rechercheClient(char nom[__STRLEN__], char prenom[__STRLEN__], FILE * c
{
Client * client = NULL;
client = malloc(sizeof(Client));
fseek(clientF, sizeof(unsigned long int)+sizeof(Bool), SEEK_SET); //On se possitionne apprès le nbr de client
fseek(clientF, 0, SEEK_SET);
clearerr(clientF); //Clear EOF
do
{
@ -537,9 +589,10 @@ Bool gestionClient(Client * client, Date * date)
do
{
printf("A. Donnees client\n");
printf("B. Facture\n");
printf("C. Retour\n");
printf("D. Quitter");
printf("B. Modification client\n");
printf("C. Facture\n");
printf("D. Retour\n");
printf("E. Quitter");
clean_keyboard();scanf("%c", &choix);
choix |= 0b00100000;
}while(!(choix == 'a' || choix == 'b' || choix == 'c' || choix == 'd'));
@ -548,123 +601,101 @@ Bool gestionClient(Client * client, Date * date)
afficherClient(client, date);
break;
case 'b':
gestionFacture(client, date);
break;
case 'c':
return 1;
gestionFacture(client, date);
break;
case 'd':
return 1;
break;
case 'e':
return 0;
break;
}
}
}
FILE * localiserFichierClients(char fichierClient[__STRLEN_FILE__])
void localiserFichiersClients(char fichierClientDat[__STRLEN_FILE__], FILE ** clientF)
{
FILE * clientF;
char rep[__STRLEN_FILE__];
char repTmp[__STRLEN_FILE__];
printf("Repertoir ou se trouve le fichier client.dat");
char repTmp[2][__STRLEN_FILE__];
printf("Repertoir ou se trouve le fichier client.dat et client.txt");
clean_keyboard();
scanf("%s", rep);
sprintf(repTmp,"%sclients.dat", rep);
clientF = fopen(repTmp, "r");
if(clientF == NULL)
sprintf(repTmp[__TAB_CLIENTDAT_FILE__],"%sclients.dat", rep);
sprintf(repTmp[__TAB_CLIENTTXT_FILE__],"%sclients.txt", rep);
clientF[__TAB_CLIENTDAT_FILE__] = fopen(repTmp[__TAB_CLIENTDAT_FILE__], "r");
clientF[__TAB_CLIENTTXT_FILE__] = fopen(repTmp[__TAB_CLIENTTXT_FILE__], "r");
if(clientF[__TAB_CLIENTDAT_FILE__] == NULL || clientF[__TAB_CLIENTTXT_FILE__] == NULL)
{
char c;
fclose(clientF);
printf("\nLe fichier n'existe pas. Voulez-vous le creer (C) ou changer de repertoir (R) (C|R)?");
fclose(clientF[__TAB_CLIENTDAT_FILE__]);
fclose(clientF[__TAB_CLIENTTXT_FILE__]);
printf("\nUn fichier n'existe pas. Voulez-vous les creers (C) ou changer de repertoir (R). (C|R)?");
if(!(clientF[__TAB_CLIENTDAT_FILE__] == NULL && clientF[__TAB_CLIENTTXT_FILE__] == NULL))
printf("Attention un des deux fichier a été détecter ! Il seront remis a 0 si vous les creers\nChoix (C|R):" );
clean_keyboard();scanf("%c", &c);
if(c&0b00100000 == '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);
clientF[__TAB_CLIENTDAT_FILE__] = fopen(repTmp[__TAB_CLIENTDAT_FILE__], "wb");
clientF[__TAB_CLIENTTXT_FILE__] = fopen(repTmp[__TAB_CLIENTTXT_FILE__], "w");
fclose(clientF[__TAB_CLIENTDAT_FILE__]);
fclose(clientF[__TAB_CLIENTTXT_FILE__]);
}
else
return localiserFichierClients(fichierClient);
localiserFichiersClients(fichierClientDat, clientF);
}
else
fclose(clientF);
clientF = fopen(repTmp, "r+b");
if(clientF == NULL)
{
fclose(clientF[__TAB_CLIENTDAT_FILE__]);
fclose(clientF[__TAB_CLIENTTXT_FILE__]);
}
clientF[__TAB_CLIENTDAT_FILE__] = fopen(repTmp[__TAB_CLIENTDAT_FILE__], "r+b");
clientF[__TAB_CLIENTTXT_FILE__] = fopen(repTmp[__TAB_CLIENTTXT_FILE__], "r+");
if(clientF[__TAB_CLIENTDAT_FILE__] == NULL || clientF[__TAB_CLIENTTXT_FILE__] == NULL)
{
printf("\nErreur ouverture fichier");
return localiserFichierClients(fichierClient);
localiserFichiersClients(fichierClientDat, clientF);
}
strcpy(fichierClient, repTmp);
return clientF;
strcpy(fichierClientDat, repTmp[__TAB_CLIENTDAT_FILE__]);
}
int ajouterClientBin(FILE * clientF, Client * client)
int sauvegarderClientFile(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);
fseek(clientF[__TAB_CLIENTDAT_FILE__], 0, SEEK_END);
fseek(clientF[__TAB_CLIENTTXT_FILE__], 0, SEEK_END);
posi = ftell(clientF[__TAB_CLIENTDAT_FILE__]);
fwrite(client, sizeof(Client), 1, clientF[__TAB_CLIENTDAT_FILE__]);
fflush(clientF[__TAB_CLIENTDAT_FILE__]);
sauvegarderClientTXT(client, clientF[__TAB_CLIENTTXT_FILE__], 0);
fflush(clientF[__TAB_CLIENTTXT_FILE__]);
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);
int renvois;
sprintf(np1,"%s%s", nom1, prenom1);
sprintf(np2,"%s%s", nom2, prenom2);
renvois = strcmp(np1,np2);
if(renvois>0)
renvois = 1;
else if(renvois<0)
renvois = -1;
return renvois;
}
void qSortClientBinByNP(FILE * clientF)
void clientBinToTXT(FILE *, FILE*)
{
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);
}
}
}
void clientBinToTXT(FILE * bin, FILE* txt)
{
fseek(bin, 0, SEEK_SET);fseek(txt, 0, SEEK_SET);