Created
May 27, 2019 01:53
-
-
Save pxpc2/5d7fbbf5c9621bf68376db5a15318272 to your computer and use it in GitHub Desktop.
zzz
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 insere_inicio(Lista* lista, struct registro r); | |
/* | |
* TODO: opções 2, 3 e 4 do menu, e criar função de ordenação | |
*/ | |
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 palavra específica\n" | |
"3 - Visualizar registros que contém palavra específica\n" | |
"4 - Visualizar todos registros\n" | |
"5 - Sair\n", qt); | |
int opcao; | |
scanf("%d", &opcao); | |
switch (opcao) | |
{ | |
case 1: | |
{ | |
struct registro r; | |
printf("Entre com o nome\n"); | |
scanf("%s", r.nome); | |
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; | |
} | |
insere_inicio(lista, r); | |
// TODO: ordenar lista após inserir novo elemento. | |
} break; | |
case 2: | |
break; | |
case 3: | |
break; | |
case 4: | |
break; | |
case 5: | |
flag = 0; | |
break; | |
default: | |
printf("Entre com uma opção válida.\n"); | |
break; | |
} | |
} | |
return 0; | |
} | |
/* | |
* Carrega os contatos do arquivo contatos.txt e os insere em Lista* lista, | |
* TODO: ordenar depois de inserir | |
*/ | |
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++; | |
int flag = insere_inicio(lista, r); | |
if (flag == 0) printf("Erro ao inserir elemento\n"); | |
continue; | |
} | |
switch (contador) | |
{ | |
case 0: | |
strcpy(r.nome, texto); | |
break; | |
case 1: | |
strcpy(r.telefone, texto); | |
break; | |
case 2: | |
strcpy(r.endereco, texto); | |
break; | |
case 3: | |
strcpy(r.cep, texto); | |
break; | |
case 4: | |
strcpy(r.nascimento, texto); | |
break; | |
default: | |
break; | |
} | |
contador++; | |
} | |
fclose(arq); | |
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 insere_inicio(Lista* lista, struct registro r) | |
{ | |
if (lista == NULL) | |
return 0; | |
struct no* n = (struct no*) malloc(sizeof(struct no)); | |
if (n == NULL) | |
return 0; | |
n->data = r; | |
n->prox = (*lista); // próximo elemento vira o elemento atual que a lista aponta | |
n->ant = NULL; // nó vai ser o primeiro elemento | |
if ((*lista) != NULL) | |
(*lista)->ant = n; | |
(*lista) = n; | |
return 1; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment