Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <malloc.h>
- #include <stdlib.h>
- #include <string.h>
- using namespace std;
- struct Adresa {
- char* strada;
- int numar;
- };
- struct CotaIntretinere {
- Adresa adresa;
- int nrApartament;
- int nrPersoane;
- int anul;
- int luna;
- float valoareIntretinere;
- };
- struct Nod {
- CotaIntretinere* info;
- Nod* next;
- };
- struct HashTable {
- int size;
- Nod** vector;
- };
- Adresa createAdresa(const char* str, int nr)
- {
- Adresa a;
- a.strada = (char*)malloc(strlen(str) + 1);
- strcpy(a.strada, str);
- a.numar = nr;
- return a;
- }
- CotaIntretinere createCotaIntretinere(Adresa adresa, int nrApartament, int nrPersoane,
- int anul, int luna, float valoareIntretinere) {
- CotaIntretinere c;
- c.adresa = adresa;
- c.nrApartament = nrApartament;
- c.nrPersoane = nrPersoane;
- c.anul = anul;
- c.luna = luna;
- c.valoareIntretinere = valoareIntretinere;
- return c;
- }
- Nod* createNod(CotaIntretinere* cota) {
- Nod* nou = (Nod*)malloc(sizeof(Nod));
- nou->info = cota; // shallow copy
- nou->next = NULL;
- return nou;
- }
- HashTable initHashTable(int size) {
- HashTable ht;
- ht.size = size;
- ht.vector = (Nod**)malloc(sizeof(Nod*)*size);
- for (int i = 0; i < size; i++) {
- ht.vector[i] = NULL;
- }
- return ht;
- //memset(ht.vector, NULL, sizeof(Nod*)*size);
- }
- int fhash(HashTable ht, int nrApartament) {
- return nrApartament % ht.size;
- }
- Nod* listInserting(Nod* cap, CotaIntretinere* cota) {
- Nod* nou = createNod(cota);
- if (cap) {
- Nod* temp = cap;
- while (temp->next) {
- temp = temp->next;
- }
- temp->next = nou;
- }
- else {
- cap = nou;
- nou->next = NULL;
- }
- return cap;
- }
- void insertHashTable(HashTable ht, CotaIntretinere* cota)
- {
- int index = fhash(ht, cota->nrApartament);
- if (ht.vector[index] != NULL)
- {
- ht.vector[index] = listInserting(ht.vector[index], cota);
- }
- else {
- ht.vector[index] = createNod(cota);
- }
- }
- void afisareHashTable(HashTable ht){
- for (int i = 0; i < ht.size; i++)
- {
- if (ht.vector[i] == NULL) {
- printf("%d. NULL \n", i);
- }
- else {
- printf("%d. Apartamentul %d are intretinrea de platit: %5.2f ;", i, ht.vector[i]->info->nrApartament, ht.vector[i]->info->valoareIntretinere);
- Nod* temp = ht.vector[i];
- while (temp->next) {
- temp = temp->next;
- printf("%5.2f ;", temp->info->valoareIntretinere);
- }
- printf("\n");
- }
- }
- }
- float valoareaTotalAnuala(HashTable ht, int nrApartament, Adresa adresa) {
- float suma = 0;
- int index = fhash(ht, nrApartament);
- Nod* temp = ht.vector[index];
- while( temp != NULL) {
- if ((strcmp(temp->info->adresa.strada, adresa.strada) == 0) && temp->info->adresa.numar == adresa.numar)
- {
- suma += temp->info->valoareIntretinere;
- }
- temp = temp->next;
- }
- return suma;
- }
- //void stergeCoteAleUnuiApartament(HashTable ht, Adresa adresa, int nrApartament)
- //{
- //
- // int index = fhash(ht, nrApartament);
- // Nod* temp = ht.vector[index];
- // while (temp != NULL) {
- // if ((strcmp(temp->info->adresa.strada, adresa.strada) == 0) && temp->info->adresa.numar == adresa.numar)
- // {
- // ht.vector[index] = temp->next;
- // free(temp->info->adresa.strada);
- // free(temp);
- // }
- // temp = temp->next;
- // if ((strcmp(temp->next->info->adresa.strada, adresa.strada) == 0) && temp->next->info->adresa.numar == adresa.numar)
- // {
- // Nod* nod = temp->next;
- //
- // }
- // }
- //}
- void stergereCoteAleUnuiApartament(HashTable* ht, Adresa adresa, int nrApartament) {
- int index = fhash(*ht, nrApartament);
- Nod* nou = NULL;
- Nod* temp = ht->vector[index];
- while (temp != NULL)
- {
- if ((strcmp(temp->info->adresa.strada, adresa.strada) != 0) || (temp->info->adresa.numar != adresa.numar))
- {
- 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));
- }
- temp = temp->next;
- }
- Nod* maxum = ht->vector[index];
- while (maxum->next) {
- //free(temp->info->adresa.strada);
- Nod* temp2 = maxum;
- free(temp2->info->adresa.strada);
- maxum = maxum->next;
- //free(temp->info);
- free(temp2);
- }
- ht->vector[index] = nou;
- }
- void stergere(HashTable ht, Adresa adresa, int nrApartament, Nod* x) {
- int index = fhash(ht, nrApartament);
- if (ht.vector[index] != NULL) {
- if ((strcmp(ht.vector[index]->info->adresa.strada, adresa.strada) == 0) && ht.vector[index]->info->adresa.numar == adresa.numar)
- {
- Nod* tmp = ht.vector[index];
- free(tmp->info->adresa.strada);
- ht.vector[index] = ht.vector[index]->next;
- free(tmp);
- }
- stergere(ht, adresa, nrApartament, ht.vector[index]->next);
- }
- }
- void main() {
- HashTable ht = initHashTable(3);
- CotaIntretinere c1 = createCotaIntretinere(createAdresa("preciziei", 24), 1, 3, 2019, 1, 100.11);
- CotaIntretinere c2 = createCotaIntretinere(createAdresa("preciziei", 24), 1, 3, 2019, 2, 350.84);
- CotaIntretinere c3 = createCotaIntretinere(createAdresa("preciziei", 24), 1, 3, 2019, 3, 900.9);
- CotaIntretinere c5 = createCotaIntretinere(createAdresa("maxum", 24), 1, 3, 2019, 1, 100.11);
- CotaIntretinere c6 = createCotaIntretinere(createAdresa("lkdald", 28), 1, 3, 2019, 2, 350.84);
- CotaIntretinere c7 = createCotaIntretinere(createAdresa("preciziei", 28), 1, 3, 2019, 3, 900.9);
- CotaIntretinere c21 = createCotaIntretinere(createAdresa("militari", 4), 2, 3, 2019, 1, 785.11);
- CotaIntretinere c22 = createCotaIntretinere(createAdresa("militari", 4), 2, 3, 2019, 2, 987.11);
- CotaIntretinere c23 = createCotaIntretinere(createAdresa("militari", 4), 2, 3, 2019, 3, 785.11);
- CotaIntretinere c24 = createCotaIntretinere(createAdresa("militari", 4), 2, 3, 2019, 4, 17.11);
- CotaIntretinere c31 = createCotaIntretinere(createAdresa("piata romana", 24), 3, 3, 2019, 1, 165.11);
- CotaIntretinere c32 = createCotaIntretinere(createAdresa("piata romana", 24), 3, 3, 2019, 2, 500.11);
- CotaIntretinere c33 = createCotaIntretinere(createAdresa("piata romana", 24), 3, 3, 2019, 3, 1000.11);
- CotaIntretinere c34 = createCotaIntretinere(createAdresa("piata romana", 24), 3, 3, 2019, 4, 50.11);
- insertHashTable(ht, &c1);
- insertHashTable(ht, &c2);
- insertHashTable(ht, &c3);
- insertHashTable(ht, &c5);
- insertHashTable(ht, &c6);
- insertHashTable(ht, &c7);
- insertHashTable(ht, &c21);
- insertHashTable(ht, &c22);
- insertHashTable(ht, &c23);
- insertHashTable(ht, &c24);
- insertHashTable(ht, &c31);
- insertHashTable(ht, &c32);
- insertHashTable(ht, &c33);
- insertHashTable(ht, &c34);
- afisareHashTable(ht);
- //float suma = valoareaTotalAnuala(ht, 1, createAdresa("maxum", 24));
- //printf("%5.2f", suma);
- //stergereCoteAleUnuiApartament(&ht, createAdresa("maxum", 24), 1);
- //afisareHashTable(ht);
- stergere(ht, createAdresa("maxum", 24), 1, ht.vector[0]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement