-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupport.cpp
More file actions
208 lines (173 loc) · 4.82 KB
/
Support.cpp
File metadata and controls
208 lines (173 loc) · 4.82 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <iomanip>
#include <string>
#include "Support.h"
#include <fstream>
#include <sstream>
#include <queue>
using namespace std;
// Location member functions
bool location::operator<(const location &L) const{
// Matches state first then city
if (L.state == state) return (L.city > city);
return (L.state > state);
}
void location::print() const{
for (int i=0;i<42;i++) cout << "-";
cout << endl;
cout << city << ", " << state << " (" <<
geocode << ")" << endl;
for (int i=0;i<42;i++) cout << "-";
cout << endl;
}
// Raw data member functions
void rawdata::print() const{
cout << fixed << setprecision(2);
cout << setw(5) << month << " " << setw(5) << setprecision(2) << precip << " " << setw(5) << temp << endl;
}
// Summary member functions
summary::summary(){
N = 0;
precip_avg = 0;
precip_max = 0;
precip_min = 0;
temp_avg = 0;
temp_max = 0;
temp_min = 0;
}
void summary::operator+=(rawdata &D){
// Essentially, incorporate data
// Initialize stats values
if (N==0) {
precip_min = D.precip;
precip_max = D.precip;
temp_min = D.temp;
temp_max = D.temp;
}
else {
if (D.precip > precip_max) precip_max = D.precip;
if (D.precip < precip_min) precip_min = D.precip;
if (D.temp > temp_max) temp_max = D.temp;
if (D.temp < temp_min) temp_min = D.temp;
}
precip_avg += D.precip;
temp_avg += D.temp;
N += 1;
}
void summary::print() const{
cout << fixed << setprecision(2);
cout << ":" << setw(6) << setw(6) <<
precip_min <<setw(6) << precip_max << setw(6) << precip_avg/N
<< " : " << fixed << setprecision(1) << temp_min << " " << temp_max <<
" " << temp_avg/N << endl;
}
// Database member functions
void database::init_rawdata(string filename){
// Set up file reading
ifstream fin(filename.c_str());
if (!fin.is_open()){
cerr << filename << " failed to open." << endl;
return;
}
string Line;
while (getline(fin,Line)){
extract_rawdata(Line);
istringstream sin(Line);
location loc;
rawdata rd;
// Read to rawdata and location structs
sin >> rd.month >> loc.city >> loc.state >> loc.geocode;
sin >> rd.precip >> rd.temp;
int index;
map<location, int>::iterator p0 = location_map.find(loc);
// Set index
if (p0 == location_map.end()) {
index = location_map.size();
location_map[loc] = index;
rawdata_cache.push_back(list<rawdata>());
} else {
index = p0->second;
}
// Add raw data
rawdata_cache[index].push_back(rd);
}
}
void database::print_rawdata(){
// Handling all printing related to raw data;
for (map<location,int>::iterator p0 = location_map.begin(); p0 != location_map.end(); p0++){
p0->first.print();
list<rawdata>& Raw = rawdata_cache[p0->second];
for (list<rawdata>::iterator p1 = Raw.begin(); p1 != Raw.end(); p1++) {
p1->print();
}
}
}
void database::init_summary(){
extract_summary();
// Populate hash tables
for (map<location, int>::iterator p0 = location_map.begin(); p0 != location_map.end(); p0++) {
const location& loc = p0->first;
geocode_map[loc.geocode] = loc;
city_map[loc.city].insert(loc.geocode);
state_map[loc.state].insert(loc.geocode);
}
}
void database::print_summary(string target){
queue<string> geocodes;
// Input is a geocode
if (target.length() == 3) {
geocodes.push(target);
}
// Finds corresponding state geocodes
else if (state_map.find(target) != state_map.end()) {
for (set<string>::iterator p0 = state_map[target].begin(); p0 != state_map[target].end(); p0++){
geocodes.push(*p0);
}
}
// Finds corresponding city's geocodes
else if (city_map.find(target) != city_map.end()){
for (set<string>::iterator p0 = city_map[target].begin(); p0 != city_map[target].end(); p0++){
geocodes.push(*p0);
}
}
// Index corresponds to month-1
string months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
while (!geocodes.empty()){
location Loc = geocode_map[geocodes.front()];
// Checks a geocode and then prints out
if (geocode_map.find(geocodes.front()) != geocode_map.end()){
int index = location_map[Loc];
Loc.print();
for (int i = 0; i < 12; i++) {
cout << months[i];
summary_cache[index][i].print();
}
}
geocodes.pop();
}
}
void database::extract_rawdata(string &Line){
// To iterate through each character replacing ' ' -> '_'
for (size_t i=0; i<Line.size(); i++) {
if (Line[i] == ' ') {
Line[i] = '_';
}
}
// ',' -> ' '
for (size_t i=0; i<Line.size(); i++) {
if (Line[i] == ',') {
Line[i] = ' ';
}
}
}
void database::extract_summary(){
summary_cache.resize(rawdata_cache.size());
for (size_t i = 0; i < rawdata_cache.size(); i++){
summary_cache[i].resize(12);
// Iterate and update summary_cache with raw data
for (list<rawdata>::iterator p0 = rawdata_cache[i].begin(); p0 != rawdata_cache[i].end(); p0++){
int month = p0->month - 1;
summary_cache[i][month] += *p0;
}
}
}