445 lines
12 KiB
C
445 lines
12 KiB
C
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <stdint.h>
|
||
#include <string.h>
|
||
#include <stdio.h>
|
||
#include <conio.h>
|
||
#include <sys/stat.h>
|
||
#define __TVA__ 1.21
|
||
#define __REDUC__ 0.9
|
||
#define __MAXNBRART__ 3
|
||
|
||
#define __STRLEN__ 34
|
||
|
||
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__];
|
||
Adresse adresse;
|
||
Contact contact;
|
||
Date anniversaire;
|
||
Panier panier;
|
||
} Client;
|
||
|
||
|
||
|
||
|
||
void EncoderDate(Date *);
|
||
int DateValide (Date *);
|
||
void afficherArticle(Article * article, unsigned int nbrArt, FILE * file);
|
||
void encoderArticle(Article * article);
|
||
void encoderClient(Client *);
|
||
void afficherClient(Client *);
|
||
void encoderFacture(Panier *);
|
||
FILE * creerFichier(Date *, Client *);
|
||
Bool fichierExistant(char *);
|
||
void genererFacture(FILE *, Date *, Client *);
|
||
void cleanFgets(char*);
|
||
int itoaY(int i, char*);
|
||
int itoaDM(int i, char*);
|
||
|
||
|
||
int main()
|
||
{
|
||
char buf[5];
|
||
itoaY(1999,buf);
|
||
printf("%s\n", buf);
|
||
Date date;
|
||
FILE * file;
|
||
Client client;
|
||
do
|
||
EncoderDate(&date);
|
||
while(!DateValide(&date));
|
||
|
||
encoderClient(&client);
|
||
|
||
file = creerFichier(&date, &client);
|
||
if(file == NULL)
|
||
{
|
||
printf("Erreur lors de l'ouvertur !\n");
|
||
return -1;
|
||
}
|
||
|
||
|
||
client.panier.article = (Article *)malloc(sizeof(Article));
|
||
client.panier.nbrArt = 1;
|
||
if(client.panier.article == NULL)
|
||
{
|
||
printf("ERREUR ALLOCATION !");
|
||
free(client.panier.article);
|
||
return -1;
|
||
}
|
||
|
||
|
||
|
||
//En t<>te de la facture
|
||
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("%u\n", client.panier.nbrArt);
|
||
afficherArticle(client.panier.article,client.panier.nbrArt, file);
|
||
getch();
|
||
fclose(file);
|
||
free(client.panier.article);
|
||
return 0;
|
||
}
|
||
void EncoderDate(Date * date)
|
||
{
|
||
printf("Entrez la date sous cette forme : jj/mm/aaaa\n");
|
||
fflush(stdin); scanf("%d/%d/%d",&(date->jour),&(date->moi),&(date->annee));
|
||
}
|
||
|
||
int 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 1;
|
||
else return 0;
|
||
}
|
||
else
|
||
{
|
||
if(j >= 1 && j <= 31) return 1;
|
||
else return 0;
|
||
}
|
||
}
|
||
else if(m > 7 && m <= 12)
|
||
{
|
||
if(m %2 == 0)
|
||
{
|
||
if(j >= 1 && j <= 31) return 1;
|
||
else return 0;
|
||
}
|
||
else
|
||
{
|
||
if(j >= 1 && j <= 30) return 1;
|
||
else return 0;
|
||
}
|
||
}
|
||
else return 0;
|
||
}
|
||
else
|
||
{
|
||
if(a %100 == 0)
|
||
{
|
||
if(a %400 == 0) bis = 1;
|
||
else bis = 0;
|
||
}
|
||
else
|
||
{
|
||
if(a %4 == 0)bis = 1;
|
||
else bis = 0;
|
||
}
|
||
if(bis == 1)
|
||
{
|
||
if(j >= 1 && j <= 29) return 1;
|
||
else return 0;
|
||
}
|
||
else
|
||
{
|
||
if(j >= 1 && j <= 28) return 1;
|
||
else return 0;
|
||
}
|
||
}
|
||
}
|
||
|
||
void afficherArticle(Article * article, unsigned int nbrArt,FILE * file)
|
||
{
|
||
unsigned int i;
|
||
float prixTot=0;
|
||
fprintf(file,"\n|---------------------------------------------------------------------------------------------|\
|
||
\n| Designation | prixHTVA | Quantite | 10%% | prixTVAC |");
|
||
printf("\n|---------------------------------------------------------------------------------------------|\
|
||
\n| Designation | prixHTVA | Quantite | 10%% | prixTVAC |");
|
||
for(i=0;i<nbrArt;i++)
|
||
{
|
||
printf("\n|%34s|%12.2f Eur |%14d| %c |%12.2f |",\
|
||
article[i].deisgnation,article[i].prixHTVA,article[i].quant\
|
||
,(article[i].quant>=20)?'x':' ',\
|
||
(article[i].quant>=20)?article[i].prixHTVA*__TVA__*__REDUC__*article[i].quant:article[i].prixHTVA*__TVA__*article[i].quant);
|
||
fprintf(file,"\n|%34s|%12.2f Eur |%14d| %c |%12.2f |",\
|
||
article[i].deisgnation,article[i].prixHTVA,article[i].quant\
|
||
,(article[i].quant>=20)?'x':' ',\
|
||
(article[i].quant>=20)?article[i].prixHTVA*__TVA__*__REDUC__*article[i].quant:article[i].prixHTVA*__TVA__*article[i].quant);
|
||
|
||
prixTot += (article[i].quant>=20)?article[i].prixHTVA*__TVA__*__REDUC__*article[i].quant:article[i].prixHTVA*__TVA__*article[i].quant;
|
||
}
|
||
printf("\n|---------------------------------------------------------------------------------------------|");
|
||
printf("\nPrix totale :%f", prixTot);
|
||
|
||
fprintf(file,"\n|---------------------------------------------------------------------------------------------|");
|
||
fprintf(file,"\nPrix totale :%f", prixTot);
|
||
}
|
||
void encoderArticle(Article * article)
|
||
{
|
||
printf("\nVeuillez entrer le nom : ");
|
||
scanf("%34s", article->deisgnation);
|
||
printf("\nVeuillez entrer le prix a l'unite hors TVA : ");
|
||
scanf("%f", &article->prixHTVA);
|
||
printf("\nVeuillez entrer la quantite desiree : ");
|
||
fflush(stdin);
|
||
scanf("%d", &article->quant);
|
||
}
|
||
|
||
|
||
|
||
|
||
void encoderClient(Client * client)
|
||
{
|
||
printf("Encoage Client\n");
|
||
fflush(stdin);
|
||
if(!(printf("Entrez la rue :") && !fflush(stdin) && fgets(client->adresse.rue,__STRLEN__,stdin) != NULL && \
|
||
printf("\nEntrez le numero :" ) && !fflush(stdin) && fgets(client->adresse.numero,__STRLEN__,stdin) != NULL && \
|
||
printf("\nEntrez la localité :") && !fflush(stdin) && fgets(client->adresse.localite,__STRLEN__,stdin) != NULL && \
|
||
printf("\nEntrez le code postal :") && !fflush(stdin) && scanf("%d", &(client->adresse.codePostal)) && \
|
||
printf("\nNom client :") && !fflush(stdin) && fgets(client->nom,__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);
|
||
if(!(printf("\n Entrez le numéro de tél. :") && !fflush(stdin) &&fgets(client->contact.telephone,13,stdin) && \
|
||
printf("\nEntrez l'adresse email :") && !fflush(stdin) &&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);
|
||
printf("\n Anniversaire client :");
|
||
do
|
||
EncoderDate(&(client->anniversaire));
|
||
while(!DateValide(&(client->anniversaire)));
|
||
}
|
||
void afficherClient(Client *client)
|
||
{
|
||
printf("Client : %s\n", client->nom);
|
||
printf("rue : %s",client->adresse.rue);
|
||
printf("numero : %s",client->adresse.numero);
|
||
printf("localité : %s",client->adresse.localite);
|
||
printf("code postal : %d",client->adresse.codePostal);
|
||
printf("numéro de tél. : %s",client->contact.telephone);
|
||
printf("adresse email : %s",client->contact.email);
|
||
|
||
}
|
||
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)");
|
||
fflush(stdin);
|
||
scanf("%c", &continuer);
|
||
if(panier->nbrArt < 3 && (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
|
||
{
|
||
system("cls");
|
||
printf("Vous avez ateint le nombre d'article maximum !\n\n");
|
||
break;
|
||
}
|
||
}
|
||
}while((continuer|0b00100000) == 'y');
|
||
}
|
||
FILE * creerFichier(Date * date, Client * client)
|
||
{
|
||
char cat[__STRLEN__*+15]; //Dans l'ideal malloc pour un nombre de fichier illimiter mais flem ...
|
||
cat[0] = '\0';
|
||
char i;
|
||
char buf[5];
|
||
strcat(cat,client->nom);
|
||
printf("%s\n", cat);
|
||
itoaY(date->annee, buf);
|
||
printf("%s %s\n", cat, buf);
|
||
strcat(cat,buf);
|
||
printf("%s\n", cat);
|
||
itoaDM(date->moi, buf);
|
||
strcat(cat, buf);
|
||
printf("%s\n", cat);
|
||
itoaDM(date->jour, buf);
|
||
strcat(cat, buf);
|
||
printf("%s\n", cat);
|
||
strcat(cat,"_");
|
||
for(i='1'; i<= '9'; i++)//Le caractère 10 n'existe pas, flem de coder pour avoir plus que 9 et aloc dynamique nécessaire
|
||
{
|
||
char itmp[2] = {i,'\0'};
|
||
strcat(cat,itmp);
|
||
if(fichierExistant(cat))
|
||
{
|
||
char * p = strchr(cat, i); //Renvois l'adresse de i\0
|
||
//cat contient actuellement nom+date+_i\0
|
||
*p = '\0';
|
||
//Maintenant cat contient actuellement nom+date+_\0\0
|
||
//Si on remplace par '' strcat va ajouter au \0 donc on aura nom+date+_+''+QQChose\0
|
||
//On aura donc un caractère en plus a chaque tour de boucle ...
|
||
}
|
||
else
|
||
{
|
||
printf("%s\n", cat);
|
||
FILE * f = fopen(cat,"w");
|
||
if(f==NULL)
|
||
{
|
||
printf("Erreur lors de l'ouverture du fichier");
|
||
exit(-1);
|
||
return f;
|
||
}
|
||
}
|
||
}
|
||
return NULL;
|
||
}
|
||
Bool fichierExistant(char * filename)
|
||
{
|
||
FILE * f = fopen(filename, "r");
|
||
if(f==NULL)
|
||
return 0;
|
||
else
|
||
return 1;
|
||
}
|
||
void genererFacture(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|---------------------------------------------------------------------------------------------|\
|
||
\n| Designation | prixHTVA | Quantite | 10%% | prixTVAC |");
|
||
|
||
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);
|
||
}
|
||
|
||
|
||
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]++;
|
||
}
|
||
//printf("%s, %d, %d, %d, %d, j%d\n",buf,buf[3],buf[2],buf[1],buf[0],j );
|
||
}
|
||
if(j>9999)
|
||
return 0;
|
||
else
|
||
return 1;
|
||
}
|
||
int itoaDM(int i, char * buf)
|
||
{
|
||
buf[0] = '0';
|
||
buf[1] = '0';
|
||
buf[2] = '\0';
|
||
printf("%s\n", buf);
|
||
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;
|
||
}
|