-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
159 lines (140 loc) · 5.82 KB
/
Copy pathmain.cpp
File metadata and controls
159 lines (140 loc) · 5.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
/**
* @file main.cpp
* @authors Evan Brisbin, Jason Donkor, Ethan Fischer, Tim Stevens, Markose Mesay
* @date 2025-09-22
* @version 1.1
* @brief Main program to read ZIP code CSV and calculate geographic extremes per state.
* @details
* - Opens a ZIP code CSV file and reads each record using ZipCodeRecordBuffer.
* - Groups records by state and updates easternmost, westernmost, northernmost, and southernmost ZIP codes.
* - Generates two output files:
* 1. `runbystate.txt` — extreme ZIP codes per state.
* 2. `runbyzip.txt` — all ZIP codes with latitude and longitude.
*/
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <string>
#include <iomanip>
#include <limits>
#include <algorithm>
#include "ZipCodeRecordBuffer.h"
using namespace std;
/**
* @struct StateRecord
* @brief Holds the four extreme ZIP codes and their coordinates for a state.
* @details
* - Tracks easternmost, westernmost, northernmost, and southernmost ZIP codes.
* - Initialized with extreme numeric values to ensure the first record is correctly stored.
*/
struct StateRecord {
string easternmost_zip; /**< ZIP code with largest longitude. */
double easternmost_lon = -numeric_limits<double>::max(); /**< Longitude of easternmost ZIP. */
string westernmost_zip; /**< ZIP code with smallest longitude. */
double westernmost_lon = numeric_limits<double>::max(); /**< Longitude of westernmost ZIP. */
string northernmost_zip; /**< ZIP code with largest latitude. */
double northernmost_lat = -numeric_limits<double>::max(); /**< Latitude of northernmost ZIP. */
string southernmost_zip; /**< ZIP code with smallest latitude. */
double southernmost_lat = numeric_limits<double>::max(); /**< Latitude of southernmost ZIP. */
};
/**
* @brief Main program entry point.
* @details
* - Reads the ZIP code CSV.
* - Updates `StateRecord` map with geographic extremes.
* - Writes two output files:
* - `runbystate.txt`: state extremes.
* - `runbyzip.txt`: all ZIPs with coordinates.
* @return 0 on success, 1 on file errors.
*/
int main() {
map<string, StateRecord> all_states; /**< Map storing extreme ZIP codes for each state. */
vector<pair<string, StateRecord>> records_by_zip; /**< Vector to store ZIP records for sorting. */
ZipCodeRecordBuffer buffer;
/** @brief Open the CSV file */
ifstream file("data/us_postal_codes.csv");
if (!file.is_open()) {
cerr << "Error opening CSV file." << endl;
return 1;
}
string header;
getline(file, header); /**< Skip header line. */
/** @brief Read all records and populate state extremes and ZIP vector */
while (buffer.ReadRecord(file)) {
string state = buffer.getState();
string zip = buffer.getZipCode();
double latitude = buffer.getLatitude();
double longitude = buffer.getLongitude();
// Store by state
if (all_states.find(state) == all_states.end()) {
all_states[state] = StateRecord{};
}
StateRecord& record = all_states[state];
if (longitude > record.easternmost_lon) { record.easternmost_lon = longitude; record.easternmost_zip = zip; }
if (longitude < record.westernmost_lon) { record.westernmost_lon = longitude; record.westernmost_zip = zip; }
if (latitude > record.northernmost_lat) { record.northernmost_lat = latitude; record.northernmost_zip = zip; }
if (latitude < record.southernmost_lat) { record.southernmost_lat = latitude; record.southernmost_zip = zip; }
// Store for ZIP-based output
records_by_zip.push_back(make_pair(state, record));
}
file.close();
/** @section state_output Output by State
* Writes the extreme ZIP codes per state to `runbystate.txt`.
*/
ofstream state_file("runbystate.txt");
if (!state_file.is_open()) {
cerr << "Error opening runbystate.txt" << endl;
return 1;
}
/** @brief Write header */
state_file << left << setw(8) << "State"
<< setw(15) << "Easternmost"
<< setw(15) << "Westernmost"
<< setw(15) << "Northernmost"
<< setw(15) << "Southernmost" << "\n";
state_file << string(68, '-') << "\n";
/** @brief Write each state's extreme ZIPs */
for (const auto& pair : all_states) {
const auto& r = pair.second;
state_file << left << setw(8) << pair.first
<< setw(15) << r.easternmost_zip
<< setw(15) << r.westernmost_zip
<< setw(15) << r.northernmost_zip
<< setw(15) << r.southernmost_zip
<< "\n";
}
state_file.close();
/** @section zip_output Output by ZIP
* Writes all ZIP codes with latitude and longitude to `runbyzip.txt`.
*/
ofstream zip_file("runbyzip.txt");
if (!zip_file.is_open()) {
cerr << "Error opening runbyzip.txt" << endl;
return 1;
}
/** @brief Reopen CSV file for reading all ZIP records */
file.open("data/us_postal_codes.csv");
if (!file.is_open()) {
cerr << "Error reopening CSV file." << endl;
return 1;
}
getline(file, header); /**< Skip header line */
/** @brief Write ZIP header */
zip_file << left << setw(8) << "State"
<< setw(10) << "ZIP"
<< setw(10) << "Lat"
<< setw(10) << "Lon" << "\n";
zip_file << string(40, '-') << "\n";
/** @brief Write each ZIP record */
while (buffer.ReadRecord(file)) {
zip_file << left << setw(8) << buffer.getState()
<< setw(10) << buffer.getZipCode()
<< setw(10) << fixed << setprecision(4) << buffer.getLatitude()
<< setw(10) << fixed << setprecision(4) << buffer.getLongitude()
<< "\n";
}
file.close();
zip_file.close();
return 0;
}