I'm trying to organize the code according to the run menu of a schedule using the binary tree data structure because if I search for a user, the menu always appears first, and the listed user is left behind. How do I organize the source code correctly of this schedule in C?
The C code of the agenda tree:
file: phonebook.h
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Arvore
{
char name[100];
int phone;
struct Arvore *esq;
struct Arvore *dir;
};
typedef struct Arvore Agenda;
void toUpper(char str[]);
void option(char op[]);
void showContacts();
void getName(char *name);
void getPhone(int *phone);
void menuPhonebook(int *num);
void order(Agenda *raiz);
Agenda** maior_esq(Agenda *raiz);
Agenda** menor_dir(Agenda *raiz);
void searchContact(Agenda *raiz, char str[]);
void insertContact(Agenda **raiz, char str[]);
void changeContact(Agenda **raiz, char str[]);
void deleteContact(Agenda **raiz, char str[]);
void message1();
void message2();
void message3();
void message4();
void message5();
void message6();
void message7();
void message8();
file: phonebook.c
#include <string.h>
#include "phonebook.h"
int strcasecmp(const char *s1, const char *s2);
void insertContact(Agenda **raiz , char str[50])
{
int phone;
if (*raiz == NULL)
{
*raiz = (Agenda*)malloc(sizeof(Agenda));
strcpy((*raiz)->name, str);
getPhone(&phone);
(*raiz)->phone = phone;
(*raiz)->esq = NULL;
(*raiz)->dir = NULL;
}
else
{
if (strcasecmp((*raiz)->name,str) > 0)
{
insertContact(&(*raiz)->esq, str);
}
else
{
if (strcasecmp((*raiz)->name,str) < 0)
{
insertContact(&(*raiz)->dir, str);
}
else
{
if (strcmp((*raiz)->name,str) == 0)
{
printf("NAME ALREADY EXISTS!\n");
}
}
}
}
}
void order(Agenda *raiz)
{
if (raiz!=NULL)
{
order((raiz)->esq);
printf("\n\tNAME: %s", (raiz)->name);
printf("\tPHONE: %d\n", (raiz)->phone);
order((raiz)->dir);
}
}
void searchContact(Agenda *raiz, char str[50])
{
if (raiz != NULL)
{
if (strcasecmp((raiz)->name,str) > 0)
{
searchContact((raiz)->esq, str);
}
else
{
if (strcasecmp((raiz)->name,str) < 0)
{
searchContact((raiz)->dir, str);
}
else
{
if (strcmp((raiz)->name,str) == 0)
{
message6();
printf("\n\tNAME: %s", (raiz)->name);
printf("\tPHONE: %d\n", (raiz)->phone);
}
}
}
}
else
{
message8();
}
}
void changeContact(Agenda **raiz, char *str)
{
if ((*raiz) != NULL)
{
if (strcasecmp((*raiz)->name, str) > 0)
{
changeContact(&(*raiz)->esq, str);
}
else
{
if (strcasecmp((*raiz)->name, str) < 0)
{
changeContact(&(*raiz)->dir, str);
}
else
{
if (strcmp((*raiz)->name, str) == 0)
{
int phone;
message6();
printf("\n\tNAME: %s", (*raiz)->name);
scanf(" %[^\n]s",(*raiz)->name);
printf("\n\tNEW PHONE: ");
scanf(" %d",&phone);
(*raiz)->phone = phone;
message8();
}
}
}
}
else
{
message8();
}
}
void deleteContact(Agenda **raiz, char str[])
{
Agenda **aux2, *aux3;
if (*raiz != NULL)
{
if (strcasecmp((*raiz)->name , str) == 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)->name, aux3->name);
free(aux3);
aux3 = NULL;
}
}
else
{
if (strcasecmp(str,(*raiz)->name) < 0)
{
deleteContact(&(*raiz)->esq, str);
}
else
{
deleteContact(&(*raiz)->dir, str);
}
}
}
else
{
message8();
}
}
Agenda** maior_esq(Agenda *raiz)
{
Agenda **aux = &(raiz);
if ((*aux)->esq != NULL)
{
aux = &(*aux)->esq;
while((*aux)->dir != NULL)
{
aux = &(*aux)->dir;
}
}
return aux;
}
Agenda** menor_dir(Agenda *raiz)
{
Agenda **aux = &(raiz);
if ((*aux)->dir != NULL)
{
aux = &(*aux)->dir;
while((*aux)->esq != NULL)
{
aux = &(*aux)->esq;
}
}
return aux;
}
void menuPhonebook(int *num)
{
printf("\033[2J\033[H");
printf("\t *-------------*\n");
printf("\t | PHONEBOOK |\n");
printf("\t|-----------------------|\n");
printf("\t| 1 - INSERT CONTACT |\n");
printf("\t|-----------------------|\n");
printf("\t| 2 - SHOW CONTACT |\n");
printf("\t|-----------------------|\n");
printf("\t| 3 - SEARCH CCONTACT |\n");
printf("\t|-----------------------|\n");
printf("\t| 4 - DELETE CONTACT |\n");
printf("\t|-----------------------|\n");
printf("\t| 5 - ALTER CCONTACT |\n");
printf("\t|-----------------------|\n");
printf("\t| 6 - EXIT |\n");
printf("\t|-----------------------|\n");
printf("\tCHOICE OPTION: ");
scanf("%d", num);
}
void message1()
{
printf("\033[2J\033[H");
printf("\t*-------------------*\n");
printf("\t| INSERT CONTACTS |\n");
printf("\t*-------------------*\n");
}
void message2()
{
printf("\033[2J\033[H");
printf("\t*-----------------*\n");
printf("\t| SHOW CONTACTS |\n");
printf("\t*-----------------*\n");
}
void message3()
{
printf("\033[2J\033[H");
printf("\t*------------------*\n");
printf("\t| SEARCH CONTACT |\n");
printf("\t*------------------*\n");
}
void message4()
{
printf("\033[2J\033[H");
printf("\t*------------------*\n");
printf("\t| DELETE CONTACT |\n");
printf("\t*------------------*\n");
}
void message5()
{
printf("\033[2J\033[H");
printf("\t*-----------------*\n");
printf("\t| ALTER CONTACT |\n");
printf("\t*-----------------*\n");
}
void message6()
{
printf("\t*----------------*\n");
printf("\t| RECORD FOUND |\n");
printf("\t*----------------*\n");
}
void message7()
{
printf("\033[2J\033[H");
printf("\t*-------------------*\n");
printf("\t| CHANGED CONTACT |\n");
printf("\t*-------------------*\n");
}
void message8()
{
printf("\033[2J\033[H");
printf("\t*------------------*\n");
printf("\t| NAME NOT FOUND |\n");
printf("\t*------------------*\n");
}
void getPhone(int *phone)
{
printf("\tENTER PHONE: ");
fflush(stdin);
scanf(" %d", phone);
}
void getName(char str[])
{
printf("\tENTER NAME: ");
fflush(stdin);
scanf(" %[^\n]s", str);
toUpper(str);
}
void toUpper(char str[])
{
size_t i;
for (i = 0; i < strlen(str); i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
str[i]-=32;
}
}
}
void option(char op[])
{
printf("\n\tDo you wan't continue? Enter (Y)-yes (N)-no: ");
fflush(stdin);
scanf(" %[^\n]s", op);
}
file: horario.c
#include "phonebook.h"
int main()
{
int num;
char op[2];
char name[100];
Agenda *raiz = NULL;
do {
menuPhonebook(&num);
switch(num)
{
case 1:
do {
message1();
getName(name);
insertContact(&raiz, name);
printf("\n\tDo you wan't continue? Enter (Y)-yes (N)-no: ");
scanf("%s", op);
}while (*op == 's' || *op =='S');
break;
case 2:
message2();
order(raiz);
break;
case 3:
message3();
getName(name);
searchContact(raiz,name);
break;
case 4:
do {
message4();
getName(name);
option(op);
if(*op == 'n' || *op == 'N')break;
deleteContact(&raiz,name);break;
} while(*op == 's' || *op == 'S');
break;
case 5:
message5();
getName(name);
changeContact(&raiz,name);
break;
}
} while(num != 6);
}
Compilation command:
gcc -o horario horario.c -std=c99 -Wall -Wextra -Wpedantic
fflush(stdin);is undefined behavior. 2) Don't usescanf(). Read a whole line as input withfgets()/getline()and then parse it withstrtol()/sscanf()etc. 3) Useislower()from<ctype.h>instead of(str[i] >= 'a' && str[i] <= 'z'). \$\endgroup\$printf("\033[2J\033[H");:\033[His unnecessary. Clearing the entire screen will reset the display's row/col to the top-left corner automatically. \$\endgroup\$stdio.hshould be included by each C file and not included in the phonebook.h file. The only reason we should refer the OP to SO is if the code isn't working as intended. \$\endgroup\$scanf(" %[^\n]s" ..., :sis unnecessary. The%[ ]IS the format specifier, complete, for a string, called a scanset. Every character up to the newline, including spaces, is part of the string being stored. 1) Always provide a width specifier, like%10[^\n], to prevent buffer overflow. 2) Always check the returned value from any of thescanf()family of functions. Please study the documentation carefully. (Comment reposted to fix my own copy/paste mistake in the format string... Doh!) \$\endgroup\$