From c84027cdff2582f471e15baf1ae9a94569576448 Mon Sep 17 00:00:00 2001 From: y-syo Date: Fri, 29 Aug 2025 11:52:44 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix(ex00):=20a?= =?UTF-8?q?dded=20date=20parsing,=20ex00=20is=20done=20pogit=20c=20:D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex00/BitcoinExchange.cpp | 59 ++++++++++++++++++++++++++++++++-------- ex00/input.txt | 2 ++ ex00/main.cpp | 2 +- 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/ex00/BitcoinExchange.cpp b/ex00/BitcoinExchange.cpp index 6829de0..17bd55d 100644 --- a/ex00/BitcoinExchange.cpp +++ b/ex00/BitcoinExchange.cpp @@ -1,5 +1,12 @@ #include "BitcoinExchange.hpp" +bool is_leap_year(int year) +{ + if (year % 100 != 0 && year % 400 == 0) + return false; + return (year % 4 == 0); +} + Date::Date(void) { } @@ -7,7 +14,6 @@ Date::Date(void) Date::Date(std::string str) { // check format (4 char & isdigit into dash into 2 char isdigit & < 1 && isdigit & < 2, and same for day except ahah funny cuz 30 and 31 and 28 and 29 agfjasfgfjahkfga) - _value = 0; for (std::string::iterator it = str.begin(); it < str.end(); ++it) { @@ -16,6 +22,18 @@ Date::Date(std::string str) _value *= 10; _value += *it - '0'; } + + int year = _value / 10000; + int month = (_value / 100) % 100; + int day = _value % 100; + + if (month > 12 || month == 0 || day == 0) + throw std::invalid_argument("invalid date."); + + int monthly_days[] = {0, 31, is_leap_year(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 ,31 }; + + if (day > monthly_days[month]) + throw std::invalid_argument("invalid date."); } Date::Date(const Date &other) @@ -88,7 +106,13 @@ void BitcoinExchange::parseData(const std::string filename) std::string value_str; if (!std::getline(ss, date_str, ',') || !std::getline(ss, value_str, ',')) throw std::runtime_error("parsing issue in the data.csv file."); - this->_data.insert(std::make_pair(Date(date_str), std::strtod(value_str.c_str(), NULL))); + try { + this->_data.insert(std::make_pair(Date(date_str), std::strtod(value_str.c_str(), NULL))); + } + catch (std::invalid_argument &e) + { + throw std::invalid_argument(e.what()); + } } /*for (std::map::iterator it = this->_data.begin(); it != this->_data.end(); it++) @@ -108,7 +132,7 @@ std::string trim(const std::string &s) std::string::size_type end = s.size(); while (end > start && std::isspace(static_cast(s[end - 1]))) - --end; + --end; return s.substr(start, end - start); } @@ -142,16 +166,29 @@ void BitcoinExchange::str(std::string filename) std::cout << "Error: not a positive number." << std::endl; continue ; } - Date date(date_str); - std::map::iterator it = _data.lower_bound(date); - if (it == _data.end() || it->first > date) - { - if (it == _data.begin()) - it = _data.end(); - else + try { + Date date(date_str); + if (date < _data.begin()->first) + { + std::cout << "Error: input date too low." << std::endl; + continue ; + } + std::map::iterator it = _data.lower_bound(date); + if (it == _data.end() || it->first > date) + { + if (it == _data.begin()) + it = _data.end(); + else --it; + } + std::cout << date_str << " => " << value_str << " = " << it->second * std::strtod(value_str.c_str(), NULL) << std::endl; + + } + catch (std::invalid_argument &e) + { + std::cout << "Error: " << e.what() << std::endl; + continue ; } - std::cout << date_str << " => " << value_str << " = " << it->second * std::strtod(value_str.c_str(), NULL) << std::endl; } } diff --git a/ex00/input.txt b/ex00/input.txt index 3c0b3ec..96d7b85 100644 --- a/ex00/input.txt +++ b/ex00/input.txt @@ -1,4 +1,6 @@ date | value +1971-01-02 | 5 +1971-31-02 | 5 2011-01-03 | 3 2011-01-03 | 2 2011-01-03 | 1 diff --git a/ex00/main.cpp b/ex00/main.cpp index f298732..57575f8 100644 --- a/ex00/main.cpp +++ b/ex00/main.cpp @@ -7,9 +7,9 @@ int main(int argc, char **argv) std::cout << "Error: could not open file." << std::endl; return 1; } - BitcoinExchange btc; try { + BitcoinExchange btc; btc.str(argv[1]); } catch (std::runtime_error &e)