-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.cpp
166 lines (149 loc) · 3.12 KB
/
HashTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct Moneda
{
char codMoneda[3];
char* emitent;
char* denumire;
int sold;
};
struct HashTable
{
Moneda* *vector;
int dim;
};
Moneda* adaugareMoneda(const char cod[3], const char* emitent, const char* denumire, int sold)
{
Moneda* m = (Moneda*)malloc(sizeof(Moneda));
strcpy(m->codMoneda, cod);
m->emitent = (char*)malloc(sizeof(char)*(strlen(emitent) + 1));
strcpy(m->emitent, emitent);
m->denumire = (char*)malloc(sizeof(char)*(strlen(denumire) + 1));
strcpy(m->denumire, denumire);
m->sold = sold;
return m;
}
int hashFunction(HashTable h, Moneda* m)
{
int s = 0;
for (int i = 0; i < strlen(m->codMoneda); i++)
{
s += m->codMoneda[i];
}
return s % h.dim;
}
HashTable initHashtable(int dim)
{
HashTable h;
h.dim = dim;
h.vector = (Moneda**)malloc(sizeof(Moneda*)*h.dim);
for (int i = 0; i < h.dim; i++)
{
h.vector[i] = NULL;
}
return h;
}
void afisareMoneda(Moneda* m)
{
printf("%s --- %s --- %s --- %d\n", m->codMoneda, m->emitent, m->denumire, m->sold);
}
void afisareHashTable(HashTable h)
{
if (h.dim > 0)
{
for (int i = 0; i < h.dim; i++)
if (h.vector[i])
afisareMoneda(h.vector[i]);
}
}
int inserareHashTable(Moneda* m, HashTable h)
{
if (h.dim > 0)
{
int hashCode = hashFunction(h, m);
if (h.vector[hashCode])
{
int index = (hashCode + 1) % h.dim;
while (hashCode != index && h.vector[index] != NULL)
{
index = (index + 1) % h.dim;
}
if (index == hashCode)
{
return -1;//tabela este plina
}
else
{
h.vector[index] = m;
return hashCode;
}
}
else
{
h.vector[hashCode] = m;
return hashCode;
}
}
else
return -2; //tabela este goala
}
void comparare(HashTable h, int valoare)
{
int nr = 0;
for (int i = 0; i < h.dim; i++)
{
if (h.vector[i]->sold > valoare)
nr++;
}
Moneda** nou = (Moneda**)malloc(sizeof(Moneda*)*nr);
for (int i = 0; i < h.dim; i++)
{
int j = 0;
if (h.vector[i]->sold > valoare)
{
nou[j] = h.vector[i];
j++;
afisareMoneda(h.vector[i]);
}
}
}
void inlocuireValoare(HashTable h, const char* denumire, int valoare)
{
if (h.dim > 0)
{
int poz;
int hashCode = hashFunction(h, h.vector[0]);
for (int i = 0; i < h.dim; i++)
{
if (h.vector[hashCode] && strcmp(h.vector[hashCode]->denumire, denumire) == 0)
{
h.vector[hashCode]->sold = valoare;
}
else
{
hashCode = (hashCode + 1) % h.dim;
}
}
}
}
void main()
{
HashTable h = initHashtable(5);
Moneda* m1 = adaugareMoneda("RON", "Banca Nationala Roamana", "LEU", 10000);
inserareHashTable(m1, h);
Moneda* m2 = adaugareMoneda("EUR", "Banca Europeana Centrala", "EURO", 4000);
inserareHashTable(m2, h);
Moneda* m3 = adaugareMoneda("GBP", "Banca Reginei Angliei", "LIRA STERLINA", 5000);
inserareHashTable(m3, h);
Moneda* m4 = adaugareMoneda("CHF", "La Casa de Papel", "FRANC ELVETIAN", 2000);
inserareHashTable(m4, h);
Moneda* m5 = adaugareMoneda("USD", "United States of America", "DOLAR", 8000);
inserareHashTable(m5, h);
afisareHashTable(h);
printf("\n\n");
comparare(h, 6000);
printf("\n\n");
inlocuireValoare(h,"DOLAR" , 1111);
afisareHashTable(h);
}