diff options
author | Matt Strapp <matt@mattstrapp.net> | 2021-10-11 20:02:46 -0500 |
---|---|---|
committer | Matt Strapp <matt@mattstrapp.net> | 2021-10-11 20:02:46 -0500 |
commit | aa2a4faf537c9bc7e2e87ff38483e2bf53ed24cf (patch) | |
tree | d5049549bcb1fed21d118c200ae40bac7e1ebbcc /dev/a3-earthquake/earthquake_database.cc | |
parent | Fix gnu's stupidity (diff) | |
parent | Publish a3 (diff) | |
download | csci4611-aa2a4faf537c9bc7e2e87ff38483e2bf53ed24cf.tar csci4611-aa2a4faf537c9bc7e2e87ff38483e2bf53ed24cf.tar.gz csci4611-aa2a4faf537c9bc7e2e87ff38483e2bf53ed24cf.tar.bz2 csci4611-aa2a4faf537c9bc7e2e87ff38483e2bf53ed24cf.tar.lz csci4611-aa2a4faf537c9bc7e2e87ff38483e2bf53ed24cf.tar.xz csci4611-aa2a4faf537c9bc7e2e87ff38483e2bf53ed24cf.tar.zst csci4611-aa2a4faf537c9bc7e2e87ff38483e2bf53ed24cf.zip |
Merge branch 'support-code' of github.umn.edu:umn-csci-4611-f21/shared-upstream
Diffstat (limited to '')
-rw-r--r-- | dev/a3-earthquake/earthquake_database.cc | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/dev/a3-earthquake/earthquake_database.cc b/dev/a3-earthquake/earthquake_database.cc new file mode 100644 index 0000000..2c9a822 --- /dev/null +++ b/dev/a3-earthquake/earthquake_database.cc @@ -0,0 +1,77 @@ +/** CSci-4611 Assignment 3: Earthquake + */ + +#include <fstream> +#include <algorithm> + +#include "earthquake_database.h" + + +EarthquakeDatabase::EarthquakeDatabase(std::string filename) { + + std::ifstream in(filename.c_str()); + std::string line; + while (getline(in, line)) { + if (line.size() > 30) { + Earthquake e(line); + if (earthquakes.size() == 0) { + min_mag_ = (float)e.magnitude(); + max_mag_ = (float)e.magnitude(); + } + if (e.magnitude() > max_mag_) { + max_mag_ = (float)e.magnitude(); + } + if (e.magnitude() < min_mag_) { + min_mag_ = (float)e.magnitude(); + } + earthquakes.push_back(e); + } + } +} + +Earthquake EarthquakeDatabase::earthquake(int index) { + return earthquakes[index]; +} + +int EarthquakeDatabase::min_index() { + return 250; +} + +int EarthquakeDatabase::max_index() { + return (int)earthquakes.size() - 1; +} + +int EarthquakeDatabase::FindMostRecentQuake(Date d) { + double targetSeconds = d.ToSeconds(); + int start = min_index(); + int end = max_index(); + while (start < end-1) { + int half = (start + end) / 2; + if (earthquakes[half].date().ToSeconds() > targetSeconds) { + end = half - 1; + } + else { + start = half; + } + } + if (start == end) { + return start; + } + else { + if (earthquakes[end].date().ToSeconds() > targetSeconds) { + return start; + } + else { + return end; + } + } +} + +float EarthquakeDatabase::min_magnitude() { + return min_mag_; +} + +float EarthquakeDatabase::max_magnitude() { + return max_mag_; +} + |