This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock.c
72 lines (63 loc) · 1.5 KB
/
stock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "stock.h"
#include "raler.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TITRE_S 10
void free_stock(struct stock *st)
{
free(st->livres);
free(st->disp);
}
void init_stock(int n, char *argv[], struct stock *st)
{
st->n = n;
st->livres = (char *) calloc(n * TITRE_S, sizeof(char));
st->disp = (int *) malloc(n * sizeof(int));
if (st->livres == NULL || st->disp == NULL)
{
raler(0, "erreur malloc");
}
for (int i=0; i<n; ++i)
{
int n_ecrits = snprintf(&st->livres[i*TITRE_S], TITRE_S, "%s", argv[i]);
st->disp[i] = 1;
if (n_ecrits > TITRE_S)
{
free_stock(st);
raler(0, "Erreur : Titre %s trop long", argv[i]);
}
}
}
int reserver_livre(const char *titre, struct stock *st)
{
int ind = -1;
for (int i=0; i<st->n; ++i)
{
if ((strcmp(&st->livres[i*TITRE_S], titre) == 0) && st->disp[i])
{
ind = i;
st->disp[i] = 0;
break;
}
}
return ind;
}
int est_disponible(const char *titre, const struct stock *st)
{
int ind = -1;
for (int i=0; i<st->n; ++i)
if ((strcmp(&st->livres[i*TITRE_S], titre) == 0) && st->disp[i])
{
ind = i;
break;
}
return ind;
}
void afficher_stock(const struct stock *st)
{
printf("Stock disponible : ");
for (int i = 0; i < st->n; ++i)
if (st->disp[i]) printf("%s ", &st->livres[i*TITRE_S]);
printf("\n\n");
}