-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquerys.c
151 lines (134 loc) · 4.83 KB
/
querys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "querys.h"
#include "htmlTable.h"
#include "writeCSV.h"
#include "stationADT.h"
#define FIRST 0
#define THIRD 2
#define BLOCK 10
#define SECOND 1
#define SET_ERRNO 0
#define STRMAXLONG 20
#define AMOUNT_OF_TRIPS_DIGIT 10
#define QUERY_2_DATE_FORMAT_LONGITUD 20
#define ERROR_ALLOCATION "Error alocando memoria\n"
static int countDigit(int num) {
int count = 0;
// Caso especial para el número 0
if (num == 0) {
return 1;
}
// Contar los dígitos del número
while (num != 0) {
num /= 10;
count++;
}
return count;
}
static void writeQ1Rec(stationsADT stations, htmlTable tablaQ1, FILE * csvQ1){
while (hasNextCount(stations)){
size_t members=getMembersCount(stations);
size_t casuals=getCasualsCount(stations);
size_t total=getTotalCount(stations);
char * membersStr=calloc(1,sizeof(char)*(countDigit(members)+1));
char * casualsStr=calloc(1,sizeof(char)*(countDigit(casuals)+1));
char * totalStr=calloc(1,sizeof(char)*(countDigit(total)+1));
char * stationName=getStationNameCount(stations);
if (membersStr==NULL || casualsStr==NULL || totalStr ==NULL){
perror(ERROR_ALLOCATION);
exit(EXIT_FAILURE);
}
sprintf(membersStr,"%zu",members);
sprintf(casualsStr,"%zu",casuals);
sprintf(totalStr,"%zu",total);
addHTMLRow(tablaQ1,stationName,membersStr,casualsStr,totalStr);
writeRowQ1(csvQ1,stationName,membersStr,casualsStr,totalStr);
free(membersStr);
free(casualsStr);
free(totalStr);
nextCount(stations);
}
return;
}
void query1(stationsADT stations){
FILE * csvQ1=newFile("query1.csv");
writeHeaderQ1(csvQ1);
htmlTable tablaQ1 = newTable("query1.html",4,"bikeStation","memberTrips","casualTrips","allTrips");
toBeginCount(stations);
writeQ1Rec(stations,tablaQ1,csvQ1);
closeHTMLTable(tablaQ1);
fclose(csvQ1);
}
static void writeQ2Rec(stationsADT stations, htmlTable tablaQ2, FILE * csvQ2){
while (hasNextAlpha(stations))
{
if (hasRentsAlpha(stations))
{
char * stationNamestr = getStationNameAlpha(stations);
char * stationNameEndStr = getOldestRentalStationNameEndAlpha(stations);
struct tm startDate = getOldestRentalStartDateAlpha(stations);
char * startDateStr = calloc(1,sizeof(char)*QUERY_2_DATE_FORMAT_LONGITUD);
if (startDateStr==NULL){
perror(ERROR_ALLOCATION);
exit(EXIT_FAILURE);
}
strftime(startDateStr,QUERY_2_DATE_FORMAT_LONGITUD,"%d/%m/%Y %H:%M",&(startDate));
addHTMLRow(tablaQ2,stationNamestr,stationNameEndStr,startDateStr);
writeRowQ2(csvQ2,stationNamestr,stationNameEndStr,startDateStr);
free(startDateStr);
}
nextAlpha(stations);
}
}
void query2(stationsADT stations){
FILE * csvQ2 = newFile("query2.csv");
writeHeaderQ2(csvQ2);
htmlTable tablaQ2 = newTable("query2.html",3,"bikeStation","bikeEndStation","oldestDateTime");
toBeginAlpha(stations);
writeQ2Rec(stations,tablaQ2,csvQ2);
closeHTMLTable(tablaQ2);
fclose(csvQ2);
}
void query3(stationsADT stations){
size_t * startedTrips=getStartedTrips(stations);
size_t * endedTrips=getEndedTrips(stations);
char * weekDays[]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
errno = SET_ERRNO;
FILE * csvQ3 = newFile("query3.csv");
writeHeaderQ3(csvQ3);
htmlTable tablaQ3 = newTable("query3.html",3,"weekDay","startedTrips","endedTrips");
for (size_t i = 0; i < DAYS_IN_WEEK; i++)
{
char sT[STRMAXLONG];
sprintf(sT,"%lu",startedTrips[i]);
char eT[STRMAXLONG];
sprintf(eT,"%lu",endedTrips[i]);
writeRowQ3(csvQ3,*(weekDays+i),sT,eT);
addHTMLRow(tablaQ3,*(weekDays+i),sT,eT);
}
closeHTMLTable(tablaQ3);
fclose(csvQ3);
}
void query4(stationsADT stations){
FILE * csvQ4 = newFile("query4.csv");
writeHeaderQ4(csvQ4);
htmlTable tablaQ4 = newTable("query4.html",3,"bikeStation","mostPopRouteEndStation","mostPopRouteTrips");
toBeginAlpha(stations);
char * mostPopularName;
size_t amountOfTrips;
while(hasNextAlpha(stations)){
mostPopularName = getMostPopularFromStationAlpha(stations,&amountOfTrips);
char * name =getStationNameAlpha(stations);
char amountOfTripsStr[AMOUNT_OF_TRIPS_DIGIT];
sprintf(amountOfTripsStr,"%lu",amountOfTrips);
writeRowQ4(csvQ4,name, mostPopularName, amountOfTripsStr);
addHTMLRow(tablaQ4,name, mostPopularName, amountOfTripsStr);
nextAlpha(stations);
}
closeHTMLTable(tablaQ4);
fclose(csvQ4);
return;
}