Created
November 16, 2011 13:31
-
-
Save titouanc/1370080 to your computer and use it in GitHub Desktop.
ULg - INFO2009 - Exercice fin cours partie 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Slides de cours - Partie 4 Exercice de fin | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#define between(min, n, max) ((n) >= (min) && (n) <= (max)) | |
#define MAX_ARTICLES 100 | |
typedef struct { | |
unsigned int code; | |
double price; | |
unsigned int remaining; | |
} Article; | |
/* | |
* [pre] Le tableau result est alloue, le fichier specifie contient une liste d'article au format specifie | |
* [post] Le tableau result contient un nombre d'articles egal au retour de la fonction | |
*/ | |
int Articles_load(const char *path, Article result[MAX_ARTICLES]){ | |
int i = 0; | |
FILE *input = fopen(path, "r"); | |
if (! input) | |
return 0; | |
while (fscanf(input, "%d %lf %d", | |
&(result[i].code), | |
&(result[i].price), | |
&(result[i].remaining)) == 3){ | |
if (between(0, result[i].code, MAX_ARTICLES-1) && result[i].price >= 0 && result[i].remaining >= 0) | |
i++; | |
} | |
fclose(input); | |
return i; | |
} | |
/* | |
* [pre] Le fichier specifie par +path+ est accessible en ecriture, +stock+ contient | |
+stock_len+ articles | |
* [post] Le fichier specifie par +path+ contient le stock au format specifie | |
*/ | |
int Articles_save(const char *path, Article stock[MAX_ARTICLES], int stock_len){ | |
FILE *output = fopen(path, "w"); | |
int i; | |
if (! output) | |
return 0; | |
for (i=0; i<stock_len; i++){ | |
if (stock[i].price == 0.0 && stock[i].remaining == 0) | |
continue; | |
fprintf(output, "%2d %.2lf %d\n", stock[i].code, stock[i].price, stock[i].remaining); | |
} | |
fclose(output); | |
return 1; | |
} | |
/* | |
* [pre] +list+ contient +stock_len+ articles | |
* [post] Renvoie un ptr sur l'article correspondant a +code+, ou NULL si non trouve | |
*/ | |
Article *Article_find(Article list[MAX_ARTICLES], unsigned int code, int stock_len){ | |
int i; | |
if (! between(0, code, MAX_ARTICLES-1)) | |
return NULL; | |
for (i=0; i<stock_len; i++) | |
if (list[i].code == code) | |
return &(list[i]); | |
return NULL; | |
} | |
/* | |
* [pre] +list+ contient +stock_len+ articles | |
* [post] Renvoie le prix de l'article +code+, ou -1.0 si l'article n'est pas dans la liste | |
*/ | |
double Article_price(Article list[MAX_ARTICLES], unsigned int code, int stock_len){ | |
Article *found = Article_find(list, code, stock_len); | |
return (found) ? found->price : -1.0; | |
} | |
/* | |
* [pre] +list+ contient +stock_len+ articles | |
* [post] Renvoie le nombre d'articles +code+ en stock, ou -1 si l'article n'est pas dans la liste | |
*/ | |
int Article_remaining(Article list[MAX_ARTICLES], unsigned int code, int stock_len){ | |
Article *found = Article_find(list, code, stock_len); | |
return (found) ? found->remaining : -1; | |
} | |
#define Article_available(list, code, stock_len) ((Article_remaining((list), (code), (stock_len)) > 0) ? 1 : 0) | |
/* | |
* [pre] +list+ contient +stock_len+ articles, +quantity+ est le nombre d'articles a acheter | |
* [post] Renvoie le nombre d'articles +code+ achetes (peut etre inferieur a +quantity+ si le nombre | |
d'articles en stock est inferieur a la quantite desiree) | |
*/ | |
unsigned int Article_destock(Article list[MAX_ARTICLES], unsigned int code, unsigned int quantity, int stock_len){ | |
Article *found = Article_find(list, code, stock_len); | |
unsigned int res; | |
if (! found) | |
return 0; | |
if (found->remaining < quantity){ | |
res = found->remaining; | |
found->remaining = 0; | |
return res; | |
} | |
found->remaining -= quantity; | |
return quantity; | |
} | |
/* | |
* [pre] +list+ contient +stock_len+ articles | |
* [post] Renvoie la valeur totale du stock en euros | |
*/ | |
double Stock_value(Article list[MAX_ARTICLES], int stock_len){ | |
int i; | |
double sum=0.0; | |
for (i=0; i<stock_len; i++) | |
sum += list[i].remaining * list[i].price; | |
return sum; | |
} | |
int main(int argc, char **argv){ | |
Article stock[MAX_ARTICLES]; | |
int running = 1; | |
int cmd; | |
unsigned int a, b; | |
char output_path[256] = {'\0'}; | |
int stock_len; | |
if (argc < 2){ | |
printf("Usage: %s DATABASE\n", argv[0]); | |
return 1; | |
} | |
stock_len = Articles_load(argv[1], stock); | |
if (! stock_len){ | |
printf("Impossible de charger le fichier %s !\n", argv[1]); | |
return 1; | |
} | |
while (running){ | |
printf("Que voulez-vous faire ?\n"); | |
printf(" * 1 : Affiche le prix d'un produit\n"); | |
printf(" * 2 : Affiche si un article est disponible ou non\n"); | |
printf(" * 3 : Affiche l'etat du stock d'un produit\n"); | |
printf(" * 4 : Permet d'acheter un produit\n"); | |
printf(" * 5 : Affiche la valeur actuelle du stock en euros\n"); | |
printf(" * 6 : Enregistre le stock et quitte le programme\n"); | |
printf("choix > "); | |
if (scanf("%d", &cmd) != 1){ | |
while (getchar() != '\n'); | |
continue; | |
} | |
if (! between(1, cmd, 6)){ | |
printf("\033[1mChoix %d non valide !\033[0m\n", cmd); | |
continue; | |
} | |
switch (cmd){ | |
case 1: | |
do printf("Numero de l'article > "); while (scanf("%u", &a) != 1); | |
printf("\033[1mPrix de l'article %d: %.2fEUR\033[0m\n", a, Article_price(stock, a, stock_len)); | |
break; | |
case 2: | |
do printf("Numero de l'article > "); while (scanf("%u", &a) != 1); | |
if (Article_available(stock, a, stock_len)) | |
printf("\033[1mL'article %d est encore en stock\033[0m\n", a); | |
else | |
printf("\033[1mL'article %d n'est pas disponible\033[0m\n", a); | |
break; | |
case 3: | |
do printf("Numero de l'article > "); while (scanf("%u", &a) != 1); | |
printf("\033[1m%d articles %d en stock\033[0m\n", Article_remaining(stock, a, stock_len), a); | |
break; | |
case 4: | |
do printf("Numero de l'article > "); while (scanf("%u", &a) != 1); | |
do printf("Quantite a acheter > "); while (scanf("%u", &b) != 1); | |
printf("\033[1mAchat de %d articles num. %d\033[0m\n", Article_destock(stock, a, b, stock_len), a); | |
break; | |
case 5: | |
printf("\033[1mValeur totale du stock: %.2fEUR\033[0m\n", Stock_value(stock, stock_len)); | |
break; | |
case 6: | |
running = 0; | |
break; | |
} | |
} | |
snprintf(output_path, 255, "%s.after", argv[1]); | |
Articles_save(output_path, stock, stock_len); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment