Created
May 27, 2019 17:23
-
-
Save pxpc2/ce4cba0e5f248b7122fcea8a105a35b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| struct registro | |
| { | |
| char nome[101], | |
| telefone[11], | |
| endereco[101], | |
| cep[9], | |
| nascimento[11]; | |
| }; | |
| struct no | |
| { | |
| struct no* ant; | |
| struct registro data; | |
| struct no* prox; | |
| }; | |
| typedef struct no* Lista; | |
| int carregarContatos(Lista* lista); | |
| Lista* criarLista(); | |
| int comparar(struct no* n1, struct no* n2); | |
| int ordenarLista(Lista* lista); | |
| int insere(Lista* lista, struct registro r); // TODO: ordenar após inserir | |
| int remover(Lista* lista, struct no* n); | |
| int removerRegistros(Lista* lista, char* filtro); // TODO: ordenar após remover | |
| int visualizarRegistrosFiltro(Lista* lista, char* filtro); | |
| /* | |
| * TODO: 4 do menu | |
| */ | |
| int main() { | |
| Lista* lista = criarLista(); | |
| printf("Carregando registros do arquivo contatos.txt\n"); | |
| int qt = carregarContatos(lista); | |
| printf("Total de %d registros carregados.\n", qt); | |
| int flag = 1; | |
| while (flag) | |
| { | |
| printf("Digite uma opção:\n" | |
| "1 - Inserir novo registro\n" | |
| "2 - Remover registros que contém certa string no nome\n" | |
| "3 - Visualizar registros que contém palavra específica no nome\n" | |
| "4 - Visualizar todos registros\n" | |
| "5 - Sair\n"); | |
| int opcao; | |
| scanf("%d", &opcao); | |
| switch (opcao) | |
| { | |
| case 1: | |
| { | |
| struct registro r; | |
| printf("Entre com o nome\n"); | |
| getchar(); | |
| fgets(r.nome, 101, stdin); | |
| strtok(r.nome, "\n"); | |
| printf("Entre com o telefone no formato XXXXX-XXXX\n"); | |
| while (1) | |
| { | |
| scanf("%s", r.telefone); | |
| if (strlen(r.telefone) < 9 || r.telefone[5] != '-') { | |
| printf("Entre com um formato válido. (XXXXX-XXXX)\n"); | |
| continue; | |
| } | |
| break; | |
| } | |
| getchar(); | |
| printf("Entre com o endereço\n"); | |
| fgets(r.endereco, 101, stdin); | |
| strtok(r.endereco, "\n"); | |
| printf("Entre com o CEP\n"); | |
| scanf("%s", r.cep); | |
| printf("Entre com a data de nascimento, no formato DD/MM/AAAA\n"); | |
| while (1) | |
| { | |
| scanf("%s", r.nascimento); | |
| if (r.nascimento[2] != '/' && r.nascimento[5] != '/') { | |
| printf("Entre com um formato válido\n"); | |
| continue; | |
| } | |
| break; | |
| } | |
| int f = insere(lista, r); | |
| ordenarLista(lista); | |
| if (f) printf("Registro inserido com sucesso.\n"); | |
| else printf("Falha ao inserir registro.\n"); | |
| } break; | |
| case 2: | |
| { | |
| char filtro[101]; | |
| printf("Entre com a sequência de caracteres de filtro para remover os registros\n"); | |
| getchar(); | |
| fgets(filtro, 101, stdin); | |
| strtok(filtro, "\n"); | |
| int f = removerRegistros(lista, filtro); | |
| if (f) printf("Registros removidos com sucesso.\n"); | |
| else printf("Falha ao remover registros.\n"); | |
| } break; | |
| case 3: | |
| { | |
| char filtro[101]; | |
| printf("Entre com a sequência de caracteres de filtro para consultar os registros\n"); | |
| getchar(); | |
| fgets(filtro, 101, stdin); | |
| strtok(filtro, "\n"); | |
| int f = visualizarRegistrosFiltro(lista, filtro); | |
| if (f) printf("Registros consultados com sucesso.\n"); | |
| else printf("Falha ao consultar registros.\n"); | |
| } break; | |
| case 4: | |
| { | |
| char filtro[2] = {'-'}; | |
| int f = visualizarRegistrosFiltro(lista, filtro); | |
| if (!f) printf("Falha ao visualizar registros.\n"); | |
| } break; | |
| case 5: | |
| flag = 0; | |
| break; | |
| default: | |
| printf("Entre com uma opção válida.\n"); | |
| break; | |
| } | |
| } | |
| return 0; | |
| } | |
| int carregarContatos(Lista* lista) | |
| { | |
| int qt = 0; | |
| FILE *arq = fopen("contatos.txt", "r"); | |
| if (arq == NULL) | |
| { | |
| printf("Erro ao carregar/criar \"contatos.txt\""); | |
| return -1; | |
| } | |
| char texto[255]; | |
| int contador = 0; | |
| struct registro r; | |
| while (fgets(texto, 255, arq) != NULL) | |
| { | |
| if (contador == 5) | |
| { | |
| contador = 0; | |
| qt++; | |
| //printf("Telefone_teste1: %s\n", r.telefone); | |
| insere(lista, r); | |
| // printf("telefone_teste2: %s\n", r.telefone); | |
| continue; | |
| } | |
| switch (contador) | |
| { | |
| case 0: | |
| strtok(texto, "\n"); | |
| strcpy(r.nome, texto); | |
| printf("Nome: %s\n", r.nome); | |
| break; | |
| case 1: | |
| strtok(texto, "\n"); | |
| strcpy(r.telefone, texto); | |
| printf("Telefone: %s\n", r.telefone); | |
| break; | |
| case 2: | |
| strtok(texto, "\n"); | |
| strcpy(r.endereco, texto); | |
| printf("Endereço: %s\n", r.endereco); | |
| break; | |
| case 3: | |
| strtok(texto, "\n"); | |
| strcpy(r.cep, texto); | |
| printf("CEP: %s\n", r.cep); | |
| break; | |
| case 4: | |
| strtok(texto, "\n"); | |
| strcpy(r.nascimento, texto); | |
| printf("Nascimento: %s\n", r.nascimento); | |
| break; | |
| } | |
| contador++; | |
| } | |
| fclose(arq); | |
| //ordenarLista(lista); | |
| return qt; | |
| } | |
| Lista* criarLista() | |
| { | |
| Lista* lista = (Lista*) malloc(sizeof(Lista)); | |
| if (lista != NULL) // se conseguiu alocar | |
| *lista = NULL; // Lista é do tipo elemento*, entao *lista aponta pra um elemento, inicializando como nulo | |
| return lista; | |
| } | |
| int comparar(struct no* n1, struct no* n2) | |
| { | |
| return strcmp(n1->data.nome, n2->data.nome); // < 0 vem antes, > 0 depois, == 0 strings iguais | |
| } | |
| int ordenarLista(Lista* lista) | |
| { | |
| struct no* curr = (*lista); | |
| struct no* prox = NULL; | |
| if (curr == NULL) | |
| { | |
| printf("head == NULL\n"); | |
| return 0; | |
| } | |
| for (curr = (*lista); curr->prox != NULL; curr = curr->prox) | |
| { | |
| for (prox = curr->prox; prox != NULL; prox = prox->prox) | |
| { | |
| if (comparar(curr, prox) >= 0) // trocar a ordem dos dois | |
| { | |
| struct registro temp = curr->data; | |
| curr->data = prox->data; | |
| prox->data = temp; | |
| } | |
| } | |
| } | |
| } | |
| int insere(Lista* lista, struct registro r) | |
| { | |
| struct no* n = (struct no*) malloc(sizeof(struct no)); | |
| n->data = r; | |
| n->prox = (*lista); | |
| n->ant = NULL; | |
| if ((*lista) != NULL) | |
| (*lista)->ant = n; | |
| (*lista) = n; | |
| } | |
| int remover(Lista *lista, struct no* n) | |
| { | |
| printf("Removendo registro de nome: %s", n->data.nome); | |
| if ((*lista) == n) | |
| (*lista) = n->prox; | |
| if (n->prox != NULL) // não é o último | |
| n->prox->ant = n->prox; | |
| if (n->ant != NULL) | |
| n->ant->prox = n->prox; | |
| free(n); | |
| return 1; | |
| } | |
| int removerRegistros(Lista* lista, char* filtro) | |
| { | |
| if (lista == NULL) | |
| return 0; | |
| //int flag = 1; | |
| struct no* n = (*lista); | |
| struct no* prox; | |
| while (n != NULL) | |
| { | |
| if (strstr(n->data.nome, filtro) != NULL) | |
| { | |
| prox = n->prox; | |
| remover(lista, n); | |
| n = prox; | |
| } | |
| else | |
| n = n->prox; | |
| } | |
| free(n); | |
| ordenarLista(lista); | |
| return 1; | |
| } | |
| int visualizarRegistrosFiltro(Lista* lista, char* filtro) | |
| { | |
| if (lista == NULL) | |
| return 0; | |
| if ((*lista) == NULL) | |
| { | |
| while ((*lista) == NULL) | |
| (*lista) = (*lista)->prox; | |
| } | |
| int qt = 0; | |
| struct no* n = (*lista); | |
| while (1) | |
| { | |
| if (n == NULL) break; | |
| if (strstr(n->data.nome, filtro) != NULL || filtro[0] == '-') | |
| { | |
| printf("--- Registro encontrado #%d ---\n", qt+1); | |
| struct registro r = n->data; | |
| printf("Nome: %s\n", r.nome); | |
| printf("Telefone: %s\n", r.telefone); | |
| printf("Endereço: %s\n", r.endereco); | |
| printf("Cep: %s\n", r.cep); | |
| printf("Data de nascimento: %s\n", r.nascimento); | |
| n = n->prox; | |
| qt++; | |
| } | |
| else | |
| n = n->prox; | |
| } | |
| free(n); | |
| if (qt == 0) printf("Nenhum registro com este filtro encontrado.\n"); | |
| return 1; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment