# 121a - TD 5 - 05-03-21 PM ```cpp= // --- exercice 4 struct Fourniture { string libelle; int code, nbAchete, nbVendu; float prix; } void litFourniture(Fourniture &f){ cout << "entrez le code, le libelle et le prix "<< endl; cin >> f.code >> f.libelle >> f.prix ; do { cout << "nb achetes et nb vendus " << endl; cin >> f.nbAchat >> f.nbVendu ; } while((f.nbAchat < 0) or (f.nbVendu > f.nbAchat) ); } void ecritFourniture(Fourniture f){ cout << "la fourniture " << f.libelle << " de code " << f.code << " commandee " << f.nbAchat <<" fois, a ete vendue " << f.nbVendu <<" fois" <<endl; } void maj(Fourniture &f, int n){ f.nbVendu += n; } void stock(Fourniture f, int &n, float &montant){ n = f.nbAchat-f.nbVendu; montant = n * f.prix; } void ajouteAuCatalogue(vector<Fourniture> &c, Fourniture f){ c.push_back(f); } void bilan(vector<Fourniture> c){ int n; float m, total=0; Fourniture f; for (int k = 0; k<c.size(); k++){ f = c[k]; stock(f,n,m); cout << "Il y a encore " << n << " " << f.libelle << " en stock, pour " << m << "Euros" << endl; total += m; } cout << "Valeur totale du stock " << total; } void retireCatalogue(vector<Fourniture> &c, int code) { for(int i = 0; i < c.size(); i++){ if(c[i].code == code){ c.erase(i); } } } void retireCatalogue(vector<Fourniture> &c, int code){ int i=0; while (i <c.size() and c[i].code != code) {i++;} if (i==c.size()) { cout << "la fourniture de code " << code << "n existe pas "; } else { // on supprime la i-ieme donc on decale le reste du tableau for(int j=i; j+1<c.size(); j++) c[j] = c[j+1]; c.pop_back(); } } typedef vector<Fourniture> Catalogue; Fourniture f1= {"ciseaux", 101, 20, 4, 1.10}; Fourniture f2= {"stylo", 1024, 20, 7, 0.50}; void demoAffiche(){ ecritFourniture(f1); } void demoMaj(){ maj(f1,2); ecritFourniture(f1); } void demoStock(){ float m;int n; stock(f1,n,m); cout <<"stock de f1:" << n << " en euro, ca represente " << m << endl; } void demoBilan(){ vector<Fourniture> c; ajouteAuCatalogue(c, f1); ajouteAuCatalogue(c, f2); bilan(c); } void demoRetire(){ vector<Fourniture> c; ajouteAuCatalogue(c, f1); ajouteAuCatalogue(c, f2); bilan(c); retireCatalogue(c, 1024); bilan(c); } int main(){ demoAffiche(); demoMaj() ; demoStock() ; demoRetire(); return 0; } // ---- exercice 3 bool p(float x, float &r) { bool b; r = (x-1)*(2-x); if (r) >= 0 { r = sqrt(r); b = true; }else{ r = 0; b = false; } return b; } int main() { float a,s; for (i=0 ; i<2 ; i++) { cout << "saisir un valeur "; cin >> a; if(p(a,s)) { cout << "p(x) vaut " << s << endl; } else { cout << " p(" << a << ") non defini" << endl; } } return 0; } // -- exercice 2 void retrancher(int &a, int b) { a = a-b; } int modulo(int a, int b) { while(a >= b) { retrancher(a,b); } return a; } int main(){ int a,b; do { cout << "Choix 2 int positifs" << endl; cin >> a >> b; } while(b=<0 or a<b); cout << "a modulo b = "<< modulo(a, b) << endl; return 0; } int main() { int a,b,m; cout << "Choix : " <<endl; do { cin >> a,b; }while(b<=0 or a<b) m = modulo(a,b); cout << "reste a par b" << m << endl; return 0; } // -- EXERCICE 4 --> #include <iostream> #include <string> #include <vector> using namespace std; struct Supply{ int code; int bought; int sold; string name; float price; } void init(Supply &S){ cout << "Choisissez dans l'ordre; name, price, code, bought, sold." << endl; cin >> S.name >> S.price >> S.code >> S.bought >> S.sold; } void info(Supply S){ cout << "Nom: " << S.name << " Prix: " << S.price << " achetes: " << S.bought << " vendus: " << S.sold << " Code: " << S.code << endl; } void sell(Supply &S, int n){ S.sold += n; } void inStock(Supply S){ int stock = S.bought - S.sold; cout << "En stock: " << stock << " . Prix Equivalent: " << stock*S.price << endl; } void putInCatalog(Supply S, vector<Supply> &C){ C.push_back(S); } void checkup(vector<Supply> C){ for(Supply s : C){ info(s); inStock(s); } } void remove(vector<Supply> &C, int s_code){ for(int i = 0; i < code.size(); i++){ if(C[i].code == s_code){ C.erase(i); } } } int main(){ return 0; } // FIN ```