Skip to content

Instantly share code, notes, and snippets.

@fabianosalles
Last active December 2, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabianosalles/5d38815469264127cce242ec1ba8259a to your computer and use it in GitHub Desktop.
Save fabianosalles/5d38815469264127cce242ec1ba8259a to your computer and use it in GitHub Desktop.
/*** API de Tarefas ***/
Tarefa *tarefa_create(char *descricao, bool pendente) {
Tarefa *result = calloc(1, sizeof(Tarefa));
memcpy(result->descicao, descricao, strlen(descricao));
result->pendente = pendente;
return result;
}
/*** API de Telefones ***/
Telefone *telefone(char tipo, char *ddd, char *numero) {
Telefone *result = calloc(1, sizeof(Telefone));
result->tipo = tipo;
memcpy(result->ddd, ddd, strlen(ddd));
memcpy(result->numero, numero, strlen(numero));
return result;
}
/*** API de Pessoas ***/
Pessoa *pessoa_create(char *nome) {
Pessoa *result = calloc(1,sizeof(Pessoa));
memcpy(result->nome, nome, strlen(nome));
result->tarefas = list_create(sizeof(Tarefa));
result->telefones = list_create(sizeof(Pessoa));
return result;
}
void pessoa_destroy(Pessoa *pessoa){
list_destroy(pessoa->tarefas);
list_destroy(pessoa->telefones);
free(pessoa);
}
void adicionar_tarefa(Pessoa *pessoa, Tarefa *tarefa) {
list_add(pessoa->tarefas, tarefa);
}
void adicionar_telefone(Pessoa *pessoa, Telefone *telefone) {
list_add(pessoa->telefones, telefone);
}
void listar_tarefas(Pessoa *pessoa) {
Node *node = pessoa->tarefas->head;
Tarefa *tarefa;
int i = 1;
printf("Tarefas:\n");
while (node != NULL){
tarefa = (Tarefa *)node->data;
printf(" [%s] %02d - %s\n", tarefa->pendente ? " " : "OK", i, tarefa->descicao);
node = node->next;
i++;
}
}
void listar_telefones(Pessoa *pessoa) {
Node *node = pessoa->telefones->head;
Telefone *telefone;
int i = 1;
printf("Telefones:\n");
while (node != NULL) {
telefone = (Telefone *)node->data;
switch (telefone->tipo){
case TEL_FIXO:
printf(" FIXO (%s) %s\n", telefone->ddd, telefone->numero);
break;
case TEL_CELULAR:
printf(" CELULAR (%s) %s\n", telefone->ddd, telefone->numero);
break;
}
node = node->next;
i++;
}
}
void listar_detalhes(Pessoa *pessoa) {
printf("Listando dados de %s:\n", pessoa->nome);
pessoa_listar_tarefas(pessoa);
pessoa_listar_telefones(pessoa);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment