-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagenda_pbufferf.c
More file actions
197 lines (156 loc) · 9.33 KB
/
Copy pathagenda_pbufferf.c
File metadata and controls
197 lines (156 loc) · 9.33 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
// Aloca buffer inicial: 3 ints para controle + 100 bytes para dados temporários
void* pBuffer = malloc(sizeof(int) * 3 + 50 + 50);
if (!pBuffer) {
printf("Error: Could not allocate memory for the contact list!\n");
return 1;
}
// Inicializa contador de pessoas
*((int*)pBuffer) = 0;
do {
printf("\n+==========================================+\n");
printf("| CONTACT LIST |\n");
printf("+==========================================+\n");
printf("| 1 - Add New Person |\n");
printf("| 2 - Remove Existing Person |\n");
printf("| 3 - Search for Specific Person |\n");
printf("| 4 - View All People |\n");
printf("| 5 - Exit Program |\n");
printf("+==========================================+\n");
printf("Your choice: ");
scanf("%d", ((int*)pBuffer + 1));
getchar();
switch(*((int*)pBuffer + 1)) {
case 1: // Adicionar nova pessoa
printf("\n=== ADDING NEW PERSON ===\n");
// Lê dados de entrada para áreas temporárias do buffer
printf("Enter full name: ");
fgets((char*)pBuffer + sizeof(int) * 3, 50, stdin);
((char*)pBuffer + sizeof(int) * 3)[strcspn((char*)pBuffer + sizeof(int) * 3, "\n")] = '\0';
printf("Enter age: ");
scanf("%d", ((int*)pBuffer + 2));
getchar();
printf("Enter email: ");
fgets((char*)pBuffer + sizeof(int) * 3 + 50, 50, stdin);
((char*)pBuffer + sizeof(int) * 3 + 50)[strcspn((char*)pBuffer + sizeof(int) * 3 + 50, "\n")] = '\0';
// Realoca buffer para acomodar nova pessoa (104 bytes por pessoa)
pBuffer = realloc(pBuffer, sizeof(int) * 3 + 50 + 50 + ((*((int*)pBuffer) + 1) * (50 + sizeof(int) + 50)));
if (!pBuffer) {
printf("Error: Insufficient memory to add person!\n");
return 1;
}
// Calcula localização na memória para nova pessoa
char* newPerson = (char*)pBuffer + sizeof(int) * 3 + 50 + 50 + (*((int*)pBuffer) * (50 + sizeof(int) + 50));
// Copia dados do buffer temporário para armazenamento permanente
strcpy(newPerson, (char*)pBuffer + sizeof(int) * 3);
*((int*)(newPerson + 50)) = *((int*)pBuffer + 2);
strcpy(newPerson + 50 + sizeof(int), (char*)pBuffer + sizeof(int) * 3 + 50);
(*((int*)pBuffer))++;
printf("Success! Person added. Total: %d person(s)\n", *((int*)pBuffer));
break;
case 2: // Remover pessoa
if (*((int*)pBuffer) == 0) {
printf("Your contact list is empty! Nothing to remove.\n");
break;
}
printf("\n=== REMOVING PERSON ===\n");
printf("Enter the name of the person to be removed: ");
fgets((char*)pBuffer + sizeof(int) * 3, 50, stdin);
((char*)pBuffer + sizeof(int) * 3)[strcspn((char*)pBuffer + sizeof(int) * 3, "\n")] = '\0';
// Procura pela pessoa para remover
*((int*)pBuffer + 2) = -1; // Flag de não encontrado
for (*((int*)pBuffer + 1) = 0; *((int*)pBuffer + 1) < *((int*)pBuffer); (*((int*)pBuffer + 1))++) {
char* currentPerson = (char*)pBuffer + sizeof(int) * 3 + 50 + 50 + (*((int*)pBuffer + 1) * (50 + sizeof(int) + 50));
if (strcmp(currentPerson, (char*)pBuffer + sizeof(int) * 3) == 0) {
*((int*)pBuffer + 2) = *((int*)pBuffer + 1); // Armazena índice encontrado
break;
}
}
if (*((int*)pBuffer + 2) == -1) {
printf("Person not found in contact list!\n");
break;
}
// Move pessoas restantes para preencher o espaço vazio
for (*((int*)pBuffer + 1) = *((int*)pBuffer + 2); *((int*)pBuffer + 1) < *((int*)pBuffer) - 1; (*((int*)pBuffer + 1))++) {
char* destination = (char*)pBuffer + sizeof(int) * 3 + 50 + 50 + (*((int*)pBuffer + 1) * (50 + sizeof(int) + 50));
char* source = (char*)pBuffer + sizeof(int) * 3 + 50 + 50 + ((*((int*)pBuffer + 1) + 1) * (50 + sizeof(int) + 50));
memcpy(destination, source, 50 + sizeof(int) + 50);
}
(*((int*)pBuffer))--;
// Reduz buffer se ainda restam pessoas
if (*((int*)pBuffer) > 0) {
pBuffer = realloc(pBuffer, sizeof(int) * 3 + 50 + 50 + (*((int*)pBuffer) * (50 + sizeof(int) + 50)));
}
printf("Success! Person removed. Current total: %d person(s)\n", *((int*)pBuffer));
break;
case 3: // Buscar pessoa
if (*((int*)pBuffer) == 0) {
printf("Contact list is empty! Add some people first.\n");
break;
}
printf("\n=== SEARCHING FOR PERSON ===\n");
printf("Enter the name of the person you're looking for: ");
fgets((char*)pBuffer + sizeof(int) * 3, 50, stdin);
((char*)pBuffer + sizeof(int) * 3)[strcspn((char*)pBuffer + sizeof(int) * 3, "\n")] = '\0';
*((int*)pBuffer + 2) = 0; // Flag de encontrado
// Busca linear através do array de pessoas
for (*((int*)pBuffer + 1) = 0; *((int*)pBuffer + 1) < *((int*)pBuffer); (*((int*)pBuffer + 1))++) {
char* currentPerson = (char*)pBuffer + sizeof(int) * 3 + 50 + 50 + (*((int*)pBuffer + 1) * (50 + sizeof(int) + 50));
if (strcmp(currentPerson, (char*)pBuffer + sizeof(int) * 3) == 0) {
printf("\n=== PERSON FOUND! ===\n");
printf("Name: %s\n", currentPerson);
printf("Age: %d years\n", *((int*)(currentPerson + 50)));
printf("Email: %s\n", currentPerson + 50 + sizeof(int));
*((int*)pBuffer + 2) = 1;
break;
}
}
if (!*((int*)pBuffer + 2)) {
printf("Person not found in contact list!\n");
}
break;
case 4: // Listar todas as pessoas
if (*((int*)pBuffer) == 0) {
printf("Your contact list is empty!\n");
printf("Tip: Use option 1 to add people.\n");
break;
}
printf("\n=== COMPLETE CONTACT LIST ===\n");
printf("Total registered people: %d\n\n", *((int*)pBuffer));
// Itera através de todas as pessoas armazenadas
for (*((int*)pBuffer + 2) = 0; *((int*)pBuffer + 2) < *((int*)pBuffer); (*((int*)pBuffer + 2))++) {
char* currentPerson = (char*)pBuffer + sizeof(int) * 3 + 50 + 50 + (*((int*)pBuffer + 2) * (50 + sizeof(int) + 50));
printf("+-------------------------------------------+\n");
printf("| Person #%d |\n", *((int*)pBuffer + 2) + 1);
printf("+-------------------------------------------+\n");
printf("| Name: %-30s |\n", currentPerson);
printf("| Age: %-30d |\n", *((int*)(currentPerson + 50)));
printf("| Email: %-30s |\n", currentPerson + 50 + sizeof(int));
printf("+-------------------------------------------+\n\n");
}
break;
case 5: // Sair do programa
printf("\n=== CLOSING PROGRAM ===\n");
printf("Thank you for using the Personal Contact List!\n");
printf("Registered people: %d\n", *((int*)pBuffer));
printf("See you next time!\n");
break;
default:
printf("Invalid option! Please choose between 1 and 5.\n");
printf("Tip: Type only the number of the desired option.\n");
}
// Espera entrada do usuário antes de continuar
if(*((int*)pBuffer + 1) != 5 && *((int*)pBuffer + 1) >= 1 && *((int*)pBuffer + 1) <= 4) {
printf("\nPress Enter to continue...");
getchar();
}
} while(*((int*)pBuffer + 1) != 5);
// Limpa a memória alocada
printf("\nFreeing memory... ");
free(pBuffer);
printf("Done!\n");
return 0;
}