Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //EXERCÍCIO 01
- #include <stdio.h>
- #include <string.h>
- #include <conio.h>
- typedef struct agenda {
- struct agenda *ant;
- char nome[51];
- int telefone[9];
- char email[51];
- struct agenda *prox;
- }Tagenda;
- Tagenda * procurar(Tagenda *L, char nome[]);
- void adicionar(Tagenda **L, char nome[]) {
- Tagenda *novo, *busca;
- busca = procurar(*L, nome);
- if (busca != NULL){
- printf("Impossivel adicionar, contato ja inserido!\n");
- return;
- }
- else{
- novo = (Tagenda *)malloc(sizeof(Tagenda));
- strcpy(novo->nome, nome);
- printf("Informe seu email: ");
- gets(novo->email);
- fflush(stdin);
- printf("Informe seu telefone: ");
- gets(novo->telefone);
- fflush(stdin);
- if (*L == NULL){
- *L = novo;
- novo->prox = NULL;
- novo->ant = NULL;
- }
- else{
- novo->prox = *L;
- (*L)->ant = novo;
- *L = novo;
- novo->ant = NULL;
- }
- }
- }
- Tagenda * procurar(Tagenda *L, char nome[]) {
- Tagenda *aux;
- if (L == NULL) {
- return NULL;
- }
- else{
- aux = L;
- while (aux != NULL) {
- if (strcmp(aux->nome, nome) == 0) {
- return aux;
- }
- else if (strcmp(aux->nome, nome) > 0){
- return NULL;
- }
- else{
- aux = aux->prox;
- }
- }
- }
- return NULL;
- }
- void remover(Tagenda **L, char nome[]) {
- Tagenda *remover, *auxAnt, *auxPost, *busca, *auxAAnt;
- busca = procurar(*L, nome);
- if (busca == NULL) {
- printf("Esse contato nao consta na agenda!\n");
- return;
- }
- else {
- remover = *L;
- if (remover->prox == NULL && remover == busca) {
- free(remover);
- *L = NULL;
- printf("Remocao realizada com sucesso!\n");
- }
- else if (remover->prox != NULL && remover == busca) {
- auxAnt = *L;
- auxPost = remover->prox;
- free(remover);
- auxAnt->prox = auxPost;
- auxPost->prox = NULL;
- auxPost->ant = auxAnt;
- printf("Remocao realizada com sucesso!\n");
- }
- else {
- while (remover != busca) {
- auxAAnt = auxAnt;
- auxAnt = remover;
- remover = remover->prox;
- auxPost = remover->prox;
- }
- if (remover->prox == NULL) {
- auxAnt->prox = NULL;
- auxAnt->ant = auxAAnt;
- free(remover);
- printf("Remocao realizada com sucesso!\n");
- }
- else {
- auxAnt->prox = auxPost;
- auxPost->ant = auxAnt;
- free(remover);
- printf("Remocao realizada com sucesso!\n");
- }
- }
- }
- }
- void listar(Tagenda **L) {
- Tagenda *aux;
- int contato = 0;
- aux = *L;
- while (aux != NULL) {
- printf("contato %d\n", contato + 1);
- puts(aux->nome);
- puts(aux->email);
- printf("telefone: %s\n", aux->telefone);
- aux = aux->prox;
- contato++;
- }
- }
- void exibir(Tagenda **L, char nome[]) {
- Tagenda *busca;
- busca = procurar(*L, nome);
- if (busca == NULL) {
- printf("Contato nao cadastrado!\n");
- }
- else {
- printf("Dados do contato\n");
- puts(busca->nome);
- puts(busca->email);
- printf("Telefone: %s\n", busca->telefone);
- }
- }
- void alterar(Tagenda **L, char nome[]) {
- Tagenda *busca;
- int op, novoTelefone;
- char novoEmail[51];
- busca = procurar(*L, nome);
- if (busca == NULL) {
- printf("Esse contato nao esta contido na agenda!\n");
- return;
- }
- else {
- do {
- printf("1 - alterar email\n2 - alterar telefone\n3 - alterar ambos\nDigite a opcao: ");
- scanf("%d", &op);
- fflush(stdin);
- switch (op){
- case 1:
- printf("Informe o email: ");
- gets(novoEmail);
- fflush(stdin);
- strcpy(busca->email, novoEmail);
- break;
- case 2:
- printf("Informe o telefone: ");
- scanf("%d", &novoTelefone);
- fflush(stdin);
- strcpy(busca->telefone, novoTelefone);
- break;
- case 3:
- printf("Informe o email: ");
- gets(novoEmail);
- fflush(stdin);
- strcpy(busca->email, novoEmail);
- printf("Informe o telefone: ");
- scanf("%d", &novoTelefone);
- fflush(stdin);
- strcpy(busca->telefone, novoTelefone);
- break;
- default:
- break;
- }
- } while (op < 1 || op > 3);
- }
- }
- void removeALL(Tagenda **L) {
- Tagenda *remover, *aux;
- aux = *L;
- if (*L == NULL) {
- return;
- }
- else {
- while (aux != NULL) {
- remover = aux;
- aux = aux->prox;
- free(remover);
- }
- }
- }
- int main() {
- Tagenda *agenda = NULL;
- int op;
- char nome[50];
- do {
- printf("1 - Adicionar contato\n2 - Remover contato\n3 - Listar agenda\n4 - Exibir um contato\n5 - Alterar contato\n6 - Fechar Programa\n Digite a opcao: ");
- scanf("%d", &op);
- fflush(stdin);
- switch (op) {
- case 1:
- printf("Informe o nome:");
- gets(nome);
- fflush(stdin);
- adicionar(&agenda, nome);
- break;
- case 2:
- printf("Informe o nome:");
- gets(nome);
- fflush(stdin);
- remover(&agenda, nome);
- break;
- case 3:
- listar(&agenda);
- break;
- case 4:
- printf("Informe o nome do contato que deseja exibir: ");
- gets(nome);
- fflush(stdin);
- exibir(&agenda, nome);
- break;
- case 5:
- printf("Informe o nome do contato que deseja alterar: ");
- gets(nome);
- fflush(stdin);
- alterar(&agenda, nome);
- break;
- case 6:
- removeALL(&agenda);
- op = 6;
- printf("Obrigado por usar o programa!\n");
- break;
- default:
- printf("Opcao invalida!\n");
- break;
- }
- } while (op != 6);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement