From: Thadeu Lima de Souza Cascardo Date: Sat, 1 Aug 2015 13:37:04 +0000 (-0300) Subject: Insere rendimentos de forma ordenada. X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fdeclara.git;a=commitdiff_plain;h=5f2dd49d87066d04229e35ba0a19a65c9204f286 Insere rendimentos de forma ordenada. O cabeçalho da declaração contém os CNPJs das pessoas jurídicas que pagaram os maiores rendimentos. Insere os rendimentos de forma ordenada, com os maiores rendimentos primeiro, e cria uma função para retornar o CNPJ de um rendimento dado o seu índice. --- diff --git a/gera.c b/gera.c index c394fad..f77c22b 100644 --- a/gera.c +++ b/gera.c @@ -80,10 +80,10 @@ static void gera_header(struct declaracao *dec, FILE *f) fprintf(f, " "); /* RRA4 */ fprintf(f, "%-11.11s", ""); /* CPF RRA4 */ fprintf(f, "0000000000000"); /* TODO: Doacao ECA */ - fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior */ - fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 2 */ - fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 3 */ - fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior 4 */ + fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 0)); /* TODO: CNPJ maior */ + fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 1)); /* TODO: CNPJ maior 2 */ + fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 2)); /* TODO: CNPJ maior 3 */ + fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 3)); /* TODO: CNPJ maior 4 */ fprintf(f, "%-11.11s", ""); /* CPF Dependente 1 */ fprintf(f, "%-8.8s", ""); /* DN Dependente 1 */ fprintf(f, "%-11.11s", ""); /* CPF Dependente 2 */ diff --git a/rendimento.c b/rendimento.c index 80311db..caa7a64 100644 --- a/rendimento.c +++ b/rendimento.c @@ -38,6 +38,18 @@ void rendimento_free(void *pointer) free(rendimento); } +static int rendimento_cmp(void *p1, void *p2) +{ + struct rendimento *r1 = p1; + struct rendimento *r2 = p2; + /* O rendimento maior vem primeiro. */ + if (r1->rendimento > r2->rendimento) + return -1; + else if (r1->rendimento < r2->rendimento) + return 1; + return 0; +} + static struct rendimento * rendimento_new(char **args) { struct rendimento *rendimento; @@ -73,7 +85,7 @@ static int run_rendimento(struct declaracao *dec, char **args, int argc) rendimento = rendimento_new(args); if (!rendimento) return -ENOMEM; - r = list_add(&dec->rendimento, rendimento); + r = list_insert_ordered(&dec->rendimento, rendimento, rendimento_cmp); if (r < 0) { rendimento_free(rendimento); return r; @@ -101,3 +113,12 @@ int rendimento_cmd_init(void) cmd_add(&cmd_rendimento); return 0; } + +char * rendimento_cnpj_ordenado(struct declaracao *dec, int i) +{ + struct rendimento *rendimento; + rendimento = list_get(dec->rendimento, i); + if (!rendimento) + return ""; + return rendimento->cnpj; +} diff --git a/rendimento.h b/rendimento.h index 0f183a7..2f96a2d 100644 --- a/rendimento.h +++ b/rendimento.h @@ -38,4 +38,6 @@ void rendimento_free(void *pointer); int rendimento_cmd_init(void); +char * rendimento_cnpj_ordenado(struct declaracao *dec, int i); + #endif