« Utilisateur:Zulul/Wikimodule:langage C++ niveau1:Etudiant zulul » : différence entre les versions

Contenu supprimé Contenu ajouté
Ligne 2 407 :
 
}
</pre>
<pre>
// 9.2 - Gestion de listes d'articles
 
//PRD.h - Scope produit-----------
#ifndef PRD_M
#define PRD_M
 
struct PRD{
char ref[9];
char den[99];
int qte;
};
 
void saisir_prd(PRD &e,char ref[]);
void aff_prd(PRD e);
 
#endif
//--------------------------------
 
//PRD.cpp - Fonctions produits----
#include "menu.h"
#include<iostream>
#include <cstdlib>
 
using namespace std;
 
void saisir_prd(PRD &e,char ref[]) {
 
if(strlen(ref)<3)
while(strlen(ref)<3){cout<<"Reference ? ";cin>>ref;}
else
strcpy(e.ref,ref);
strcpy(e.ref,ref);
cout<<"Tapez la denomination : ";cin>>e.den;
e.qte=0;
 
}
 
void aff_prd(PRD e) {
cout<<e.ref<<" "<<e.den<<" "<<e.qte<<endl;
}
//--------------------------------
 
//LST.h - Scope liste-------------
 
#ifndef LST_M
#define LST_M
#include "PRD.h"
 
const int lst_max=100;
 
struct LST {
int nb;
PRD lst_prd[lst_max];
};
 
LST init_lst();
bool add(LST &l);
bool sup(LST &l);
int ach(LST &l);
int ven(LST &l1,LST &l2);
void aff(LST l);
 
#endif
//--------------------------------
 
//LST.cpp - Fonctions listes------
 
#include "LST.h"
#include <iostream>
#include <cstring>
 
using namespace std;
 
LST init_lst() {
LST lst;lst.nb=0;
return lst;
}
 
bool add(LST &l) {
 
bool ok=0,ko=0;char ref[9];int i;
 
cout<<"nb avant : "<<l.nb<<endl;
if(l.nb<=lst_max) {
while(!ok) {
 
cout<<"reference produit ? "<<endl;cin>>ref;
for(i=0;i<(l.nb-1)&&(strcmp(l.lst_prd[i].ref,ref));i++);
if(strcmp(l.lst_prd[i].ref,ref)) {
saisir_prd(l.lst_prd[l.nb],ref);
ok=1;l.nb++;
cout<<"nb apres : "<<l.nb<<endl;
} else {
cout<<ref<<" deja present"<<endl;
}
}
} else {
ko=1;
}
return ko;
}
 
bool sup(LST &l) {
 
char ref[9];int i;bool ok=0;
 
while(!ok) {
cout<<"Produit a supprimer : "<<endl;cin>>ref;
for(i=0;(strcmp(l.lst_prd[i].ref,ref)!=0&&(i<l.nb));++i);
if(strcmp(l.lst_prd[i].ref,ref)==0) {
cout<<"sup "<<l.lst_prd[i].ref<<" - "<<ref<<endl;
ok=1;
for(i;i<l.nb;++i)
l.lst_prd[i]=l.lst_prd[i+1];
l.nb--;
}
}
 
return ok;
 
}
 
int ach(LST &l) {
 
int qte,i; char ref[9];bool ok=0;
while(!ok) {
cout<<"Produit ? Quantite ?"<<endl;cin>>ref;cin>>qte;
for(i=0;(strcmp(l.lst_prd[i].ref,ref)!=0&&(i<l.nb));++i);
if(strcmp(l.lst_prd[i].ref,ref)==0){
l.lst_prd[i].qte+=qte;
ok=1;
}
}
 
return qte;
 
}
 
int chk(LST l,char ref[]){
 
int pos=-1,i;
for(i=0;(strcmp(l.lst_prd[i].ref,ref)!=0&&(i<l.nb));++i);
if(strcmp(l.lst_prd[i].ref,ref)==0) pos=i;
return pos;
}
 
int ven(LST &l1,LST &l2) {
 
int qte,i,a; char ref[9];
cout<<"Produit ? Quantite ?"<<endl;cin>>ref;cin>>qte;
for(i=0;(strcmp(l1.lst_prd[i].ref,ref)!=0&&(i<l1.nb));++i);
if((strcmp(l1.lst_prd[i].ref,ref)==0)&&(l1.lst_prd[i].qte>0)) {
if(l1.lst_prd[i].qte-qte>-1) {
l1.lst_prd[i].qte-=qte;
(a=chk(l2,ref))!=101?a:a=l2.nb++;
strcpy(l2.lst_prd[a].ref,l1.lst_prd[i].ref);
l2.lst_prd[a].qte=((int)(qte));
}else{
(a=chk(l2,ref))!=101?a:a=l2.nb++;
qte=l2.lst_prd[a].qte=l1.lst_prd[i].qte;
l1.lst_prd[i].qte=0;
cout<<ref<<" stock epuisé"<<endl;qte=0;
}
}else{
cout<<ref<<" stock epuisé"<<endl;qte=0;
}
 
return qte;
 
}
 
void aff(LST l) {
 
cout<<"LISTE DES PRODUITS-------------------------------------------"<<endl;
for(int i=0;i<l.nb;++i)
aff_prd(l.lst_prd[i]);
 
}
//--------------------------------
 
//MENU.h - Scope menu-------------
#ifndef MENU_H
#define MENU_H
#include "LST.h"
 
void menu(LST &l, LST &l2);
int chx();
 
bool exe_chx(LST &art, LST &cad, int chx);
void menu(LST &l1, LST &l2);
 
#endif
//--------------------------------
 
//MENU.CPP - GESTION DES ARTICLES-
#include "menu.h"
#include<iostream>
#include<ctype.h>
 
using namespace std;
 
int chx() {
 
char i;
cout<<"1. Ajouter un produit"<<endl;
cout<<"2. Afficher les produits."<<endl;
cout<<"3. Supprimer un produit"<<endl;
cout<<"4. Acheter les produits"<<endl;
cout<<"5. Vendre un produit"<<endl;
cout<<"6. Quitter"<<endl;
while(!(isdigit(i))||(i-48<1)||(i-48>6)){
cout<<"Votre choix : ";cin>>i;
}
return ((int)(i-48));
 
}
 
void menu(LST &l1, LST &l2) {
 
bool ok=0;
int i;
do {
i=chx();
ok=exe_chx(l1,l2,i);
}while(!ok);
 
}
 
bool exe_chx(LST &art, LST &cad, int chx) {
 
bool ok=0;
 
cout<<"art : "<<art.nb<<endl;
switch(chx) {
 
case 1:
if(add(art)) cout<<"La liste est pleine"<<endl;break;
case 2:
aff(art);break;
case 3:
sup(art);break;
case 4:
ach(art);break;
case 5:
ven(art,cad);break;
case 6:
ok=1;break;
 
}
 
return ok;
 
}
//--------------------------------
 
//main.cpp Appli------------------
#include <cstdlib>
#include <iostream>
#include "MENU.h";
 
using namespace std;
 
int main(int argc, char *argv[]) {
 
LST article, caddy;
article=init_lst();
caddy=init_lst();
menu(article, caddy);
system("PAUSE");
return 0;
 
}
//--------------------------------
</pre>