🔨」 fix(ex00): added date parsing, ex00 is done pogit c :D

This commit is contained in:
y-syo
2025-08-29 11:52:44 +02:00
parent 1458548d67
commit c84027cdff
3 changed files with 51 additions and 12 deletions

View File

@@ -1,5 +1,12 @@
#include "BitcoinExchange.hpp" #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) Date::Date(void)
{ {
} }
@@ -7,7 +14,6 @@ Date::Date(void)
Date::Date(std::string str) 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) // 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; _value = 0;
for (std::string::iterator it = str.begin(); it < str.end(); ++it) for (std::string::iterator it = str.begin(); it < str.end(); ++it)
{ {
@@ -16,6 +22,18 @@ Date::Date(std::string str)
_value *= 10; _value *= 10;
_value += *it - '0'; _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) Date::Date(const Date &other)
@@ -88,7 +106,13 @@ void BitcoinExchange::parseData(const std::string filename)
std::string value_str; std::string value_str;
if (!std::getline(ss, date_str, ',') || !std::getline(ss, value_str, ',')) if (!std::getline(ss, date_str, ',') || !std::getline(ss, value_str, ','))
throw std::runtime_error("parsing issue in the data.csv file."); 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<Date, double>::iterator it = this->_data.begin(); it != this->_data.end(); it++) /*for (std::map<Date, double>::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(); std::string::size_type end = s.size();
while (end > start && std::isspace(static_cast<unsigned char>(s[end - 1]))) while (end > start && std::isspace(static_cast<unsigned char>(s[end - 1])))
--end; --end;
return s.substr(start, end - start); 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; std::cout << "Error: not a positive number." << std::endl;
continue ; continue ;
} }
Date date(date_str); try {
std::map<Date, double>::iterator it = _data.lower_bound(date); Date date(date_str);
if (it == _data.end() || it->first > date) if (date < _data.begin()->first)
{ {
if (it == _data.begin()) std::cout << "Error: input date too low." << std::endl;
it = _data.end(); continue ;
else }
std::map<Date, double>::iterator it = _data.lower_bound(date);
if (it == _data.end() || it->first > date)
{
if (it == _data.begin())
it = _data.end();
else
--it; --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;
} }
} }

View File

@@ -1,4 +1,6 @@
date | value date | value
1971-01-02 | 5
1971-31-02 | 5
2011-01-03 | 3 2011-01-03 | 3
2011-01-03 | 2 2011-01-03 | 2
2011-01-03 | 1 2011-01-03 | 1

View File

@@ -7,9 +7,9 @@ int main(int argc, char **argv)
std::cout << "Error: could not open file." << std::endl; std::cout << "Error: could not open file." << std::endl;
return 1; return 1;
} }
BitcoinExchange btc;
try try
{ {
BitcoinExchange btc;
btc.str(argv[1]); btc.str(argv[1]);
} }
catch (std::runtime_error &e) catch (std::runtime_error &e)