#include <stdio.h>
#include <malloc.h>
#define MAX 50
typedef struct {
char numero[ 12 ] ; //permite um telefone nacional
char nome[ 31 ] ; //permite 30 caracteres
} Entrada;
typedef struct {
int tamanho;
Entrada * contatos;
} Agenda;
void cadastrar( Agenda * agenda) {
int op = 1 ;
do {
printf ( "Digite o telefone\n " ) ; scanf ( "%11s" , agenda
-> contatos
[ agenda
-> tamanho
] .
numero ) ; printf ( "Digite o seu nome\n " ) ; scanf ( " %30[^\n ]" , agenda
-> contatos
[ agenda
-> tamanho
] .
nome ) ; printf ( "Digite 1 para continuar 2 para parar\n " ) ; agenda-> tamanho++;
} while ( op != 2 ) ;
}
void imprime( Agenda agenda) {
for ( int i
= 0 ; i
< agenda.
tamanho ; i
++ ) printf ( "%11s - %s\n " , agenda.
contatos [ i
] .
numero , agenda.
contatos [ i
] .
nome ) ; }
Agenda iniciaAlocaAgenda( ) {
return ( Agenda
) { .
tamanho = 0 , .
contatos = malloc ( sizeof ( Entrada
) * MAX
) } ;
}
void liberaAgenda( Agenda agenda) {
}
int main( ) {
Agenda agenda = iniciaAlocaAgenda( ) ;
int op = 1 ;
do {
printf ( "1-Adicionar numero\n " ) ; printf ( "3-Procurar numero\n " ) ; printf ( "6-Reincia a agenda\n " ) ; switch ( op) {
case 1 :
cadastrar( & agenda) ;
break ;
case 2 :
case 3 :
break ;
case 4 :
imprime( agenda) ;
break ;
case 5 :
break ;
case 6 :
agenda = iniciaAlocaAgenda( ) ;
break ;
}
} while ( op != 7 ) ;
liberaAgenda( agenda) ;
}
//https://pt.stackoverflow.com/q/310814/101
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYWxsb2MuaD4KCiNkZWZpbmUgTUFYIDUwCgp0eXBlZGVmIHN0cnVjdCB7CiAgICBjaGFyIG51bWVyb1sxMl07IC8vcGVybWl0ZSB1bSB0ZWxlZm9uZSBuYWNpb25hbAogICAgY2hhciBub21lWzMxXTsgLy9wZXJtaXRlIDMwIGNhcmFjdGVyZ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yk7Cn0KCgppbnQgbWFpbigpIHsKICAgIEFnZW5kYSBhZ2VuZGEgPSBpbmljaWFBbG9jYUFnZW5kYSgpOwogICAgaW50IG9wID0gMTsKICAgIGRvIHsKICAgICAgICBwcmludGYoIjEtQWRpY2lvbmFyIG51bWVyb1xuIik7CiAgICAgICAgcHJpbnRmKCIyLVJlbW92ZXIgbnVtZXJvXG4iKTsKICAgICAgICBwcmludGYoIjMtUHJvY3VyYXIgbnVtZXJvXG4iKTsKICAgICAgICBwcmludGYoIjQtSW1wcmltaXJcbiIpOwogICAgICAgIHByaW50ZigiNi1SZWluY2lhIGEgYWdlbmRhXG4iKTsKICAgICAgICBwcmludGYoIjctUGFyYXJcbiIpOwogICAgICAgIHNjYW5mKCIlZCIsICZvcCk7CiAgICAgICAgc3dpdGNoIChvcCkgewogICAgICAgIGNhc2UgMToKICAgICAgICAgICAgY2FkYXN0cmFyKCZhZ2VuZGEpOwogICAgICAgICAgICBicmVhazsKICAgICAgICBjYXNlIDI6CiAgICAgICAgY2FzZSAzOgogICAgICAgICAgICBicmVhazsKICAgICAgICBjYXNlIDQ6CiAgICAgICAgICAgIGltcHJpbWUoYWdlbmRhKTsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgY2FzZSA1OgogICAgICAgICAgICBicmVhazsKICAgICAgICBjYXNlIDY6CiAgICAgICAgICAgIGFnZW5kYSA9IGluaWNpYUFsb2NhQWdlbmRhKCk7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgIH0gd2hpbGUgKG9wICE9IDcpOwogICAgbGliZXJhQWdlbmRhKGFnZW5kYSk7Cn0KCi8vaHR0cHM6Ly9wdC5zdGFja292ZXJmbG93LmNvbS9xLzMxMDgxNC8xMDE=