commit 38da0843026c71ec0d065c62c0538c2fade63107
parent 8c88c53d19e5c5edf43867eb69867e4c9170816d
Author: klewer-martin <martin.cachari@gmail.com>
Date: Mon, 8 Feb 2021 02:44:22 -0300
Update: Finally working as it should, added a feature that prints
the monthly infected by country;
Diffstat:
4 files changed, 40 insertions(+), 20 deletions(-)
diff --git a/main.c b/main.c
@@ -37,6 +37,10 @@ int main(int argc, char * argv[])
ulong country, date, infected;
country = date = infected = 0;
+ prev_month = prev_country = -1;
+
+ infected_monthly = 0;
+
// El siguiente arreglo de dos dimensiones es donde se van a guardar los
// codigos de los paises;
char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH];
@@ -67,7 +71,6 @@ int main(int argc, char * argv[])
if((fpo = fopen(dest, "w")) == NULL)
return ERROR_READING_FILE;
-
size_t line;
for(line = 0; (st = read_file(fpi, &country, &date, &infected)) == OK; line++) {
if(line != 0)
@@ -79,6 +82,8 @@ int main(int argc, char * argv[])
if(st != OK && st != END_OF_INPUT_FILE)
return st;
+ fprintf(fpo, "Infectados por mes: %lu\n", infected_monthly);
+
fclose(fpi);
fclose(fpo);
diff --git a/main.h b/main.h
@@ -8,6 +8,7 @@
#include "macros.h"
+
#define COUNTRY_CODES_FILE_NAME "iso3166-1.csv"
#define COUNTRIES_NUMBER 1000
@@ -19,12 +20,18 @@
#define SIZE_OF_BUFF2 32
#define INITIAL_SIZE 1000
-#define TIME_MAX_DIGITS 1000
+#define TIME_MAX_DIGITS 16
+
extern const char formato_de_la_fecha[];
extern ulong country, date, infected;
+extern ulong infected_monthly;
+
+extern int prev_month, prev_country;
+
+
typedef enum {
OK,
IO_FILE_NOT_FOUND,
@@ -35,7 +42,7 @@ typedef enum {
ERROR_READING_FILE,
ERROR_ALLOCATING_TIME,
ERROR_DATA_ON_FILE_MISSING,
- END_OF_INPUT_FILE
+ END_OF_INPUT_FILE,
} status_t;
diff --git a/output.txt b/output.txt
@@ -10,6 +10,8 @@ Pais: Argentina
Fecha: 15 Jan 2020
Infectados: 9324
+Infectados por mes: 16589
+
Pais: Colombia
Fecha: 01 Jan 2020
Infectados: 8234
@@ -22,6 +24,8 @@ Pais: Colombia
Fecha: 15 Jan 2020
Infectados: 9423
+Infectados por mes: 35246
+
Pais: Germany
Fecha: 01 Jan 2020
Infectados: 8432
@@ -34,3 +38,4 @@ Pais: Germany
Fecha: 20 Jan 2020
Infectados: 4214
+Infectados por mes: 48589
diff --git a/print_file.c b/print_file.c
@@ -2,33 +2,36 @@
const char formato_de_la_fecha[] = "%d %b %Y";
+int prev_month, prev_country;
+ulong infected_monthly;
+
status_t print_file(FILE *dest, char country_codes[COUNTRIES_NUMBER][ARRAYS_LENGTH], ulong *country, ulong *date, ulong *infected) {
-
+ int month;
char time_s[TIME_MAX_DIGITS];
time_translator(*(date), time_s, sizeof(time_s), "%m");
-// month = atoi(time_s);
-// printf("%d\n", month);
-//
-// if(prev_country == 0 && prev_month == -1) {
-// prev_country = country;
-// prev_month = month;
-// }
-//
+
+ month = atoi(time_s);
+ if((prev_month == -1) || (prev_country == -1)) {
+ prev_month = month;
+ prev_country = *country;
+ }
+
// Imprime la suma de infectados por mes cada vez que cambia el pais;
-// if(country == prev_country && month == prev_month) {
-// infected_monthly += infected;
-// }
-// else if(country != prev_country) {
-// fprintf(fpo, "Infectados por mes: %lu\n\n", infected_monthly);
-// prev_country = country;
-// }
+ if(*(country) == prev_country && month == prev_month) {
+ infected_monthly += *(infected);
+ }
+ else if(*(country) != prev_country || month != prev_month) {
+ fprintf(dest, "Infectados por mes: %lu\n\n", infected_monthly);
+ prev_country = *(country);
+ }
+
// Imprime datos segun el archivo de entrada;
fprintf_country(dest, *(country), country_codes);
fprintf_date(dest, *(date));
fprintf_infected(dest, *(infected), '\n');
-
+
return OK;
}