Agenda feita em C usando árvore binária
Publicado por Marcos Augusto (última atualização em 05/12/2017)
[ Hits: 16.677 ]
Homepage: ...
Download 1511239697.Agenda.rar (versão 2)
Download 1511240106.Agenda.rar (versão 3)
Este é um algoritmo que simula uma agenda onde é possível guardar o nome e o telefone dos contados. A estrutura de dados usada para implementar a agenda foi uma árvore binária feita como TAD.
Este algoritmo foi implementado no Dev-C++:
http://sourceforge.net/projects/dev-cpp/
Versão 2 - Enviado por Marcos Augusto em 21/11/2017
Changelog: Mudanças foram realizadas para tornar o código mais legível e organizado.
Download 1511239697.Agenda.rar
Versão 3 - Enviado por Marcos Augusto em 21/11/2017
Changelog: Mudanças foram realizadas para tonar o código mais organizado.
Download 1511240106.Agenda.rar
struct arvore
{
char Nome[100];
int telefone;
struct arvore *esq;
struct arvore *dir;
};
typedef struct arvore Arvore;
void inserir(Arvore **raiz,char string[]);
void ordem(Arvore *raiz);
void busca(Arvore *raiz,char string[]);
void Altera( Arvore **raiz, char *string);
void excluir(Arvore **raiz,char string[]);
Arvore** menor_dir(Arvore *raiz);
Arvore** maior_esq (Arvore *raiz);
void ler_string(char string[]);
void ler_telefone(int *telefone);
void maiuscula(char string[]);
void Menu(int *num);
void opcao(char op[]);
void Menu_1();
void Menu_2();
void Menu_3();
void Menu_4();
void Menu_5();
void posicao(int x, int y);
void cor(WORD cor);
# include <windows.h>
# include <string.h>
# include <stdlib.h>
# include <stdio.h>
# include <conio.h>
# include "Arv.h"
void inserir(Arvore **raiz ,char string[])
{
int telefone;
if(*raiz == NULL)
{
*raiz = (Arvore*) malloc (sizeof(Arvore));
strcpy((*raiz)->Nome,string);
ler_telefone(&telefone);
(*raiz)->telefone = telefone;
(*raiz)->esq = NULL;
(*raiz)->dir = NULL;
}else{
if(strcasecmp((*raiz)->Nome,string)>0)
{
inserir(&(*raiz)->esq,string);
}else{
if(strcasecmp((*raiz)->Nome,string)<0)
{
inserir(&(*raiz)->dir,string);
}else{
if(strcmp((*raiz)->Nome,string)==0)
{
cor(15); printf("Nome j\xa0 foi registrado\n");
getch();
}}}}}
void ordem(Arvore *raiz)
{
if(raiz!=NULL)
{
ordem((raiz)->esq);
cor(10);printf("\n\nNome = ");
cor(11);printf("%s",(raiz)->Nome);
cor(10);printf("\nTelefone = ");
cor(11);printf("%d",(raiz)->telefone);
ordem((raiz)->dir);
}}
void busca(Arvore *raiz,char string[])
{
if(raiz!=NULL)
{
if(strcasecmp((raiz)->Nome,string)>0)
{
busca((raiz)->esq,string);
}else{
if(strcasecmp((raiz)->Nome,string)<0)
{
busca((raiz)->dir,string);
}else{
if(strcmp((raiz)->Nome,string)==0)
{
system("cls");
cor(10);posicao(25,1);printf("REGISTRO ENCONTRADO!!\n");
cor(10);printf("\n\nNome = ");
cor(11);printf("%s",(raiz)->Nome);
cor(10);printf("\nTelefone = ");
cor(11);printf("%d",(raiz)->telefone);
getch();
}}}}else{
system("cls");
cor(10);
posicao(25,1);printf("*-----------------------*\n");
posicao(25,2);printf("| Nome n\xc6o encontrado!! |\n");
posicao(25,3);printf("*-----------------------*\n");
getch();
}}
void Altera( Arvore **raiz, char *string)
{
if((*raiz)!=NULL)
{
if(strcasecmp((*raiz)->Nome,string)>0)
{
Altera(&(*raiz)->esq,string);
}else{
if(strcasecmp((*raiz)->Nome,string)<0)
{
Altera(&(*raiz)->dir,string);
}else{
if(strcmp((*raiz)->Nome,string)==0)
{
system("cls");
int telefone;
cor(10);posicao(25,1);printf("REGISTRO ENCONTRADO!!\n");
cor(10);printf("\n\nNome = ");
cor(11);printf("%s",(*raiz)->Nome);
cor(10);printf("\nNovo Telefone = ");
cor(11);scanf("%d",&telefone);
(*raiz)->telefone= telefone;
system("cls");
cor(10);
posicao(25,1); printf("*-----------------*\n");
posicao(25,2); printf("| Dado Alterado!! |\n");
posicao(25,3); printf("*-----------------*\n");
getch();
}}}}else{
system("cls");
cor(10);
posicao(25,1);printf("*-----------------------*\n");
posicao(25,2);printf("| Nome n\xc6o encontrado!! |\n");
posicao(25,3);printf("*-----------------------*\n");
getch();
}}
void excluir(Arvore **raiz,char string[])
{
Arvore **aux2, *aux3;
if(*raiz!=NULL)
{
if( strcasecmp((*raiz)->Nome , string)==0 )
{
if((*raiz)->esq == (*raiz)->dir)
{
free(*raiz);
*raiz = NULL;
}else{
if((*raiz)->esq != NULL)
{
aux2 = maior_esq(*raiz);
aux3 = *aux2;
(*aux2) = (*aux2)->esq;
}else{
aux2 = menor_dir(*raiz);
aux3 = *aux2;
(*aux2) = (*aux2)->dir;
}
strcpy((*raiz)->Nome, aux3->Nome);
free(aux3); aux3 = NULL;
}
}else{
if(strcasecmp(string,(*raiz)->Nome)<0)
{
excluir(&(*raiz)->esq,string);
}else{
excluir(&(*raiz)->dir,string);
}
}
}
else
{
system("cls");
cor(10);
posicao(25,1); printf("*-----------------------*\n");
posicao(25,2); printf("| Nome n\xc6o encontrado!! |\n");
posicao(25,3); printf("*-----------------------*\n");
getch();
}
}
Arvore** maior_esq (Arvore *raiz)
{
Arvore **aux = &(raiz);
if((*aux)->esq != NULL)
{
aux = &(*aux)->esq;
while( (*aux)->dir != NULL )
{
aux = &(*aux)->dir;
}}
return aux;
}
Arvore** menor_dir(Arvore *raiz)
{
Arvore **aux = &(raiz);
if((*aux)->dir != NULL)
{
aux = &(*aux)->dir;
while((*aux)->esq != NULL)
{
aux=&(*aux)->esq;
}}
return aux;
}
void Menu(int *num)
{
cor(11);posicao(35,2);printf("AGENDA");
cor(10);posicao(25,2);printf("|");posicao(53,2);printf("|");
posicao(25,1);printf("*---------------------------*\n");
posicao(25,3);printf("|---------------------------|\n");
posicao(25,4);printf("| 1 - Insere Contatos |\n");
posicao(25,5);printf("|---------------------------|\n");
posicao(25,6);printf("| 2 - Imprime Contatos |\n");
posicao(25,7);printf("|---------------------------|\n");
posicao(25,8);printf("| 3 - Busca de Contatos |\n");
posicao(25,9);printf("|---------------------------|\n");
posicao(25,10);printf("| 4 - Retira Contato |\n");
posicao(25,11);printf("|---------------------------|\n");
posicao(25,12);printf("| 5 - Altera Contato |\n");
posicao(25,13);printf("|---------------------------|\n");
posicao(25,14);printf("| 7 - Sair |\n");
posicao(25,15);printf("*---------------------------*\n");
posicao(1,1);cor(15);printf("\nDigite uma op\x87\xc6o = ");
scanf("%d",num);
getchar();
}
void Menu_1()
{
system("cls");
cor(10);
posicao(28,1);printf("*------------------*\n");
posicao(28,2);printf("| INSERIR CONTATOS |\n");
posicao(28,3);printf("*------------------* \n");
}
void Menu_2()
{
system("cls");
cor(10);
posicao(28,1);printf("*---------------------*\n");
posicao(28,2);printf("| VIZUALIZAR CONTATOS |\n");
posicao(28,3);printf("*---------------------* \n");
}
void Menu_3()
{
system("cls");
cor(10);
posicao(28,1);printf("*-------------------*\n");
posicao(28,2);printf("| PESQUIZAR CONTATO |\n");
posicao(28,3);printf("*-------------------* \n");
}
void Menu_4()
{
system("cls");
cor(10);
posicao(28,1);printf("*-------------------*\n");
posicao(28,2);printf("| EXCLUIR CONTATO |\n");
posicao(28,3);printf("*-------------------* \n");
}
void Menu_5()
{
system("cls");
cor(10);
posicao(28,1);printf("*-------------------*\n");
posicao(28,2);printf("| ALTERAR CONTATO |\n");
posicao(28,3);printf("*-------------------* \n");
}
void posicao(int x, int y)
{
HANDLE SaidaSTD = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(SaidaSTD, coord);
}
void cor(WORD cor)
{
HANDLE SaidaSTD = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(SaidaSTD, cor);
}
void ler_telefone(int *telefone)
{
cor(10);printf("\nDigite o Telefone = ");
cor(11);scanf("%d",telefone);
}
void ler_string(char string[])
{
cor(10);printf("\nDigite o Nome = ");
fflush(stdin);
cor(11);gets(string);
maiuscula(string);
}
void maiuscula(char string[])
{
int i;
for(i=0;i<strlen(string);i++)
{
if((string[i]>='a') && (string[i]<='z'))
{
string[i]-=32;
}}}
void opcao(char op[])
{
cor(11);printf("\n\nDeseja continuar sim = s ou n\xc6o = n\n");
fflush(stdin);
gets(op);
}
/*
Nomes: Marcos Augusto e Giovanni Bosco;
Curso: Ciência da Computacao; 3º Periodo;
Data: 18/11/11; 17:26;
Descricao: Trabalho Inicial de Estrutura de Dados II;
*/
# include "Arv.c"
#include <conio.h>
#include <stdlib.h>
main()
{
int num;
char Nome[100],op[2];
Arvore *raiz;
raiz = NULL;
while(num!=7)
{
Menu(&num);
switch(num)
{
case 1:
do{
Menu_1();
ler_string(Nome);
inserir(&raiz,Nome);
opcao(op);
}while(*op == 's' || *op =='S');
break;
case 2:
Menu_2();
ordem(raiz);
getch();
break;
case 3:
Menu_3();
ler_string(Nome);
busca(raiz,Nome);
break;
case 4:
do{
Menu_4();
ler_string(Nome);
opcao(op);
if(*op == 'n' || *op =='N')break;
excluir(&raiz,Nome);break;
}while(*op =='s' || *op =='S');
break;
case 5:
Menu_5();
ler_string(Nome);
Altera(&raiz,Nome);
break;
}system("cls");
}
}
Busca em texto - Lista encadeada
Jogo Super Mario Bros 3 (com gráficos)
Arquivos utilizados no artigo: "Desenvolvendo um plugin para o XMMS"
IA Turbina o Desktop Linux enquanto distros renovam forças
Como extrair chaves TOTP 2FA a partir de QRCODE (Google Authenticator)
Linux em 2025: Segurança prática para o usuário
Desktop Linux em alta: novos apps, distros e privacidade marcam o sábado
Atualizando o Fedora 42 para 43
Como saber se o seu e-mail já teve a senha vazada?
Como descobrir se a sua senha já foi vazada na internet?
E aí? O Warsaw já está funcionando no Debian 13? [RESOLVIDO] (15)
Secure boot, artigo interessante, nada técnico. (4)
copiar library para diretorio /usr/share/..... su com Falha na a... (1)









