Created
May 27, 2019 20:56
-
-
Save pxpc2/6cc98d3ba4fb81447be623b720a2c171 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 Dados | |
| { | |
| char nome[101], telefone[100], endereco[101], cep[100], nascimento[100]; | |
| }; | |
| struct No | |
| { | |
| struct Dados dados; | |
| struct No* prox; | |
| struct No* ant; | |
| }; | |
| struct No* inicio = NULL; | |
| struct No* fim = NULL; | |
| void adicionarNo(struct Dados); | |
| void carregarContatos(); | |
| int comparar(struct No*, struct No*); | |
| int exibirMenu(); | |
| struct Dados getDados(); | |
| void removerRegistros(char*); | |
| void exibirLista(); | |
| void exibirRegistros(char*); | |
| void printNo(struct Dados); | |
| void escreverArquivo(); | |
| int main() { | |
| carregarContatos(); | |
| int flag = 1; | |
| while (flag) | |
| { | |
| int opcao = exibirMenu(); | |
| switch (opcao) { | |
| case 1: { | |
| struct Dados d = getDados(); | |
| adicionarNo(d); | |
| } | |
| 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"); | |
| printf("Removendo registros\n"); | |
| removerRegistros(filtro); | |
| } | |
| break; | |
| case 3: { | |
| char filtro[101]; | |
| printf("Entre com a sequência de caracteres de filtro para buscar os registros\n"); | |
| getchar(); | |
| fgets(filtro, 101, stdin); | |
| strtok(filtro, "\n"); | |
| exibirRegistros(filtro); | |
| } | |
| break; | |
| case 4: | |
| exibirLista(); | |
| break; | |
| case 5: | |
| printf("Finalizando e salvando os novos dados da lista no arquivo txt...\n"); | |
| escreverArquivo(); | |
| flag = 0; | |
| break; | |
| } | |
| } | |
| return 0; | |
| } | |
| int exibirMenu() | |
| { | |
| int opcao = 5; | |
| 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 - Salvar e sair\n"); | |
| scanf("%d", &opcao); | |
| return opcao; | |
| } | |
| struct Dados getDados() | |
| { | |
| struct Dados d; | |
| printf("Entre com o nome\n"); | |
| getchar(); | |
| fgets(d.nome, 101, stdin); | |
| strtok(d.nome, "\n"); | |
| printf("Entre com o telefone no formato XXXXX-XXXX\n"); | |
| while (1) | |
| { | |
| scanf("%s", d.telefone); | |
| if (strlen(d.telefone) < 9 || d.telefone[5] != '-') { | |
| printf("Entre com um formato válido. (XXXXX-XXXX)\n"); | |
| continue; | |
| } | |
| break; | |
| } | |
| getchar(); | |
| printf("Entre com o endereço\n"); | |
| fgets(d.endereco, 101, stdin); | |
| strtok(d.endereco, "\n"); | |
| printf("Entre com o CEP\n"); | |
| scanf("%s", d.cep); | |
| printf("Entre com a data de nascimento, no formato DD/MM/AAAA\n"); | |
| while (1) | |
| { | |
| scanf("%s", d.nascimento); | |
| if (d.nascimento[2] != '/' && d.nascimento[5] != '/') { | |
| printf("Entre com um formato válido\n"); | |
| continue; | |
| } | |
| break; | |
| } | |
| return d; | |
| } | |
| void ordenarLista() | |
| { | |
| struct No* curr = NULL; | |
| struct No* prox = NULL; | |
| if (inicio == NULL) | |
| return; // lista vazia, nada para ordenar | |
| for (curr = inicio; curr->prox != NULL; curr = curr->prox) | |
| { | |
| for (prox = curr->prox; prox != NULL; prox = prox->prox) | |
| { | |
| if (comparar(curr, prox) >= 0) | |
| { | |
| struct Dados t = curr->dados; | |
| curr->dados = prox->dados; | |
| prox->dados = t; | |
| } | |
| } | |
| } | |
| } | |
| void adicionarNo(struct Dados dados) | |
| { | |
| struct No* n = (struct No*) malloc(sizeof(struct No)); | |
| n->dados = dados; | |
| if (inicio == NULL) | |
| { | |
| // lista vazia | |
| inicio = n; | |
| fim = n; | |
| inicio->ant = NULL; | |
| fim->prox = NULL; | |
| } | |
| else | |
| { | |
| fim->prox = n; | |
| n->ant = fim; | |
| fim = n; | |
| fim->prox = NULL; | |
| } | |
| ordenarLista(); | |
| } | |
| /* | |
| * < 0 se A vem antes de B | |
| * > 0 se B vem antes de A | |
| */ | |
| int comparar(struct No* a, struct No* b) | |
| { | |
| return strcmp(a->dados.nome, b->dados.nome); | |
| } | |
| void carregarContatos() | |
| { | |
| printf("Carregando registros já salvos em contatos.txt.\n"); | |
| int qt = 0; | |
| FILE *arq = fopen("contatos.txt", "r"); | |
| if (arq == NULL) | |
| { | |
| printf("Erro ao carregar/criar \"contatos.txt\""); | |
| return; | |
| } | |
| char texto[255]; | |
| int contador = 0; | |
| struct Dados d; | |
| while (fgets(texto, 255, arq) != NULL) | |
| { | |
| if (strcmp(texto, "\n") == 0) continue; | |
| if (contador == 5) | |
| { | |
| contador = 0; | |
| qt++; | |
| adicionarNo(d); | |
| continue; | |
| } | |
| switch (contador) | |
| { | |
| case 0: | |
| strtok(texto, "\n"); | |
| strcpy(d.nome, texto); | |
| break; | |
| case 1: | |
| strtok(texto, "\n"); | |
| strcpy(d.telefone, texto); | |
| break; | |
| case 2: | |
| strtok(texto, "\n"); | |
| strcpy(d.endereco, texto); | |
| break; | |
| case 3: | |
| strtok(texto, "\n"); | |
| strcpy(d.cep, texto); | |
| break; | |
| case 4: | |
| strtok(texto, "\n"); | |
| strcpy(d.nascimento, texto); | |
| break; | |
| default: | |
| break; | |
| } | |
| contador++; | |
| } | |
| printf("Total de %d registros carregados.\n\n", qt); | |
| printf("---------------\n"); | |
| fclose(arq); | |
| ordenarLista(); | |
| } | |
| void removerNo(struct No* n) | |
| { | |
| printf("Removendo registro de nome: %s\n", n->dados.nome); | |
| if (n == fim) | |
| { | |
| fim = fim->ant; | |
| if (fim != NULL) | |
| fim->prox = NULL; | |
| } | |
| else if (n == inicio) | |
| { | |
| inicio = inicio->prox; | |
| if (inicio != NULL) | |
| inicio->ant = NULL; | |
| } | |
| else | |
| { | |
| n->ant->prox = n->prox; | |
| n->prox->ant = n->ant; | |
| } | |
| free(n); | |
| } | |
| void removerRegistros(char* filtro) | |
| { | |
| if (inicio == NULL) | |
| return; | |
| //int flag = 1; | |
| struct No* curr = inicio; | |
| struct No* prox = NULL; | |
| while (curr != NULL) | |
| { | |
| if (strstr(curr->dados.nome, filtro) != NULL) | |
| { | |
| prox = curr->prox; | |
| removerNo(curr); | |
| curr = prox; | |
| } | |
| else | |
| curr = curr->prox; | |
| } | |
| free(curr); | |
| free(prox); | |
| ordenarLista(); | |
| } | |
| void exibirLista() | |
| { | |
| struct No* curr = inicio; | |
| printf("Exibindo lista\n"); | |
| while (curr != NULL) | |
| { | |
| printNo(curr->dados); | |
| curr = curr->prox; | |
| } | |
| free(curr); | |
| } | |
| void printNo(struct Dados d) | |
| { | |
| printf("Nome: %s\n", d.nome); | |
| printf("Telefone: %s\n", d.telefone); | |
| printf("Endereço: %s\n", d.endereco); | |
| printf("CEP: %s\n", d.cep); | |
| printf("Nascimento: %s\n---------------\n", d.nascimento); | |
| } | |
| void exibirRegistros(char* filtro) | |
| { | |
| struct No* curr = inicio; | |
| while (curr != NULL) | |
| { | |
| if (strstr(curr->dados.nome, filtro) != NULL) | |
| printNo(curr->dados); | |
| curr = curr->prox; | |
| } | |
| free(curr); | |
| } | |
| void escreverArquivo() | |
| { | |
| FILE *arq = fopen("contatos.txt", "w"); | |
| struct No* n = inicio; | |
| while (n != NULL) | |
| { | |
| struct Dados d = n->dados; | |
| fprintf(arq, "%s\n%s\n%s\n%s\n%s\n$\n", d.nome, d.telefone, | |
| d.endereco, d.cep, d.nascimento); | |
| n = n->prox; | |
| } | |
| fclose(arq); | |
| free(arq); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment