Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Classe ESTRAZIONE - estrazione.cpp

estrazione.cpp

Caricato da: AldoBaldo
Scarica il programma completo

  1. // Classe ESTRAZIONE - 2013-15 di Aldo Carpanelli
  2. // versione 1.0.0 - 09/07/2013
  3. // versione 1.0.1 - 22/01/2015
  4.  
  5. #include "estrazione.h"
  6. #include <stdlib.h>
  7. #include <windows.h>
  8.  
  9.  
  10. static int srand_invocata = 0;
  11. static const char ESTRAZIONE_kErrMemInsuff[] = "memoria insufficiente";
  12.  
  13.  
  14. ESTRAZIONE::ESTRAZIONE() {
  15.     v = 0;
  16.  
  17.     inizializza( 2 ); // se son meno non e' possibile estrarre!
  18. }
  19.  
  20.  
  21. ESTRAZIONE::ESTRAZIONE( short nEl ) {
  22.     v = 0;
  23.  
  24.     inizializza( nEl );
  25. }
  26.  
  27.  
  28. ESTRAZIONE::~ESTRAZIONE() {
  29.     if( v ) delete[] v;
  30. }
  31.  
  32.  
  33. ESTRAZIONE::ESTRAZIONE(const ESTRAZIONE& other) {
  34.     short i, *ptr_nuovo, *ptr_other;
  35.  
  36.     v = 0;
  37.  
  38.     inizializza( other.qv );
  39.  
  40.     ptr_nuovo = v;
  41.     ptr_other = other.v;
  42.  
  43.     for( i=0; i<qv; ++i )
  44.         *ptr_nuovo++ = *ptr_other++;
  45.  
  46.     qv = other.qv;
  47.     ue = other.ue;
  48.     de = other.de;
  49. }
  50.  
  51.  
  52. ESTRAZIONE& ESTRAZIONE::operator=(const ESTRAZIONE& rhs) {
  53.     short i, *ptr_nuovo, *ptr_rhs;
  54.  
  55.     if (this == &rhs) return *this; // gestisce l'autoassegnamento
  56.  
  57.     inizializza( rhs.qv );
  58.  
  59.     ptr_nuovo = this->v;
  60.     ptr_rhs = rhs.v;
  61.  
  62.     for( i=0; i<de; ++i )
  63.         *ptr_nuovo++ = *ptr_rhs++;
  64.  
  65.     ue = rhs.ue;
  66.     de = rhs.de;
  67.  
  68.     return *this;
  69. }
  70.  
  71.  
  72. void ESTRAZIONE::inizializza( short nEl ) {
  73.     if( !srand_invocata )
  74.         { srand(GetTickCount()); srand_invocata = 1; }
  75.  
  76.     qv = nEl;
  77.     if( qv < 0 ) qv = -qv;
  78.     if( qv < 2 ) qv = 2; // se son meno non e' possibile estrarre!
  79.  
  80.     if( v ) delete[] v;
  81.     v = new short[qv];
  82.     if( !v ) throw ESTRAZIONE_kErrMemInsuff;
  83.  
  84.     reset();
  85.  
  86.     ue = -1;
  87.     de = qv;
  88. }
  89.  
  90.  
  91. void ESTRAZIONE::reset( void ) {
  92.     short i, *ptr = v;
  93.  
  94.     for( i=0; i<qv; ++i )
  95.         *ptr++ = i;
  96.  
  97.     de = qv;
  98. }
  99.  
  100.  
  101. short ESTRAZIONE::estrai( void ) {
  102.     short posizione, estratto;
  103.     short *ptr, *stop;
  104.  
  105.     if( !de ) reset();
  106.  
  107.     do {
  108.         posizione = rand() % de;
  109.         estratto = *(v+posizione);
  110.     } while( estratto == ue );
  111.  
  112.     stop = v + de - 1;
  113.     ptr = v + posizione;
  114.  
  115.     while( ptr != stop )
  116.         { *ptr = *(ptr+1); ++ptr; }
  117.  
  118.     ue = estratto;
  119.     --de;
  120.  
  121.     return estratto;
  122. }
  123.  
  124.  
  125. short ESTRAZIONE::ultimo_estratto( void ) {
  126.     return ue;
  127. }