-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.c
220 lines (175 loc) · 6.25 KB
/
app.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_MAP_SIZE 100
// Structure to represent an individual
struct Person {
char *id;
char *age;
char *name;
char *surname;
struct Person *next;
};
// Structure to represent a node in the hash table
struct HashNode {
char *id;
struct Person *person;
struct HashNode *next;
};
// Function to dynamically allocate memory for a string input
char *getUserInput(const char *prompt);
// Function to save individual details to a text file
void saveToFile(struct Person *person, FILE *file) {
fprintf(file, "%s$", person->name);
fprintf(file, "%s$", person->surname);
fprintf(file, "%s$", person->age);
fprintf(file, "%s$", person->id);
}
// Function to save hash map details to a text file
void saveMapToFile(struct HashNode *hashTable[], FILE *file) {
for (int i = 0; i < HASH_MAP_SIZE; ++i) {
struct HashNode *current = hashTable[i];
while (current != NULL) {
fprintf(file, "%s,%p\n", current->id, (void *)current->person);
current = current->next;
}
}
}
// Function to add a new person to the linked list and hash table
void addPerson(struct Person **head, struct HashNode *hashTable[], FILE *usersFile) {
// Buffer to store user input
char inputBuffer[2048];
size_t bufferIndex = 0;
// Dynamically allocate memory for the new person
struct Person *newPerson = (struct Person *)malloc(sizeof(struct Person));
if (newPerson == NULL) {
printf("Error: Memory allocation failed.\n");
exit(1);
}
// Input custom ID, age, name, and surname into the buffer
strcpy(inputBuffer, getUserInput("Enter custom ID (a number):"));
strcat(inputBuffer, "$");
strcpy(inputBuffer + strlen(inputBuffer), getUserInput("Enter age:"));
strcat(inputBuffer, "$");
strcpy(inputBuffer + strlen(inputBuffer), getUserInput("Enter name:"));
strcat(inputBuffer, "$");
strcpy(inputBuffer + strlen(inputBuffer), getUserInput("Enter surname:"));
strcat(inputBuffer, "$");
// Allocate blocks for the user input
size_t blockSize = 64;
size_t inputLength = strlen(inputBuffer);
while (bufferIndex < inputLength) {
// Dynamically allocate memory for the block
char *block = (char *)malloc(blockSize);
if (block == NULL) {
printf("Error: Memory allocation failed.\n");
exit(1);
}
// Copy data from the buffer to the block
strncpy(block, inputBuffer + bufferIndex, blockSize - 1);
block[blockSize - 1] = '\0'; // Null-terminate the block
// Update the buffer index
bufferIndex += blockSize - 1;
// Save details to the user file
fprintf(usersFile, "%s", block);
// Free allocated memory for the block
free(block);
// If there's more data, allocate a new block
if (bufferIndex < inputLength) {
fprintf(usersFile, "$"); // Add the special character between blocks
}
}
// Assign the next pointer
newPerson->next = *head;
// Update the head to point to the new person
*head = newPerson;
// Map custom ID to the pointer of the current person (hash table)
int hashIndex = atoi(newPerson->id) % HASH_MAP_SIZE;
// Create a new hash node
struct HashNode *newNode = (struct HashNode *)malloc(sizeof(struct HashNode));
if (newNode == NULL) {
printf("Error: Memory allocation failed.\n");
exit(1);
}
newNode->id = strdup(newPerson->id);
newNode->person = newPerson;
// Insert the hash node into the hash table
newNode->next = hashTable[hashIndex];
hashTable[hashIndex] = newNode;
}
int main() {
// Declare a pointer to the head of the linked list
struct Person *head = NULL;
// Array to represent the hash table
struct HashNode *hashTable[HASH_MAP_SIZE] = {NULL};
// Open the users file for writing (this will clear the file)
FILE *usersFile = fopen("users.txt", "w");
if (usersFile == NULL) {
printf("Error opening users file.\n");
exit(1);
}
// Check if the map file is not empty and clear it if needed
FILE *mapFile = fopen("map.txt", "r");
if (mapFile != NULL) {
fseek(mapFile, 0, SEEK_END);
long size = ftell(mapFile);
// Add a newline if the file is not empty
if (size > 0) {
fprintf(mapFile, "\n");
}
}
int numClasses;
// Prompt the user for the number of classes
printf("Enter the number of classes: ");
scanf("%d", &numClasses);
// Clear the newline character from the input buffer
while (getchar() != '\n');
// Loop to add individuals to the linked list and hash table
for (int i = 0; i < numClasses; ++i) {
addPerson(&head, hashTable, usersFile);
}
fclose(usersFile);
// Save hash map details to the map file
FILE *mapFile2 = fopen("map.txt", "a");
if (mapFile2 == NULL) {
printf("Error opening map file.\n");
exit(1);
}
saveMapToFile(hashTable, mapFile2);
fclose(mapFile2);
// Display the entered information
// Free allocated memory for the hash table
for (int i = 0; i < HASH_MAP_SIZE; ++i) {
struct HashNode *current = hashTable[i];
while (current != NULL) {
struct HashNode *temp = current;
current = current->next;
// Free allocated memory for this hash node
free(temp->id);
free(temp);
}
}
return 0;
}
//---------------------------------------------------------------------------------------------------------------/
// Function to dynamically allocate memory for a string input
char *getUserInput(const char *prompt) {
printf("%s", prompt);
char *buffer = NULL;
size_t bufferSize = 0;
// Read the input using getline
if (getline(&buffer, &bufferSize, stdin) == -1) {
printf("Error reading input.\n");
exit(1);
}
// Remove newline character if present
size_t length = strlen(buffer);
if (length > 0 && buffer[length - 1] == '\n') {
buffer[length - 1] = '\0';
} else {
// Clear the input buffer if necessary
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
return buffer;
}