/* * Fichier client : ------------------------------------------------------------ |UINTL NBClients|Bool Tri| Client 0 | Client 1 | .......ETc| ------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #define __TVA__ 1.21 #define __REDUC__ 0.9 #define __MAXNBRART__ 3 #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__]; float prixHTVA; int quant; } Article; typedef struct { Article * article; unsigned int nbrArt; } Panier; typedef struct{ int jour,moi,annee; } Date; typedef struct{ char rue[__STRLEN__],numero[__STRLEN__],localite[__STRLEN__]; int codePostal; } Adresse; typedef struct{ char telephone[13],email[__STRLEN__]; } Contact; typedef struct{ char nom[__STRLEN__]; char prenom[__STRLEN__]; Adresse adresse; Contact contact; Date anniversaire; int fidelite; Panier panier; } Client; void encoderDate(Date *, char *); Bool dateValide (Date *); void encoderArticle(Article *); void encoderClient(Client *); void afficherClient(Client *, Date *); void encoderFacture(Panier *); 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 *); Client * rechercheClient(char *, char*, FILE *, Date *); Bool gestionClient(Client *, Date *); int compNP(char *, char *,char *, char *); 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() { //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') encoderDate(&date, "Date facturation"); else { 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__],prenomClient[__STRLEN__]; char fichierClientDat[__STRLEN_FILE__]; int choixMenu; localiserFichiersClients(fichierClientDat, clientF); while(w) { printf("1. Nouveau client\n"); printf("2. Client enregistré\n"); do{ printf("Choix (1|2):");clean_keyboard();scanf("%d", &choixMenu); }while(!(choixMenu<=2 && choixMenu>=1)); switch (choixMenu) { case 1: client = malloc(sizeof(Client)); encoderClient(client); afficherClient(client, &date); 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) : "); clean_keyboard();scanf("%s", prenomClient); client = rechercheClient(nomClient,prenomClient, clientF[__TAB_CLIENTDAT_FILE__], &date); if(client != NULL) { w = gestionClient(client, &date); } else { printf("Client non trouvé !\n"); printf("%d",(int)sizeof(Client)); } break; } } fclose(clientF[__TAB_CLIENTDAT_FILE__]); return 0; } void encoderDate(Date * date, char invite[__STRLEN__]) { do { printf("%s\n", invite); printf("Entrez la date sous cette forme : jj/mm/aaaa\n"); scanf("%d/%d/%d",&(date->jour),&(date->moi),&(date->annee)); } while(!dateValide(date)); } Bool dateValide(Date * date) { int j = date->jour; int m = date->moi; int a = date->annee; int bis; if(m != 2) { 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; } 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) { printf("\nVeuillez entrer le nom : "); clean_keyboard(); scanf("%34s", article->deisgnation); printf("\nVeuillez entrer le prix a l'unite hors TVA : "); clean_keyboard(); scanf("%f", &article->prixHTVA); printf("\nVeuillez entrer la quantite desiree : "); clean_keyboard(); scanf("%d", &article->quant); } void encoderClient(Client * client) { 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("\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->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))) { printf("\nErreur lors de l'encodage des coo de contact"); exit(-1); } cleanFgets(client->contact.telephone);cleanFgets(client->contact.email); encoderDate(&(client->anniversaire), "\nAnniversaire client:"); } void afficherClient(Client *client, Date *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); printf("code postal : %d\n",client->adresse.codePostal); printf("numéro de tél. : %s\n",client->contact.telephone); printf("adresse email : %s\n",client->contact.email); printf("\nPoints de fidelite aqui : %d\n", client->fidelite); } void sauvegarderClientTXT(Client *client, FILE * file, Bool ageAff, ...) //EcrireDonneesClient { 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) { char continuer = 'y'; do{ if(panier->nbrArt <= __MAXNBRART__) { encoderArticle(&(panier->article[panier->nbrArt-1])); printf("Y-a-t-il encore un article ?(Y|n)"); clean_keyboard(); scanf("%c", &continuer); if(panier->nbrArt < __MAXNBRART__ && (continuer|0b00100000) == 'y') { panier->nbrArt++; Article * tempArticle; tempArticle = (Article*)realloc(panier->article, sizeof(Article)*(panier->nbrArt)); if(panier->article == NULL) { printf("ERREUR ALLOCATION !"); free(tempArticle); free(panier->article); exit(-1); } else { panier->article = tempArticle; } } else if((continuer|0b00100000) == 'y') { system("cls"); printf("Vous avez ateint le nombre d'article maximum !\n\n"); break; } } }while((continuer|0b00100000) == 'y' && panier->nbrArt <= __MAXNBRART__); } FILE * creerFichier(Date * date, Client * client) { char fileName[__STRLEN__+16]; fileName[0] = '\0'; char dir[__STRLEN__]; int i; char buf[5]; printf("Dossier d'enregistrement : "); clean_keyboard(); scanf("%s", dir); //Toute cette partie aurais pus être remplacé par un sprintf strcat(fileName,client->nom); itoaY(date->annee, buf); strcat(fileName,buf); itoaDM(date->moi, buf); strcat(fileName, buf); itoaDM(date->jour, buf); strcat(fileName, buf); strcat(fileName,"_"); for(i=1; i<= 99; i++) { char fileNameTmp[__STRLEN__+16+255]; sprintf(fileNameTmp,"%s%s%d.txt",dir,fileName,i); if(!fichierExistant(fileNameTmp)) { FILE * f = fopen(fileNameTmp,"w"); if(f==NULL) { printf("Erreur lors de l'ouverture du fichier"); return creerFichier(date, client); } else return f; } } return NULL; } Bool fichierExistant(char * filename) { FILE * f = fopen(filename, "r"); fclose(f); if(f==NULL) return FALSE; else return TRUE; } void sauvergarderFacture(FILE * file, Date * date, Client * client) { int i; float prixTot = 0; fprintf(file,"La Bulle Informatique\nRoute du Condroz 78, 4123 Neupre\n"); fprintf(file,"\nFacture a payer\n"); fprintf(file,"Date de facturation :"); fprintf(file,"%02d/%02d/%02d", date->jour, date->moi, date->annee); fprintf(file, "\n Info client :"); sauvegarderClientTXT(client, file, 1,ageClient(client,date)); fprintf(file,"\n|---------------------------------------------------------------------------------------------|\ \n| Designation | prixHTVA | Quantite | 10%% | prixTVAC |\ \n|---------------------------------------------------------------------------------------------|"); for(i=0;client->panier.nbrArt>i;i++) { fprintf(file,"\n|%34s|%12.2f Eur |%14d| %c |%12.2f |",\ client->panier.article[i].deisgnation,client->panier.article[i].prixHTVA,client->panier.article[i].quant\ ,(client->panier.article[i].quant>=20)?'x':' ',\ (client->panier.article[i].quant>=20)?client->panier.article[i].prixHTVA*__TVA__*__REDUC__*client->panier.article[i].quant:client->panier.article[i].prixHTVA*__TVA__*client->panier.article[i].quant); prixTot += (client->panier.article[i].quant>=20)?client->panier.article[i].prixHTVA*__TVA__*__REDUC__*client->panier.article[i].quant:client->panier.article[i].prixHTVA*__TVA__*client->panier.article[i].quant; } fprintf(file,"\n|---------------------------------------------------------------------------------------------|"); fprintf(file,"\nPrix totale :%f", prixTot); fprintf(file, "\nPoints de fidelite aqui : %d\n", client->fidelite); } void afficherFacture(Date * date, Client * client) { int i; float prixTot = 0; printf("La Bulle Informatique\nRoute du Condroz 78, 4123 Neupre\n"); printf("\nFacture a payer\n"); printf("Date de facturation :"); printf("%02d/%02d/%02d", date->jour, date->moi, date->annee); printf("\n Info client :"); afficherClient(client, date); printf("\n|---------------------------------------------------------------------------------------------|\ \n| Designation | prixHTVA | Quantite | 10%% | prixTVAC |\ \n|---------------------------------------------------------------------------------------------|"); for(i=0;client->panier.nbrArt>i;i++) { printf("\n|%34s|%12.2f Eur |%14d| %c |%12.2f |",\ client->panier.article[i].deisgnation,client->panier.article[i].prixHTVA,client->panier.article[i].quant\ ,(client->panier.article[i].quant>=20)?'x':' ',\ (client->panier.article[i].quant>=20)?client->panier.article[i].prixHTVA*__TVA__*__REDUC__*client->panier.article[i].quant:client->panier.article[i].prixHTVA*__TVA__*client->panier.article[i].quant); prixTot += (client->panier.article[i].quant>=20)?client->panier.article[i].prixHTVA*__TVA__*__REDUC__*client->panier.article[i].quant:client->panier.article[i].prixHTVA*__TVA__*client->panier.article[i].quant; } printf("\n|---------------------------------------------------------------------------------------------|"); client->fidelite = pointsFidelite(prixTot); printf("\nPrix totale :%f", prixTot); printf("\nPoints de fidelite aqui : %d\n", client->fidelite); } void cleanFgets(char * str) { char *p = strchr(str, '\n'); if(p!=NULL) *p = '\0'; else { printf("Une erreur est arrivé lors du nettoygae \\0"); exit(-1); } } int itoaY(int i, char * buf) { buf [0] = '0'; buf [1] = '0'; buf [2] = '0'; buf [3] = '0'; buf [4] = '\0'; int j; if(!i) return 1; buf [3] = '1'; for(j=1; j<=i && j<=9999 ; j++) { if(!(j%1000)) { buf[0]++; buf[1]='0'; buf[2]='0'; buf[3]='0'; } else if(!(j%100)) { buf[1]++; buf[2]='0'; buf[3]='0'; } else if(!(j%10)) { buf[2]++; buf[3]='0'; } else { buf[3]++; } } if(j>9999) return 0; else return 1; } int itoaDM(int i, char * buf) { buf[0] = '0'; buf[1] = '0'; buf[2] = '\0'; int j; if(!i) return 1; buf[1]++; for(j=1; j<=i && j<=99 ; j++) { if(!(j%10)) { (buf[0])++; (buf[1])='0'; } else { (buf[1])++; } } if(j>99) return 0; else return 1; } Bool clean_keyboard() { fflush(stdin); // char c; //while ((c = getchar()) != '\n' && c != EOF) { } /* do { c = getc(stdin); printf("%c\n", c); } while (c != '\n' && c != EOF);*/ return 1; } int ageClient(Client * client, Date * date) { if((date->moi == client->anniversaire.moi && date->jour >= client->anniversaire.jour) || (date->moi > client->anniversaire.moi))//anniversaire passé { return (date->annee - client->anniversaire.annee); } else return (date->annee - client->anniversaire.annee - 1); } int pointsFidelite(float prix) { return (int)(prix/10); } void gestionFacture(Client * client, Date * date) { FILE * file; printf("%d/%d/%d\n", date->jour, date->moi, date->annee ); file = creerFichier(date, client); client->panier.article = (Article *)malloc(sizeof(Article)); client->panier.nbrArt = 1; if(client->panier.article == NULL) { printf("ERREUR ALLOCATION ARTICLE!"); free(client->panier.article); exit(-1); } encoderFacture(&client->panier); afficherFacture(date, client); sauvergarderFacture(file, date, client); // getch(); fclose(file); free(client->panier.article); } Client * rechercheClient(char nom[__STRLEN__], char prenom[__STRLEN__], FILE * clientF, Date * date) { Client * client = NULL; client = malloc(sizeof(Client)); fseek(clientF, 0, SEEK_SET); clearerr(clientF); //Clear EOF do { fread(client, sizeof(Client), 1, clientF); }while(!feof(clientF) && compNP(client->nom, client->prenom, nom, prenom)); if(feof(clientF)) return NULL; else return client; } Bool gestionClient(Client * client, Date * date) { char choix; while(1) { do { printf("A. Donnees client\n"); 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' || choix == 'e')); switch (choix) { case 'a': afficherClient(client, date); break; case 'b': break; case 'c': gestionFacture(client, date); break; case 'd': return 1; break; case 'e': return 0; break; } } } void localiserFichiersClients(char fichierClientDat[__STRLEN_FILE__], FILE ** clientF) { char rep[__STRLEN_FILE__]; char repTmp[2][__STRLEN_FILE__]; printf("Repertoir ou se trouve le fichier client.dat et client.txt"); clean_keyboard(); scanf("%s", rep); 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[__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') { 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 localiserFichiersClients(fichierClientDat, clientF); } else { 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"); localiserFichiersClients(fichierClientDat, clientF); } strcpy(fichierClientDat, repTmp[__TAB_CLIENTDAT_FILE__]); } int sauvegarderClientFile(FILE ** clientF, Client * client) { int posi; 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]; 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 clientBinToTXT(FILE * bin, FILE* txt) { fseek(bin, 0, SEEK_SET);fseek(txt, 0, SEEK_SET); fseek(bin, sizeof(unsigned long int), SEEK_CUR); fseek(bin, sizeof(Bool), SEEK_CUR); // fread() }