Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #ifndef PRODUIT_H
  2.  
  3. #define PRODUIT_H
  4.  
  5.  
  6.  
  7. #include <string>
  8.  
  9.  
  10.  
  11. class Produit {
  12.  
  13.    
  14.  
  15. public:
  16.  
  17.         Produit();
  18.  
  19.     int GetNumProduit(void);
  20.  
  21.     void SetNumProduit(int np);
  22.  
  23.     std::string GetNomProduit(void);
  24.  
  25.     float GetPrixProduit(void);
  26.  
  27.     void PigerProduit();
  28.  
  29.  
  30.  
  31. private:
  32.  
  33.     int NumProduit;     //Le numero de serie du produit
  34.  
  35.     std::string NomProduit; //Le nom du produit
  36.  
  37.     float PrixProduit;  //Le prix du produit
  38.     size_t split(const std::string &txt, std::vector<std::string> &strs, char ch); //Pour avoir le nom et le prix séparément
  39.     int random(int min, int max);
  40.  
  41. };
  42.  
  43. #endif
  44.  
  45.  
  46. /////////////////////////////////////////////////////////////
  47. #include <iostream>
  48. #include <stdlib.h>
  49. #include <time.h>
  50. #include <fstream>
  51. #include <vector>
  52. #include "Produit.h"
  53. #include <string>
  54.  
  55. using namespace std;
  56.  
  57.  
  58.  
  59. Produit::Produit() {
  60.  //Je n'ai pas de bénéfice à le rendre plus complexe
  61. }
  62.  
  63. int Produit::GetNumProduit(void) {
  64.   return NumProduit;
  65. }
  66.  
  67. void Produit::SetNumProduit(int np) {
  68.    NumProduit = np;
  69. }
  70.  
  71. string Produit::GetNomProduit(void) {
  72.     return NomProduit;
  73. }
  74.  
  75. float Produit::GetPrixProduit(void) {
  76.     return PrixProduit;
  77. }
  78.    
  79. void Produit::PigerProduit(void) {
  80.  ifstream myfile("produits.dat");
  81.  std::string Ligne;
  82.  std::vector<std::string> Lignes;
  83.  std::vector<std::string> ParametresFichier;
  84.  int i = random(0,39);
  85.  
  86.  while (std::getline(myfile, Ligne))
  87.  {
  88.    Lignes.push_back(Ligne);
  89.  }
  90.  
  91.  split(Lignes[i],ParametresFichier, ' ');
  92.  
  93.  NomProduit = ParametresFichier[0];
  94.  PrixProduit = std::stof(ParametresFichier[1]);
  95.  
  96. }
  97.  
  98. int Produit::random(int min, int max) //range : [min, max)
  99. {
  100.    static bool first = true;
  101.    if (first)
  102.    {  
  103.       srand( time(NULL) );
  104.       first = false;
  105.    }
  106.    return min + rand() % (( max + 1 ) - min);
  107. }
  108.  
  109. size_t Produit::split(const std::string &txt, std::vector<std::string> &strs, char ch)
  110. {
  111.     size_t pos = txt.find( ch );
  112.     size_t initialPos = 0;
  113.     strs.clear();
  114.  
  115.    
  116.     while( pos != std::string::npos ) {
  117.         strs.push_back( txt.substr( initialPos, pos - initialPos ) );
  118.         initialPos = pos + 1;
  119.  
  120.         pos = txt.find( ch, initialPos );
  121.     }
  122.  
  123.    
  124.     strs.push_back( txt.substr( initialPos, std::min( pos, txt.size() ) - initialPos + 1 ) );
  125.  
  126.  
  127.     return strs.size();
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement