//Código Base para o Mini-Projeto Agenda de Amigos
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <locale.h>
#include <stdlib.h>
#include <ctype.h>
struct ficha{
char nome[40];
char telefone[20];
char email[30];
bool valido;
};
typedef struct ficha FICHA;
int procurarRec(char procurado[], FICHA vetor[], int inicio, int num_fichas)
{ if(inicio >= num_fichas) return -1; //Não achei -> Base
if(strcasecmp(procurado, vetor[inicio].nome)==0) return inicio;
return procurarRec(procurado, vetor, inicio+1, num_fichas);
}
int procurar(char procurado[], FICHA vetor[], int num_fichas)
{ int i;
char resposta[10];
for(i=0; i<num_fichas; i++)
{ if(vetor[i].valido && strstr(vetor[i].nome, procurado) != NULL)
{ printf("%s -> é este? (S/N) ", vetor[i].nome);
gets(resposta);
if(toupper(resposta[0]) == 'S')
return i;
}
}
return -1;
}
int main()
{
FICHA agenda[100];
int fichas_existentes = 0, proxima = 0;
char opcao[10]="0";
setlocale(LC_ALL, "Portuguese");
while(opcao[0] != '7')
{
printf("\nEntre com a opção desejada:\n");
printf("\n1) Inserir ficha");
printf("\n2) Procurar por nome");
printf("\n3) Listar toda a base");
printf("\n4) Excluir por nome");
printf("\n5) Alterar");
printf("\n6) Restaurar dados apagados");
printf("\n7) Sair\n\n");
gets(opcao);
if(opcao[0] == '1') //inserir
{
printf("\nEntre com um nome:");
gets(agenda[proxima].nome);
printf("\nEntre com um telefone:");
gets(agenda[proxima].telefone);
printf("\nEntre com um e-mail:");
gets(agenda[proxima].email);
agenda[proxima].valido = true;
fichas_existentes++;
proxima++;
}
if(opcao[0] == '2') //procurar
{
char procurado[40];
printf("\nEntre com o nome procurado:");
gets(procurado);
int i = procurar(procurado, agenda, fichas_existentes);
if(i != -1) printf("\n\nO telefone de %s é %s e o e-mail é %s\n",
agenda[i].nome, agenda[i].telefone, agenda[i].email);
else printf("\n\nNome não encontrado\n");
}
if(opcao[0] == '3') //listar tudo
{
printf("\n\n");
int i;
bool vazia = true;
for(i=0; i<fichas_existentes; i++)
{ if(agenda[i].valido)
{ printf("O telefone de %s é %s e o e-mail é %s\n",
agenda[i].nome, agenda[i].telefone, agenda[i].email);
vazia=false;
}
}
if(vazia)
printf("A base de dados está vazia!\n");
}
if(opcao[0] == '4') //excluir
{
char procurado[40];
printf("\nEntre com o nome que será excluÃdo: ");
gets(procurado);
int i = procurar(procurado, agenda, fichas_existentes);
if(i != -1)
{ printf("\n\nO telefone de %s, que é %s, e-mail %s, será removido\n",
agenda[i].nome, agenda[i].telefone, agenda[i].email);
agenda[i].valido = false;
}
else printf("\n\nNome não encontrado\n");
}
if(opcao[0] == '5') //alterar
{
char procurado[40];
printf("\nEntre com o nome que será alterado: ");
gets(procurado);
int i = procurar(procurado, agenda, fichas_existentes);
if(i != -1)
{ char aux[50];
printf("\n\nNome: %s, telefone: %s, e-mail: %s\n",
agenda[i].nome, agenda[i].telefone, agenda[i].email);
printf("\nTecle somente ENTER para manter o valor atual:");
printf("\nNome: ");
gets(aux);
if(aux[0] != '\0') strcpy(agenda[i].nome, aux);
printf("\nTelefone: ");
gets(aux);
if(aux[0] != '\0') strcpy(agenda[i].telefone, aux);
printf("\nE-mail: ");
gets(aux);
if(aux[0] != '\0') strcpy(agenda[i].email, aux);
}
else printf("\n\nNome não encontrado\n");
}
if(opcao[0] == '6') //restaurar dados apagados
{
printf("\n\n");
int i;
char resposta[10];
for(i=0; i<fichas_existentes; i++)
{ if(agenda[i].valido == false) //apagado
{ printf("APAGADO: O telefone de %s é %s e o e-mail é %s\n",
agenda[i].nome, agenda[i].telefone, agenda[i].email);
printf("Deseja recuperar esta ficha (S/N)? ");
gets(resposta);
if(toupper(resposta[0]) == 'S')
agenda[i].valido = true;
}
}
}
}
return 0;
}