-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path12_UniversityCatalog_v1.c
364 lines (308 loc) · 8.26 KB
/
12_UniversityCatalog_v1.c
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
Project #12 (CSeries)
11_UNIVERSITYCATALOG_V1.C
------------------
Description:
This is a HashTable which implements a hash function.
A hash function turns a key into a random-looking number,
and it must always return the same number given the same key.
Look at the output to know more ;)
Requerements:
Exercise 2:
Make an algorithm in C language that performs the search for a UNINTER student in AVA.
The search must be performed using a very efficient data structure
for this search task. Among the structures that can be used are: tree binary or hash.
1. You must store the student's name, e-mail address and RU.
To storage uses a heterogeneous data structure.
2. It is not necessary to read the data from the student data
manually. You can now pre-register the data in your code.
Register at least 10 student contacts in your data structure.
One of the contacts must be your own name and your UNINTER RU;
3. In an on-screen menu, ask the user to enter an RU. The program should
perform the search for this RU in the data structure and, if it finds the RU,
must show the corresponding name of the student and the email of this contact.
If not, an error message should be displayed.
4. To test the program, test the search with your RU and put in the capture of screen.
*************************************************
OUTPUT:
List of keys by position 0:
331066610 ->
_____________________________________
RU: 331066610
Student: Raquel Nogueira
Email: rnogueira@gmail
_____________________________________
3306660 ->
_____________________________________
RU: 3306660
Student: Lucas Nogueira Oliveira
Email: luke@gmail
_____________________________________
List of keys by position 1:
3316661 ->
_____________________________________
RU: 3316661
Student: Joao Pedro Nogueira Oliveira
Email: joaonogueira@gmail
_____________________________________
List of keys by position 2:
3326662 ->
_____________________________________
RU: 3326662
Student: Gilberto Oliveira Junior
Email: giljr@gmail
_____________________________________
List of keys by position 3:
3336663 ->
_____________________________________
RU: 3336663
Student: Anderson Silva
Email: asilva@gmail
_____________________________________
List of keys by position 4:
3346664 ->
_____________________________________
RU: 3346664
Student: Aparecido Lima
Email: alima@gmail
_____________________________________
List of keys by position 5:
3356665 ->
_____________________________________
RU: 3356665
Student: John Lennon
Email: jlennon@gmail
_____________________________________
List of keys by position 6:
3366666 ->
_____________________________________
RU: 3366666
Student: Jimmy Hendrix
Email: jhendrix@gmail
_____________________________________
List of keys by position 7:
3376667 ->
_____________________________________
RU: 3376667
Student: Marilia Mendonca
Email: mmendonca@gmail
_____________________________________
List of keys by position 8:
3376668 ->
_____________________________________
RU: 3376668
Student: Fred Mercury
Email: fmercury@gmail
_____________________________________
List of keys by position 9:
3396669 ->
_____________________________________
RU: 3396669
Student: Caetano Veloso
Email: cveloso@gmail
_____________________________________
Pressione qualquer tecla para continuar. . .
*************************************************
UNINTER - ESCOLA SUPERIOR POLITÉCNICA
GILBERTO OLIVEIRA JUNIOR
RU - 3326662
Graduation: Computer Engineering
Academic Work - Please, see requerements above o/
Edited By: J3
Date: Nov, 2021
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VECTORSIZE 10
//Typedef definition
typedef struct HashList
{ int id;
char student[50];
char email[50];
struct HashList *prox;
} HashList_t;
typedef struct Table
{
int Size;
HashList_t **ListKey;
} Table_t;
//Prototypes
int menu();
int HashingFunction(int);
void InsertHash(Table_t *, int, char[], char[], int);
void ShowHash(Table_t *);
void RemoveHash(Table_t *, int, int);
void SearchHash(Table_t *, int, int);
int main()
{
int op, c, id, pos;
Table_t *TableHash = (Table_t *)malloc(sizeof(Table_t));
TableHash->Size = VECTORSIZE;
TableHash->ListKey = (HashList_t **)malloc(VECTORSIZE * sizeof(HashList_t *));
char student[50], email[50];
for (int i = 0; i < VECTORSIZE; i++)
TableHash->ListKey[i] = NULL;
while (1)
{
op = menu();
switch (op)
{
case 1: //insert
printf("Type the id of the student: ");
scanf_s("%d", &id);
while ((c = getchar()) != '\n' && c != EOF)
{
}
printf("Type the name of the student: ");
scanf_s("%[a-zA-z ]]", &student);
while ((c = getchar()) != '\n' && c != EOF)
{
}
printf("Type the email of the student: ");
scanf_s("%[a-zA-z@ ]]", &email);
while ((c = getchar()) != '\n' && c != EOF)
{
}
pos = HashingFunction(id);
printf("Inserting in position %d of Table...\n", pos);
system("pause");
InsertHash(TableHash, id, student, email, pos);
break;
case 2: //remove
printf("Type the number to remove: ");
scanf_s("%d", &id);
while ((c = getchar()) != '\n' && c != EOF)
{
}
pos = HashingFunction(id);
printf("Removing from position %d of Table...\n", pos);
printf("WARNING: The System returns if nothing is found, ok? \n");
system("pause");
RemoveHash(TableHash, id, pos);
break;
case 3: //search
printf("Type the number to search: ");
scanf_s("%d", &id);
while ((c = getchar()) != '\n' && c != EOF)
{
}
pos = HashingFunction(id);
printf("Searching for id %i ...\n", id);
printf("WARNING: The System returns if nothing is found, ok? \n");
system("pause");
SearchHash(TableHash, id, pos);
system("pause");
break;
case 4: //show
ShowHash(TableHash);
system("pause");
break;
case 5:
return 0;
break;
default:
printf("Invalid\n");
break;
}
}
return 0;
}
int menu()
{
int op, c;
system("Cls");
printf("1. Insert in Hash\n");
printf("2. Remove from Hash\n");
printf("3. Search the Hash\n");
printf("4. Show the Hash\n");
printf("5. Exit\n");
printf("Choose the operation: ");
scanf_s("%d", &op);
while ((c = getchar()) != '\n' && c != EOF)
{
}
system("Cls");
return op;
}
int HashingFunction(int id)
{
return (id % VECTORSIZE);
}
void InsertHash(Table_t *TableHash, int id, char student[50], char email[50], int pos)
{
HashList_t *NewItem;
NewItem = (HashList_t *)malloc(sizeof(HashList_t));
NewItem->id = id;
strcpy(NewItem->student, student);
strcpy(NewItem->email, email);
NewItem->prox = NULL;
NewItem->prox = TableHash->ListKey[pos];
TableHash->ListKey[pos] = NewItem;
}
void SearchHash(Table_t *TableHash, int id, int pos)
{
HashList_t *ScanItem;
ScanItem = (HashList_t *)malloc(sizeof(TableHash));
ScanItem = TableHash->ListKey[pos];
HashList_t *Auxiliar;
Auxiliar = (HashList_t *)malloc(sizeof(HashList_t));
if (TableHash->ListKey[pos]->id == id)
{
TableHash->ListKey[pos] = TableHash->ListKey[pos]->prox;
printf("YES, The student RU-%d was found in table!\n", id);
}
else
{
while (ScanItem->id != id)
{
Auxiliar = ScanItem;
ScanItem = ScanItem->prox;
}
Auxiliar->prox = ScanItem->prox;
printf("YES, The The student RU-%d was found in table!\n", id);
}
}
void RemoveHash(Table_t *TableHash, int id, int pos)
{
HashList_t *ScanItem;
ScanItem = (HashList_t *)malloc(sizeof(TableHash));
ScanItem = TableHash->ListKey[pos];
HashList_t *Auxiliar;
Auxiliar = (HashList_t *)malloc(sizeof(HashList_t));
if (TableHash->ListKey[pos]->id == id)
{
TableHash->ListKey[pos] = TableHash->ListKey[pos]->prox;
free(ScanItem);
}
else
{
while (ScanItem->id != id)
{
Auxiliar = ScanItem;
ScanItem = ScanItem->prox;
}
Auxiliar->prox = ScanItem->prox;
free(ScanItem);
}
}
void ShowHash(Table_t *TableHash)
{
int v;
for (v = 0; v < TableHash->Size; v++)
{
HashList_t *ScanItem = TableHash->ListKey[v];
printf("\n List of keys by position %d:\n ", v);
while (ScanItem != NULL)
{
printf("%d -> ", ScanItem->id);
printf("\n_____________________________________\n");
printf("RU: %i\n", ScanItem->id);
printf("Student: %s\n", ScanItem->student);
printf("Email: %s\n", ScanItem->email);
printf("_____________________________________\n");
ScanItem = ScanItem->prox;
}
printf("\n");
}
}