Don't like ads? PRO users don't see any ads ;-)
Guest

oSZTÁLY

By: a guest on May 5th, 2012  |  syntax: C++  |  size: 2.10 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #ifndef asd
  2. #define asd
  3. #include <math.h>
  4. class BajVan {                  
  5. public:
  6.     /// @param - nem használjuk semmire
  7.     BajVan(const char &string) {}
  8. };
  9. template<class T>
  10. class Tabla
  11. {
  12.         int hash_key;
  13.         T* tarolo;
  14.         int db;
  15.         /// @param mit - Hashelendő elem
  16.         ///@return - Visszatér a hashelt adattal
  17.         int hash(T mit) ///Hash függvény
  18.         {
  19.                 return (hash_key & (int)mit)/2;
  20.         }
  21.  
  22. public:
  23.         Tabla() ///Default konstruktor
  24.         {
  25.                 tarolo=new T[1];
  26.                 tarolo[0]=0;
  27.                 db=0;
  28.         }
  29.         /// Kilistázza a tábla tartalmát.
  30.         void listaz()
  31.         {
  32.                 int i;
  33.                 for(i=0;i<=db;i++)
  34.                 {
  35.                         cout << i << ". " << tarolo[i] << endl;
  36.                 }
  37.         }
  38.         /// Ha úgy adja a sors, kitörölhetjük vele a tábla tartalmát.
  39.         void clear()
  40.         {
  41.                 for(int i=0;i<db;i++)
  42.                 {
  43.                         tarolo[i]=0;
  44.                 }
  45.         }
  46.         ///A hash key-t lehet vele beállítani, ami alapján a hash függvény működik
  47.         /// @param key - A kulcs.
  48.         void setKey(int key)
  49.         {
  50.                 this->hash_key=key;
  51.         }
  52.         ///Elemet lehet vele hozzáadni a táblához.
  53.         ///@param elem - Az elhelyezendő adat.
  54.         void add(T elem)//hozzáadás
  55.         {
  56.                 int hely = hash(elem);
  57.                 int i;
  58.                 if(db<hely)
  59.                 {cout <<"a";
  60.                         T* temp=new T[hely+1];
  61.                         for(i=0;i<=hely+1;i++) temp[i]=0;
  62.  
  63.                         for(i=0;i<=db;i++)
  64.                         {
  65.                                 temp[i]=tarolo[i];
  66.                         }
  67.                         delete[] tarolo;
  68.                         //T* tarolo=new T[hely];
  69.                         tarolo=temp;
  70.                         tarolo[hely]=elem;
  71.                         db=hely+1;
  72.                 }
  73.                 else
  74.                 {cout <<"b";
  75.                         int keres=0;
  76.                         while(tarolo[hely+keres]!=0)
  77.                         {
  78.                                 keres++;
  79.                                 //cout << "A hely foglalt, lejjebb kerult az elem!" << endl;
  80.                         }
  81.                         if(hely+keres>=db)
  82.                         {cout <<"c";
  83.                         T* temp=new T[hely+keres+1];
  84.                         for(i=0;i<=hely+keres;i++) temp[i]=0;
  85.                        
  86.                         for(i=0;i<=db;i++)
  87.                         {
  88.                                 temp[i]=tarolo[i];
  89.                         }
  90.                         tarolo=temp;
  91.                         db=hely+keres;
  92.                         }
  93.                         tarolo[hely+keres]=elem;
  94.                 }
  95.         }
  96.         ///Egy elemet lehet lekérdezni a tábla tetszőleges helyéről.
  97.         ///@param szam - A hely indexe
  98.         T get(int szam)
  99.         {
  100.                 return tarolo[szam];
  101.         }
  102.         /// Ugyanaz, mint a get függvény, csak operátorral megoldva.
  103.         ///@param szam - A lekérdezendő index
  104.         int& operator[] (const int szam)
  105.         {
  106.                 return tarolo[szam];
  107.         }
  108. };
  109.  
  110. #endif