Vous êtes sur la page 1sur 5

//1) =====================================

typedef struct produit{

char label [50];


int Qnt;
double PrixU;

}produit;

//2)======================================
//a)
/*
typedef struct maillon{

produit pro;
struct maillon *next;

}maillon;

//b)
typedef struct stock{

maillon *head;
maillon *queue;

}ListStockS;

*/
//3)=================================
//a)
typedef struct maillonD{

produit pro;
struct maillonD *next;
struct maillonD *prev;

}maillonD;

//b)

typedef struct ListStockD{

maillonD *first;
maillonD *last;
int taille;

}stock;

//c)

stock *creer_stock(){

stock *pm=malloc(sizeof(stock));
if(pm==NULL)
return NULL;
pm->first=NULL;
pm->last=NULL;
pm->taille=0;

return pm;
}

maillonD* creer_maillon(produit prod){

maillonD *m=malloc(sizeof(maillonD));

if(m==NULL)
return NULL;
m->pro=prod;
m->next=NULL;
m->prev=NULL;

return m;

// acheter fonction :
stock *ajouter(stock *st,produit pro,int Qnt,double prixU){

maillonD *m=creer_maillon(pro);
maillonD *temp=st->first;

if(st->first==NULL){
m->pro.Qnt=Qnt;
m->pro.PrixU=prixU;
st->first=st->last=m;
st->taille++;
return st;
}

else

while(temp!=NULL){

if(temp->pro.label==m->pro.label){

temp->pro.Qnt=(temp->pro.Qnt)+(m->pro.Qnt);
temp->pro.PrixU=m->pro.PrixU;
free(m);
return st;

}
temp=temp->next;

m->pro.Qnt=Qnt;
m->pro.PrixU=prixU;
m->next=st->first;
m->prev=NULL;
st->first->prev=m;
st->first=m;
st->taille++;

return st;

//supprimer fonction :

stock* supprimer(stock *st,produit pro){


if(st->first==NULL)
return NULL;
maillonD *temp=st->first;
//delet first
if(st->first->pro.label==pro.label){

st->first=st->first->next;
st->first->prev=NULL;
free(temp);
st->taille--;
return st;

while(temp!=NULL){

if(temp->pro.label==pro.label){
break;

temp=temp->next;

//delete last
if(temp->next==NULL){

st->last=st->last->prev;
st->last->next=NULL;
free(temp);
st->taille--;
return st;

//delete middle
else{

temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
st->taille--;
return st;
}

return NULL;
}

//existe

stock* Existe(stock *st,produit pro){

if(st->first==NULL)
return NULL;

maillonD *temp=st->first;
while(temp!=NULL){

if(temp->pro.label==pro.label)
return st;

temp=temp->next;
}

return NULL;

//vendre

stock* vendre(stock *st,produit pro,int Nb_unites){

if(st->first==NULL)
return NULL;
maillonD *temp=st->first;
if(Existe(st,pro)==NULL){
printf("le produit n'existe pas dans le stock !");
}

else{

while(temp!=NULL){

if(temp->pro.label==pro.label)
break;

temp=temp->next;

temp->pro.Qnt=(temp->pro.Qnt)-Nb_unites;
if(temp->pro.Qnt==0)
st=supprimer(st,pro);

return st;
}
}

//prix stock :
double PrixStock(stock *st){

if(st->first==NULL)
return 0;
double k=0;
maillonD *temp=st->first;

while(temp!=NULL){

k=k+temp->pro.PrixU;

temp=temp->next;
}
return k;

//affichage :
void affichage(stock *st){

if(st->first==NULL)
printf("le stock est vide !");

maillonD *temp=st->first;
while(temp!=NULL){

printf("|%s| |%d| |%.0lf|->",temp->pro.label,temp->pro.Qnt,temp-


>pro.PrixU);
temp=temp->next;

Vous aimerez peut-être aussi