From 5f2dd49d87066d04229e35ba0a19a65c9204f286 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Sat, 1 Aug 2015 10:37:04 -0300 Subject: [PATCH] Insere rendimentos de forma ordenada. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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. --- gera.c | 8 ++++---- rendimento.c | 23 ++++++++++++++++++++++- rendimento.h | 2 ++ 3 files changed, 28 insertions(+), 5 deletions(-) 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 -- 2.20.1