Estruturas de dados - struct com Menus Inteligentes em Linguagem C puro.
Esta estrutura é do tipo estática usando é claro vetor estático.
O programa é todo criado em funções simples, que são chamadas por um
menu inteligente com teclas do teclado do computador, e confirmado pela
tecla "ENTER", muito seguro,menu de um funcionamento excelente e o código é
100 por cento funcional, gastei muito tempo pra deixá-lo funcionando, criado
pacientemente na minha IDE padrão, o eclipse é claro, e está rodando liso,
e leve como uma pluma, por aqui não foi detectado nenhum warnings, quando foi compilado pelo mingw32-3.4.2.
Apesar de tudo isto eu recomendo o código a iniciantes em Linguagem C,
que estão aprendendo montar estruturas para cadastro, com inserção, listagem, alteração e remoção.
Veja abaixo um vídeo com o funcionamento do Programa:
Veja abaixo o código do programa:
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
struct estructura_amigo {
char nome [ 20 ];
char apelido [10 ];
char telefone [ 12 ];
int idade;
int cod;
}
amigo [ 8 ];
int i, codigo;
void Alterar_Amigo ( int j );
void Lista_Amigos ( int j );
void MenuPrimcipal ( int j );
void Janela5 ( );
void Remove_Amigo ( int j ) {
system ( "cls" );
int conf;
Janela5 ( );
do {
textcolor ( LIGHTRED );
gotoxy ( 30 , 3 );
printf ( "PROGRAMA
CADASTRO DE AMIGOS" );
textcolor ( LIGHTGRAY );
gotoxy ( 30 , 5 );
printf ( "REMOVENDO
CADASTRO DE AMIGOS" );
textcolor ( YELLOW );
gotoxy ( 27 , 7 );
printf ( "INFORME UM
CODIGO [ ENTRE 1 E 7 ]" );
textcolor ( LIGHTRED );
gotoxy ( 40 , 9 );
scanf ( "%d" , &codigo );
fflush (stdin);
if ( codigo < 1 || codigo > 7 ) {
textcolor ( LIGHTBLUE );
gotoxy ( 35 , 9 );
printf ( "\aCODIGO
INVALIDO !" );
getche ( );
system ( "cls" );
Remove_Amigo ( j );
}
if ( amigo [ codigo ].cod != '\0' ) {
} else {
textcolor ( LIGHTBLUE );
gotoxy ( 29 , 9 );
printf ( "\aNADA
CADASTRADO NESTE CODIGO !" );
getche ( );
system ( "cls" );
MenuPrimcipal ( j );
}
} while ( codigo < 1 || codigo > 7 );
if ( amigo [ codigo ].cod != '\0' ) {
system ( "cls" );
textcolor ( LIGHTRED );
gotoxy ( 30 , 3 );
printf ( "PROGRAMA
CADASTRO DE AMIGOOS" );
textcolor ( LIGHTGRAY );
gotoxy ( 35 , 5 );
printf ( "REMOVENDO
CADASTRO DE AMIGOS" );
textcolor ( YELLOW );
gotoxy ( 35 , 7 );
printf ( "DADOS DO
AMIGO" );
textcolor ( LIGHTRED );
gotoxy ( 52 , 7 );
printf ( " %d " , codigo );
textbackground ( BLACK );
textcolor ( DARKGRAY );
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 9 );
printf ( "Nome do
amigo: " );
textcolor ( LIGHTRED );
gotoxy ( 46 , 9 );
printf ( " %s " , amigo [ codigo ].nome );
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 11 );
printf ( "Apelido: " );
textcolor ( LIGHTRED );
gotoxy ( 46 , 11 );
printf ( " %s " , amigo [ codigo ].apelido );
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 13 );
printf ( "Idade:
Anos" );
textcolor ( LIGHTRED );
gotoxy ( 46 , 13 );
printf ( " %i " , amigo [ codigo ].idade );
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 15 );
printf ( "Telefone: " );
textcolor ( LIGHTRED );
gotoxy ( 46 , 15 );
printf ( " %s " , amigo [ codigo ].telefone );
getche ( );
do {
textcolor ( LIGHTBLUE );
gotoxy ( 17 , 17 );
printf (
"DESEJA
REMOVER O CADASTRO ? [ 1 PARA SIM E 2 PARA NAO ]" );
textcolor ( LIGHTRED );
gotoxy ( 40 , 19 );
scanf ( "%d" , &conf );
fflush (stdin);
if ( conf == 1 ) {
amigo [ codigo ].cod = '\0';
amigo [ codigo ].nome [ 0 ] = '\0';
amigo [ codigo ].apelido [ 0 ] = '\0';
amigo [ codigo ].idade = '\0';
amigo [ codigo ].telefone [ 0 ] = '\0';
textcolor ( YELLOW );
gotoxy ( 25 , 19 );
printf ( "\a CADASTRO
REMOVIDO COM SUCESSO!" );
getche ( );
system ( "cls" );
MenuPrimcipal ( j );
}
if ( conf == 2 ) {
textcolor ( YELLOW );
gotoxy ( 29 , 19 );
printf ( "PROCESSO DE
REMOCAO CANCELADO!" );
getche ( );
system ( "cls" );
MenuPrimcipal ( j );
} else {
textcolor ( GREEN );
gotoxy ( 38 , 19 );
printf ( "\aOPCAO ERRADA" );
Sleep ( 1800 );
gotoxy ( 38 , 19 );
printf ( " " );
}
} while ( 1 );
}
}
void Alterar_Amigo ( int j ) {
system ( "cls" );
int conf;
Janela5 ( );
do {
textcolor ( LIGHTRED );
gotoxy ( 30 , 3 );
printf ( "PROGRAMA
CADASTRO DE AMIGOS" );
textcolor ( LIGHTGRAY );
gotoxy ( 30 , 5 );
printf ( "ALTERANDO
CADASTRO DE AMIGOS" );
textcolor ( YELLOW );
gotoxy ( 27 , 7 );
printf ( "INFORME UM
CODIGO [ ENTRE 1 E 7 ]" );
textcolor ( LIGHTRED );
gotoxy ( 40 , 9 );
scanf ( "%d" , &codigo );
fflush (stdin);
if ( codigo < 1 || codigo > 7 ) {
textcolor ( LIGHTBLUE );
gotoxy ( 35 , 9 );
printf ( "\aCODIGO INVALIDO !" );
getche ( );
system ( "cls" );
}
} while ( codigo < 1 || codigo > 7 );
if ( amigo [ codigo ].cod != '\0' ) {
} else {
textcolor ( LIGHTBLUE );
gotoxy ( 29 , 9 );
printf ( "\aNADA
CADASTRADO NESTE CODIGO !" );
getche ( );
system ( "cls" );
MenuPrimcipal ( j );
}
if ( amigo [ codigo ].cod != '\0' ) {
system ( "cls" );
textcolor ( LIGHTRED );
gotoxy ( 30 , 3 );
printf ( "PROGRAMA
CADASTRO DE AMIGOOS" );
textcolor ( LIGHTGRAY );
gotoxy ( 35 , 5 );
printf ( "ALTERANDO
CADASTRO DE AMIGOS" );
textcolor ( YELLOW );
gotoxy ( 35 , 7 );
printf ( "DADOS DO
AMIGO" );
textcolor ( LIGHTRED );
gotoxy ( 52 , 7 );
printf ( " %d " , codigo );
textbackground ( BLACK );
textcolor ( DARKGRAY );
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 9 );
printf ( "Nome do
amigo: " );
textcolor ( LIGHTRED );
gotoxy ( 46 , 9 );
printf ( " %s " , amigo [ codigo ].nome );
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 11 );
printf ( "Apelido: " );
textcolor ( LIGHTRED );
gotoxy ( 46 , 11 );
printf ( " %s " , amigo [ codigo ].apelido );
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 13 );
printf ( "Idade:
Anos" );
textcolor ( LIGHTRED );
gotoxy ( 46 , 13 );
printf ( " %i " , amigo [ codigo ].idade );
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 15 );
printf ( "Telefone: " );
textcolor ( LIGHTRED );
gotoxy ( 46 , 15 );
printf ( " %s " , amigo [ codigo ].telefone );
getche ( );
do {
textcolor ( LIGHTBLUE );
gotoxy ( 17 , 17 );
printf ( "DESEJA
ALTERAR O CADASTRO ? [ 1 PARA SIM E 2 PARA NAO ]" );
textcolor ( LIGHTRED );
gotoxy ( 40 , 19 );
scanf ( "%d" , &conf );
fflush (stdin);
if ( conf == 2 ) {
textcolor ( YELLOW );
gotoxy ( 29 , 19 );
printf ( "PROCESSO DE
ALTERACAO CANCELADO!" );
getche ( );
system ( "cls" );
MenuPrimcipal ( j );
}
if ( conf == 1 ) {
system ( "cls" );
textcolor ( LIGHTRED );
gotoxy ( 30 , 3 );
printf ( "PROGRAMA
CADASTRO DE AMIGOS" );
textcolor ( LIGHTBLUE );
gotoxy ( 30 , 5 );
printf ( "ALTERANDO
CADASTRO DE AMIGOS" );
textcolor ( LIGHTCYAN );
gotoxy ( 32 , 7 );
printf ( "DIGITE OS
DADOS DO AMIGO" );
textcolor ( LIGHTGRAY );
gotoxy ( 30 , 9 );
printf ( "Nome: " );
textcolor ( LIGHTRED );
gets ( amigo [ codigo ].nome );
fflush ( stdin );
textcolor ( LIGHTGRAY );
gotoxy ( 30 , 11 );
printf ( "Apelido:
" );
textcolor ( LIGHTRED );
gets ( amigo [ codigo ].apelido );
fflush ( stdin );
textcolor ( LIGHTGRAY );
gotoxy ( 30 , 13 );
printf ( "Telefone:
" );
textcolor ( LIGHTRED );
gets ( amigo [ codigo ].telefone );
fflush ( stdin );
textcolor ( LIGHTGRAY );
gotoxy ( 30 , 15 );
printf ( "Idade: " );
textcolor ( LIGHTRED );
scanf ( "%i" , &amigo [ codigo ].idade );
fflush ( stdin );
textcolor ( YELLOW );
gotoxy ( 30 , 17 );
printf ( "CADASTRO
ALTERADO COM SUCESSO" );
getche ( );
system ( "cls" );
MenuPrimcipal ( j );
} else {
textcolor ( YELLOW );
gotoxy ( 38 , 19 );
printf ( "\aOPCAO ERRADA" );
Sleep ( 1800 );
gotoxy ( 38 , 19 );
printf ( " " );
}
} while ( 1 );
}
}
void Lista_Amigos ( int j ) {
system ( "cls" );
Janela5 ( );
do {
textcolor ( LIGHTRED );
gotoxy ( 30 , 3 );
printf ( "PROGRAMA
CADASTRO DE AMIGOS" );
textcolor ( LIGHTGRAY );
gotoxy ( 30 , 5 );
printf ( "LISTANDO
CADASTRO DE AMIGOS" );
textcolor ( YELLOW );
gotoxy ( 27 , 7 );
printf ( "INFORME UM
CODIGO [ ENTRE 1 E 7 ]" );
textcolor ( LIGHTRED );
gotoxy ( 40 , 9 );
scanf ( "%d" , &codigo );
fflush (stdin);
if ( codigo < 1 || codigo > 7 ) {
textcolor ( LIGHTBLUE );
gotoxy ( 35 , 9 );
printf ( "\aCODIGO INVALIDO !" );
getche ( );
system ( "cls" );
}
} while ( codigo < 1 || codigo > 7 );
if ( amigo [ codigo ].cod != '\0' ) {
} else {
textcolor ( LIGHTBLUE );
gotoxy ( 29 , 9 );
printf ( "\aNADA
CADASTRADO NESTE CODIGO !" );
getche ( );
system ( "cls" );
MenuPrimcipal ( j );
}
if ( amigo [ codigo ].cod != '\0' ) {
system ( "cls" );
textcolor ( LIGHTRED );
gotoxy ( 30 , 3 );
printf ( "PROGRAMA
CADASTRO DE AMIGOOS" );
textcolor ( LIGHTGRAY );
gotoxy ( 35 , 5 );
printf ( "LISTANDO
CADASTRO DE AMIGOS" );
textcolor ( YELLOW );
gotoxy ( 35 , 7 );
printf ( "DADOS DO
AMIGO" );
textcolor ( LIGHTRED );
gotoxy ( 52 , 7 );
printf ( " %d " , codigo );
textbackground ( BLACK );
textcolor ( DARKGRAY );
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 9 );
printf ( "Nome do
amigo: " );
textcolor ( LIGHTRED );
gotoxy ( 46 , 9 );
printf ( " %s " , amigo [ codigo ].nome );
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 11 );
printf ( "Apelido:
" );
textcolor ( LIGHTRED );
gotoxy ( 46 , 11 );
printf ( " %s " , amigo [ codigo ].apelido );
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 13 );
printf ( "Idade: Anos" );
textcolor ( LIGHTRED );
gotoxy ( 46 , 13 );
printf ( " %i " , amigo [ codigo ].idade );
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 15 );
printf ( "Telefone:
" );
textcolor ( LIGHTRED );
gotoxy ( 46 , 15 );
printf ( " %s " , amigo [ codigo ].telefone );
getche ( );
MenuPrimcipal ( j );
}
}
void Insere_Amigo ( int j ) {
system ( "cls" );
Janela5 ( );
do {
textcolor ( LIGHTRED );
gotoxy ( 30 , 3 );
printf ( "PROGRAMA
CADASTRO DE AMIGOS" );
textcolor ( LIGHTGRAY );
gotoxy ( 35 , 5 );
printf ( "CADASTRANDO
AMIGOS" );
textcolor ( YELLOW );
gotoxy ( 27 , 7 );
printf ( "INFORME UM
CODIGO [ ENTRE 1 E 7 ]" );
textcolor ( LIGHTRED );
gotoxy ( 40 , 9 );
scanf ( "%d" , &codigo );
fflush (stdin);
if ( codigo < 1 || codigo > 7 ) {
textcolor ( LIGHTBLUE );
gotoxy ( 35 , 9 );
printf ( "\aCODIGO
INVALIDO !" );
getche ( );
system ( "cls" );
} else {
if ( amigo [ codigo ].cod != '\0' ) {
textcolor ( LIGHTBLUE );
gotoxy ( 35 , 9 );
printf ( "\aPOSICAO
OCUPADA !" );
getche ( );
system ( "cls" );
MenuPrimcipal ( j );
}
}
} while ( codigo < 1 || codigo > 7 );
amigo [ codigo ].cod = codigo;
textcolor ( LIGHTCYAN );
gotoxy ( 30 , 9 );
printf ( "DIGITE OS
DADOS DO AMIGO" );
textcolor ( LIGHTRED );
gotoxy ( 57 , 9 );
printf ( " %d " , codigo );
textcolor ( LIGHTGRAY );
gotoxy ( 30 , 11 );
printf ( "Nome: " );
textcolor ( LIGHTRED );
gets ( amigo [ codigo ].nome );
fflush (stdin);
textcolor ( LIGHTGRAY );
gotoxy ( 30 , 13 );
printf ( "Apelido:
" );
textcolor ( LIGHTRED );
gets ( amigo [ codigo ].apelido );
fflush ( stdin );
textcolor ( LIGHTGRAY );
gotoxy ( 30 , 15 );
printf ( "Telefone:
" );
textcolor ( LIGHTRED );
gets ( amigo [ codigo ].telefone );
fflush ( stdin );
textcolor ( LIGHTGRAY );
gotoxy ( 30 , 17 );
printf ( "Idade: " );
textcolor ( LIGHTRED );
scanf ( "%i" , &amigo [ codigo ].idade );
fflush ( stdin );
textcolor ( YELLOW );
gotoxy ( 27 , 19 );
printf ( "CADASTRO
REALIZADO COM SUCESSO" );
getche ( );
MenuPrimcipal ( j );
}
time_t hora_atual;
void Funcexit ( ) {
Janela5 ( );
gotoxy ( 32 , 11 );
printf ( "OPERACAO
FINALIZADA" );
gotoxy ( 2 , 23 );
Sleep ( 1800 );
gotoxy ( 20 , 13 );
printf ( "MUITO
OBRIGADO POR TER USADO ESTE PROGRAMA\n" );
time ( &hora_atual );
gotoxy ( 20 , 15 );
printf ( "A data e hora
atuais sao: %s" , ctime ( &hora_atual ) );
Sleep ( 2800 );
exit ( 0 );
gotoxy ( 3 , 24 );
}
void Janela6 ( ) {
int lin, col;
col = 0;
for ( lin = 2; lin <= 24 ; lin++ )
for ( col = 3; col <= 78 ; col++ ) {
gotoxy ( col , lin );
textbackground ( 6 );
printf ( " " );
}
col = 0;
for ( lin = 5; lin <= 21 ; lin++ )
for ( col = 6; col <= 75 ; col++ ) {
gotoxy ( col , lin );
textbackground ( 16 );
printf ( " " );
}
}
void Janela5 ( ) {
int lin, col;
col = 0;
for ( lin = 2; lin <= 24 ; lin++ )
for ( col = 2; col <= 78 ; col++ ) {
gotoxy ( col , lin );
textbackground ( WHITE );
printf ( " " );
}
}
#define MaxNo_Menu 5 // Define quantos menu
terá seu programa
void MenuPrimcipal ( int j ) {
system ( "title
PROGRAMA CADASTRO DE AMIGOS" );
system ( "cls" );
Janela6 ( );
textcolor ( LIGHTRED );
gotoxy ( 28 , 3 );
printf ( "PROGRAMA
CADASTRO DE AMIGOS" );
textcolor ( YELLOW );
gotoxy ( 22 , 7 );
printf ( "PROGRAMA
DESENVOLVIDO POR:" );
textcolor ( LIGHTCYAN );
gotoxy ( 49 , 7 );
printf ( "Samuel
Lima" );
textcolor ( LIGHTGREEN );
gotoxy ( 30 , 9 );
printf ( "sa_sp10@hotmail.com" );
textcolor ( LIGHTBLUE );
gotoxy ( 29 , 11 );
printf ( "ESCOLHA UMA
OPCAO ABAIXO" );
char *menu_list [ MaxNo_Menu ] = { "CADASTRAR
AMIGOS",
"LISTAR
CADASTRO DE AMIGOS", "REMOVER CADASTRO DE AMIGOS",
"ALTERAR
CADASTRO DE AMIGOS", "SAIR" };
int i, xpos = 10, ypos [ MaxNo_Menu ] = { 13, 15, 17, 19, 21 }; // lista do menu com o
número da linha
for ( i = 0; i < MaxNo_Menu ; ++i ) {
gotoxy ( xpos , ypos [ i ] );
textattr ( 12 );
printf ( "%s" , menu_list [ i ] );
}
i = 0;
while ( 1 ) {
gotoxy ( xpos , ypos [ i ] );
textattr ( 16 );
printf ( "%s" , menu_list [ i ] );
switch ( _getch ( ) ) {
case 72:
if ( i > 0 ) {
gotoxy ( xpos , ypos [ i ] );
textattr ( 14 );
printf ( "%s" , menu_list [ i ] );
--i;
}
break;
case 80:
if ( i < MaxNo_Menu - 1 ) {
gotoxy ( xpos , ypos [ i ] );
textattr ( 14 );
printf ( "%s" , menu_list [ i ] );
++i;
}
break;
case 13:
if ( i == 0 ) {
system ( "cls" );
Insere_Amigo ( j );
}
if ( i == 1 ) {
system ( "cls" );
Lista_Amigos ( j );
}
if ( i == 2 ) {
system ( "cls" );
Remove_Amigo ( j );
}
if ( i == 3 ) {
system ( "cls" );
Alterar_Amigo ( j );
}
if ( i == 4 ) {
system ( "cls" );
Funcexit ( );
}
break;
}
}
}
int main ( void ) {
system ( "title
PROGRAMA CADASTRO DE AMIGOS" );
int j = 0;
for ( i = 1; i <= 7 ; i++ ) {
amigo [ i ].cod = '\0';
amigo [ i ].nome [ 0 ] = '\0';
amigo [ i ].apelido [ 0 ] = '\0';
amigo [ i ].idade = '\0';
amigo [ i ].telefone [ 0 ] = '\0';
}
MenuPrimcipal ( j );
}