Advertisement
catalin_stefann11

HASHTABLE

Jul 1st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. using namespace std;
  7.  
  8. struct Adresa {
  9.     char* strada;
  10.     int numar;
  11. };
  12.  
  13.  
  14.  
  15. struct CotaIntretinere {
  16.     Adresa adresa;
  17.     int nrApartament;
  18.     int nrPersoane;
  19.     int anul;
  20.     int luna;
  21.     float valoareIntretinere;
  22. };
  23.  
  24.  
  25. struct Nod {
  26.     CotaIntretinere* info;
  27.     Nod* next;
  28. };
  29.  
  30. struct HashTable {
  31.  
  32.     int size;
  33.     Nod** vector;
  34. };
  35.  
  36. Adresa createAdresa(const char* str, int nr)
  37. {
  38.     Adresa a;
  39.     a.strada = (char*)malloc(strlen(str) + 1);
  40.     strcpy(a.strada, str);
  41.     a.numar = nr;
  42.     return a;
  43.    
  44. }
  45.  
  46. CotaIntretinere createCotaIntretinere(Adresa adresa, int nrApartament, int nrPersoane,
  47.     int anul, int luna, float valoareIntretinere) {
  48.  
  49.     CotaIntretinere c;
  50.     c.adresa = adresa;
  51.     c.nrApartament = nrApartament;
  52.     c.nrPersoane = nrPersoane;
  53.     c.anul = anul;
  54.     c.luna = luna;
  55.     c.valoareIntretinere = valoareIntretinere;
  56.     return c;
  57. }
  58.  
  59. Nod* createNod(CotaIntretinere* cota) {
  60.  
  61.     Nod* nou = (Nod*)malloc(sizeof(Nod));
  62.     nou->info = cota; // shallow copy
  63.     nou->next = NULL;
  64.     return nou;
  65. }
  66.  
  67.  
  68. HashTable initHashTable(int size) {
  69.  
  70.     HashTable ht;
  71.     ht.size = size;
  72.     ht.vector = (Nod**)malloc(sizeof(Nod*)*size);
  73.     for (int i = 0; i < size; i++) {
  74.         ht.vector[i] = NULL;
  75.     }
  76.     return ht;
  77.  
  78.     //memset(ht.vector, NULL, sizeof(Nod*)*size);
  79. }
  80.  
  81. int fhash(HashTable ht,  int nrApartament) {
  82.  
  83.     return nrApartament % ht.size;
  84. }
  85.  
  86.  
  87. Nod* listInserting(Nod* cap, CotaIntretinere* cota) {
  88.     Nod* nou = createNod(cota);
  89.     if (cap) {
  90.         Nod* temp = cap;
  91.         while (temp->next) {
  92.             temp = temp->next;
  93.         }
  94.         temp->next = nou;
  95.     }
  96.     else {
  97.         cap = nou;
  98.         nou->next = NULL;
  99.     }
  100.     return cap;
  101. }
  102.  
  103.  
  104. void insertHashTable(HashTable ht, CotaIntretinere* cota)
  105. {
  106.  
  107.  
  108.    
  109.  
  110.     int index = fhash(ht, cota->nrApartament);
  111.     if (ht.vector[index] != NULL)
  112.     {
  113.         ht.vector[index] = listInserting(ht.vector[index], cota);
  114.     }
  115.     else {
  116.         ht.vector[index] = createNod(cota);
  117.     }
  118. }
  119.  
  120. void afisareHashTable(HashTable ht){
  121.  
  122.     for (int i = 0; i < ht.size; i++)
  123.     {
  124.         if (ht.vector[i] == NULL) {
  125.             printf("%d. NULL \n", i);
  126.         }
  127.         else {
  128.             printf("%d. Apartamentul %d are intretinrea de platit: %5.2f ;", i, ht.vector[i]->info->nrApartament, ht.vector[i]->info->valoareIntretinere);
  129.             Nod* temp = ht.vector[i];
  130.             while (temp->next) {
  131.                 temp = temp->next;
  132.                 printf("%5.2f ;", temp->info->valoareIntretinere);
  133.             }
  134.             printf("\n");
  135.         }
  136.    
  137.        
  138.     }
  139.  
  140. }
  141.  
  142.  
  143.  
  144.  
  145.  
  146. float valoareaTotalAnuala(HashTable ht, int nrApartament, Adresa adresa) {
  147.  
  148.  
  149.     float suma = 0;
  150.     int index = fhash(ht, nrApartament);
  151.    
  152.     Nod* temp = ht.vector[index];
  153.     while( temp != NULL) {
  154.         if ((strcmp(temp->info->adresa.strada, adresa.strada) == 0) && temp->info->adresa.numar == adresa.numar)
  155.         {
  156.             suma += temp->info->valoareIntretinere;
  157.  
  158.         }
  159.         temp = temp->next;
  160.     }
  161.     return suma;
  162.  
  163. }
  164.  
  165.  
  166. //void stergeCoteAleUnuiApartament(HashTable ht, Adresa adresa, int nrApartament)
  167. //{
  168. //
  169. //  int index = fhash(ht, nrApartament);
  170. //  Nod* temp = ht.vector[index];
  171. //  while (temp != NULL) {
  172. //      if ((strcmp(temp->info->adresa.strada, adresa.strada) == 0) && temp->info->adresa.numar == adresa.numar)
  173. //      {
  174. //          ht.vector[index] = temp->next;
  175. //          free(temp->info->adresa.strada);
  176. //          free(temp);
  177. //      }
  178. //      temp = temp->next;
  179. //      if ((strcmp(temp->next->info->adresa.strada, adresa.strada) == 0) && temp->next->info->adresa.numar == adresa.numar)
  180. //      {
  181. //          Nod* nod = temp->next;
  182. //
  183. //      }
  184. //  }
  185. //}
  186.  
  187. //void stergereCoteAleUnuiApartament(HashTable* ht, Adresa adresa, int nrApartament) {
  188. //
  189. //  int index = fhash(*ht, nrApartament);
  190. //  Nod* nou = NULL;
  191. //  Nod* temp = ht->vector[index];
  192. //  while (temp != NULL)
  193. //  {
  194. //      if ((strcmp(temp->info->adresa.strada, adresa.strada) != 0) || (temp->info->adresa.numar != adresa.numar))
  195. //      {
  196. //          nou = listInserting(nou, &createCotaIntretinere(createAdresa(temp->info->adresa.strada, temp->info->adresa.numar), temp->info->nrApartament, temp->info->nrPersoane, temp->info->anul, temp->info->luna, temp->info->valoareIntretinere));
  197. //      }
  198. //      temp = temp->next;
  199. //  }
  200. //
  201. //  Nod* maxum = ht->vector[index];
  202. // 
  203. //  while (maxum->next) {
  204. //     
  205. //      //free(temp->info->adresa.strada);
  206. //      Nod* temp2 = maxum;
  207. //      free(temp2->info->adresa.strada);
  208. //      maxum = maxum->next;
  209. //      //free(temp->info);
  210. //      free(temp2);
  211. //     
  212. //  }
  213. //
  214. //  ht->vector[index] = nou;
  215. // 
  216. //}
  217.  
  218. void stergere(HashTable ht, Adresa adresa, int nrApartament, Nod** x) {
  219.  
  220.     if (*x != NULL) {
  221.         if ((*x)->next != NULL)
  222.         {
  223.             if ((strcmp((*x)->info->adresa.strada, adresa.strada) == 0) && (*x)->info->adresa.numar == adresa.numar)
  224.             {
  225.                 Nod* tmp = *x;
  226.  
  227.  
  228.                 (*x) = (*x)->next;
  229.                 free(tmp->info->adresa.strada);
  230.                 free(tmp);
  231.             }
  232.             stergere(ht, adresa, nrApartament, &(*x)->next);
  233.         }
  234.     }
  235. }
  236.  
  237.  
  238. //void stergere(HashTable ht, Adresa adresa, int nrApartament, Nod* x) {
  239. //  int index = fhash(ht, nrApartament);
  240. //  if (ht.vector[index] != NULL) {
  241. //
  242. //     
  243. //      if ((strcmp(ht.vector[index]->info->adresa.strada, adresa.strada) == 0) && ht.vector[index]->info->adresa.numar == adresa.numar)
  244. //      {
  245. //          Nod* tmp = ht.vector[index];
  246. //          free(tmp->info->adresa.strada);
  247. //         
  248. //          ht.vector[index] = ht.vector[index]->next;
  249. //          free(tmp);
  250. //      }
  251. //      stergere(ht, adresa, nrApartament, ht.vector[index]->next);
  252. //
  253. //  }
  254. //}
  255.  
  256.  
  257.  
  258. void main() {
  259.    
  260.    
  261.     HashTable ht = initHashTable(3);
  262.  
  263.     CotaIntretinere c1 = createCotaIntretinere(createAdresa("preciziei", 24), 1, 3, 2019, 1, 100.11);
  264.     CotaIntretinere c2 = createCotaIntretinere(createAdresa("preciziei", 24), 1, 3, 2019, 2, 350.84);
  265.     CotaIntretinere c3 = createCotaIntretinere(createAdresa("preciziei", 24), 1, 3, 2019, 3, 900.9);
  266.  
  267.     CotaIntretinere c5 = createCotaIntretinere(createAdresa("maxum", 24), 1, 3, 2019, 1, 100.11);
  268.     CotaIntretinere c6 = createCotaIntretinere(createAdresa("lkdald", 28), 1, 3, 2019, 2, 350.84);
  269.     CotaIntretinere c7 = createCotaIntretinere(createAdresa("preciziei", 28), 1, 3, 2019, 3, 900.9);
  270.    
  271.  
  272.     CotaIntretinere c21 = createCotaIntretinere(createAdresa("militari", 4), 2, 3, 2019, 1, 785.11);
  273.     CotaIntretinere c22 = createCotaIntretinere(createAdresa("militari", 4), 2, 3, 2019, 2, 987.11);
  274.     CotaIntretinere c23 = createCotaIntretinere(createAdresa("militari", 4), 2, 3, 2019, 3, 785.11);
  275.     CotaIntretinere c24 = createCotaIntretinere(createAdresa("militari", 4), 2, 3, 2019, 4, 17.11);
  276.  
  277.     CotaIntretinere c31 = createCotaIntretinere(createAdresa("piata romana", 24), 3, 3, 2019, 1, 165.11);
  278.     CotaIntretinere c32 = createCotaIntretinere(createAdresa("piata romana", 24), 3, 3, 2019, 2, 500.11);
  279.     CotaIntretinere c33 = createCotaIntretinere(createAdresa("piata romana", 24), 3, 3, 2019, 3, 1000.11);
  280.     CotaIntretinere c34 = createCotaIntretinere(createAdresa("piata romana", 24), 3, 3, 2019, 4, 50.11);
  281.    
  282.    
  283.    
  284.     insertHashTable(ht, &c1);
  285.     insertHashTable(ht, &c2);
  286.     insertHashTable(ht, &c3);
  287.     insertHashTable(ht, &c5);
  288.     insertHashTable(ht, &c6);
  289.     insertHashTable(ht, &c7);
  290.  
  291.     insertHashTable(ht, &c21);
  292.     insertHashTable(ht, &c22);
  293.     insertHashTable(ht, &c23);
  294.     insertHashTable(ht, &c24);
  295.  
  296.     insertHashTable(ht, &c31);
  297.     insertHashTable(ht, &c32);
  298.     insertHashTable(ht, &c33);
  299.     insertHashTable(ht, &c34);
  300.  
  301.  
  302.  
  303.  
  304.     afisareHashTable(ht);
  305.  
  306.     //float suma = valoareaTotalAnuala(ht, 1, createAdresa("maxum", 24));
  307.  
  308.     //printf("%5.2f", suma);
  309.  
  310.     //stergereCoteAleUnuiApartament(&ht, createAdresa("maxum", 24), 1);
  311.  
  312.     //afisareHashTable(ht);
  313.  
  314.    
  315.  
  316. //  stergere(ht, createAdresa("maxum", 24), 1, ht.vector[0]);
  317.  
  318.  
  319.  
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement