Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef PRODUIT_H
- #define PRODUIT_H
- #include <string>
- class Produit {
- public:
- Produit();
- int GetNumProduit(void);
- void SetNumProduit(int np);
- std::string GetNomProduit(void);
- float GetPrixProduit(void);
- void PigerProduit();
- private:
- int NumProduit; //Le numero de serie du produit
- std::string NomProduit; //Le nom du produit
- float PrixProduit; //Le prix du produit
- size_t split(const std::string &txt, std::vector<std::string> &strs, char ch); //Pour avoir le nom et le prix séparément
- int random(int min, int max);
- };
- #endif
- /////////////////////////////////////////////////////////////
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- #include <fstream>
- #include <vector>
- #include "Produit.h"
- #include <string>
- using namespace std;
- Produit::Produit() {
- //Je n'ai pas de bénéfice à le rendre plus complexe
- }
- int Produit::GetNumProduit(void) {
- return NumProduit;
- }
- void Produit::SetNumProduit(int np) {
- NumProduit = np;
- }
- string Produit::GetNomProduit(void) {
- return NomProduit;
- }
- float Produit::GetPrixProduit(void) {
- return PrixProduit;
- }
- void Produit::PigerProduit(void) {
- ifstream myfile("produits.dat");
- std::string Ligne;
- std::vector<std::string> Lignes;
- std::vector<std::string> ParametresFichier;
- int i = random(0,39);
- while (std::getline(myfile, Ligne))
- {
- Lignes.push_back(Ligne);
- }
- split(Lignes[i],ParametresFichier, ' ');
- NomProduit = ParametresFichier[0];
- PrixProduit = std::stof(ParametresFichier[1]);
- }
- int Produit::random(int min, int max) //range : [min, max)
- {
- static bool first = true;
- if (first)
- {
- srand( time(NULL) );
- first = false;
- }
- return min + rand() % (( max + 1 ) - min);
- }
- size_t Produit::split(const std::string &txt, std::vector<std::string> &strs, char ch)
- {
- size_t pos = txt.find( ch );
- size_t initialPos = 0;
- strs.clear();
- while( pos != std::string::npos ) {
- strs.push_back( txt.substr( initialPos, pos - initialPos ) );
- initialPos = pos + 1;
- pos = txt.find( ch, initialPos );
- }
- strs.push_back( txt.substr( initialPos, std::min( pos, txt.size() ) - initialPos + 1 ) );
- return strs.size();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement