」 feat(ex00): bitcoin exchange should work :D

This commit is contained in:
y-syo
2025-08-27 07:50:49 +02:00
parent 7ca9f83c32
commit b9919ed1e6
5 changed files with 119 additions and 15 deletions

View File

@@ -4,13 +4,14 @@ Date::Date(void)
{
}
Date::Date(const 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)
_value = 0;
for (std::string::iterator it = str.begin(); it < str.end(); ++it)
{
if (*it != '-')
if (!isdigit(*it))
continue ;
_value *= 10;
_value += *it - '0';
@@ -62,26 +63,95 @@ bool Date::operator>=(const Date &other) const
return this->_value >= other._value;
}
// --------------------------------------------------- //
void parseData(const std::string filename)
std::ostream &operator<<(std::ostream &os, const Date &date)
{
(void) filename;
os << date.getValue();
return os;
}
void str(std::string input)
// --------------------------------------------------- //
void BitcoinExchange::parseData(const std::string filename)
{
input = "2024-10-04 | 135.00\n2014-04-14 | 24.00\n2001-09-11 | 135.00"
std::stringstream input_ss(input);
std::ifstream input_file(filename.c_str());
if (input_file.fail())
throw std::runtime_error("Error: could not open file.");
std::string line;
(void) std::getline(input_file, line); // ignore first line
while (std::getline(input_file, line))
{
std::stringstream ss(line);
std::string date_str;
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)));
}
/*for (std::map<Date, double>::iterator it = this->_data.begin(); it != this->_data.end(); it++)
{
std::cout << it->first // string (key)
<< ':'
<< it->second // string's value
<< std::endl;
}*/
}
std::string trim(const std::string &s)
{
std::string::size_type start = 0;
while (start < s.size() && std::isspace(static_cast<unsigned char>(s[start])))
++start;
std::string::size_type end = s.size();
while (end > start && std::isspace(static_cast<unsigned char>(s[end - 1])))
--end;
return s.substr(start, end - start);
}
void BitcoinExchange::str(std::string filename)
{
//std::stringstream input_ss(input);
std::ifstream input_ss(filename.c_str());
if (input_ss.fail())
throw std::runtime_error("Error: could not open file.");
std::string line;
(void) std::getline(input_ss, line); // ignore first line
while (std::getline(input_ss, line))
{
std::stringstream ss(input);
std::stringstream ss(line);
std::string date_str;
std::string value_str;
if (!std::getline(ss, date_str, '|') || !std::getline(ss, value_str, '|'))
throw ; //add exception ig
this->_data.insert(std::pair{Date(date_str), std::atoi(value_str.c_str())});
{
std::cout << "Error: bad input => " << line << std::endl;
continue ;
}
if (std::atol(value_str.c_str()) > 2147483647)
{
std::cout << "Error: too large a number." << std::endl;
continue ;
}
if (std::strtod(value_str.c_str(), NULL) < 0)
{
std::cout << "Error: not a positive number." << std::endl;
continue ;
}
Date date(date_str);
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;
}
std::cout << date_str << " => " << value_str << " = " << it->second * std::strtod(value_str.c_str(), NULL) << std::endl;
}
}