|
1 | | -#include <stdio.h> |
2 | 1 | #include "../include/contact.h" |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
3 | 5 |
|
4 | | -// Função para adicionar um novo contato à lista. |
5 | | -void addContact(Contact contacts[], int *count) { |
6 | | - printf("Inserir nome do contato: \n"); |
7 | | - scanf("%99s", contacts[*count].name); |
| 6 | +void addContact(Contact contacts[], int *count) |
| 7 | +{ |
| 8 | + printf("\n=== NOVO CONTATO ===\n"); |
| 9 | + printf("Nome: "); |
| 10 | + fgets(contacts[*count].name, MAX_NAME, stdin); |
| 11 | + contacts[*count].name[strcspn(contacts[*count].name, "\n")] = '\0'; |
| 12 | + printf("Telefone: "); |
| 13 | + fgets(contacts[*count].phone, MAX_PHONE, stdin); |
| 14 | + contacts[*count].phone[strcspn(contacts[*count].phone, "\n")] = '\0'; |
| 15 | + contacts[*count].id = *count + 1; |
| 16 | + (*count)++; |
| 17 | + printf("\nContato adicionado.\n"); |
| 18 | +} |
8 | 19 |
|
9 | | - printf("Inserir telefone do contato: \n"); |
10 | | - scanf("%19s", contacts[*count].phone); |
| 20 | +void listContacts(Contact contacts[], int count) |
| 21 | +{ |
| 22 | + int i; |
11 | 23 |
|
12 | | - (*count)++; |
| 24 | + if (count == 0) { |
| 25 | + printf("\nNenhum contato.\n"); |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + printf("\n=== CONTATOS ===\n"); |
| 30 | + |
| 31 | + for (i = 0; i < count; i++) { |
| 32 | + printf("\n[%d]\nNome: %s\nTelefone: %s\n", |
| 33 | + contacts[i].id, |
| 34 | + contacts[i].name, |
| 35 | + contacts[i].phone); |
| 36 | + } |
13 | 37 | } |
14 | 38 |
|
15 | | -// Funçao para listar os contatos armazenados. |
16 | | -void listContacts(Contact contacts[], int count) { |
| 39 | +void searchContacts(Contact contacts[], int count) |
| 40 | +{ |
| 41 | + int i; |
| 42 | + int found = 0; |
| 43 | + char term[MAX_NAME]; |
| 44 | + |
17 | 45 | if (count == 0) { |
18 | | - printf("Nenhum contato encontrado!\n"); |
| 46 | + printf("\nNenhum contato.\n"); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + printf("\nTermo de busca: "); |
| 51 | + fgets(term, MAX_NAME, stdin); |
| 52 | + term[strcspn(term, "\n")] = '\0'; |
| 53 | + |
| 54 | + if (term[0] == '\0') { |
| 55 | + printf("\nTermo vazio. Digite um nome ou telefone.\n"); |
19 | 56 | return; |
20 | 57 | } |
21 | 58 |
|
22 | | - for (int i = 0; i < count; i++) { |
23 | | - printf("Contato %d:\n", i + 1); |
24 | | - printf("Nome: %s | Telefone: %s\n", contacts[i].name, contacts[i].phone); |
| 59 | + printf("\n=== RESULTADO DA BUSCA ===\n"); |
| 60 | + for (i = 0; i < count; i++) { |
| 61 | + if (strstr(contacts[i].name, term) != NULL || |
| 62 | + strstr(contacts[i].phone, term) != NULL) { |
| 63 | + printf("\n[%d]\nNome: %s\nTelefone: %s\n", |
| 64 | + contacts[i].id, |
| 65 | + contacts[i].name, |
| 66 | + contacts[i].phone); |
| 67 | + found = 1; |
| 68 | + } |
25 | 69 | } |
| 70 | + |
| 71 | + if (!found) |
| 72 | + printf("\nNenhum contato encontrado para '%s'.\n", term); |
26 | 73 | } |
27 | | -// As funções removeContact e findContact ainda precisam ser implementadas. |
28 | 74 |
|
| 75 | +int findContact(Contact contacts[], int count, const char *query) |
| 76 | +{ |
| 77 | + int i; |
| 78 | + char *endptr; |
| 79 | + long id; |
29 | 80 |
|
| 81 | + for (i = 0; i < count; i++) { |
| 82 | + if (strcmp(contacts[i].name, query) == 0 || |
| 83 | + strcmp(contacts[i].phone, query) == 0) |
| 84 | + return i; |
| 85 | + } |
30 | 86 |
|
| 87 | + id = strtol(query, &endptr, 10); |
| 88 | + if (*query != '\0' && *endptr == '\0') { |
| 89 | + for (i = 0; i < count; i++) { |
| 90 | + if (contacts[i].id == id) |
| 91 | + return i; |
| 92 | + } |
| 93 | + } |
31 | 94 |
|
| 95 | + return -1; |
| 96 | +} |
| 97 | + |
| 98 | +void removeContact(Contact contacts[], int *count) |
| 99 | +{ |
| 100 | + int i; |
| 101 | + int index; |
| 102 | + char term[MAX_NAME]; |
| 103 | + int confirm; |
| 104 | + int c; |
| 105 | + |
| 106 | + if (*count == 0) { |
| 107 | + printf("\nLista vazia.\n"); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + printf("\nNome, telefone ou ID do contato: "); |
| 112 | + fgets(term, MAX_NAME, stdin); |
| 113 | + term[strcspn(term, "\n")] = '\0'; |
| 114 | + |
| 115 | + if (term[0] == '\0') { |
| 116 | + printf("\nTermo vazio. Nenhum contato removido.\n"); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + index = findContact(contacts, *count, term); |
| 121 | + if (index == -1) { |
| 122 | + printf("\nContato nao encontrado.\n"); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + printf("\nContato encontrado:\n[%d] %s | %s\n", |
| 127 | + contacts[index].id, |
| 128 | + contacts[index].name, |
| 129 | + contacts[index].phone); |
| 130 | + printf("Confirmar remocao? (s/N): "); |
| 131 | + confirm = getchar(); |
| 132 | + while ((c = getchar()) != '\n' && c != EOF) |
| 133 | + ; |
| 134 | + if (confirm != 's' && confirm != 'S') { |
| 135 | + printf("\nRemocao cancelada.\n"); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + for (i = index; i < *count - 1; i++) |
| 140 | + contacts[i] = contacts[i + 1]; |
| 141 | + |
| 142 | + (*count)--; |
| 143 | + |
| 144 | + for (i = 0; i < *count; i++) |
| 145 | + contacts[i].id = i + 1; |
| 146 | + |
| 147 | + printf("\nContato removido.\n"); |
| 148 | +} |
0 commit comments