「🔨」 fix(ex00): added date parsing, ex00 is done pogit c :D
This commit is contained in:
@@ -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,8 +106,14 @@ 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.");
|
||||||
|
try {
|
||||||
this->_data.insert(std::make_pair(Date(date_str), std::strtod(value_str.c_str(), NULL)));
|
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++)
|
||||||
{
|
{
|
||||||
@@ -142,7 +166,13 @@ 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 ;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
Date date(date_str);
|
Date date(date_str);
|
||||||
|
if (date < _data.begin()->first)
|
||||||
|
{
|
||||||
|
std::cout << "Error: input date too low." << std::endl;
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
std::map<Date, double>::iterator it = _data.lower_bound(date);
|
std::map<Date, double>::iterator it = _data.lower_bound(date);
|
||||||
if (it == _data.end() || it->first > date)
|
if (it == _data.end() || it->first > date)
|
||||||
{
|
{
|
||||||
@@ -152,6 +182,13 @@ void BitcoinExchange::str(std::string filename)
|
|||||||
--it;
|
--it;
|
||||||
}
|
}
|
||||||
std::cout << date_str << " => " << value_str << " = " << it->second * std::strtod(value_str.c_str(), NULL) << std::endl;
|
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 ;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user