」 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;
}
}

View File

@@ -1,5 +1,7 @@
#include <exception>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <string>
#include <map>
@@ -21,10 +23,16 @@ public:
bool operator<=(const Date &) const;
bool operator>=(const Date &) const;
int getValue(void) const {return _value;}
private:
unsigned long long _value;
};
std::ostream &operator<<(std::ostream &, const Date &);
class BitcoinExchange {
public:
BitcoinExchange(void) {parseData("data.csv");}
@@ -32,9 +40,11 @@ public:
BitcoinExchange &operator=(const BitcoinExchange &other) {this->_data = other._data; return *this;}
~BitcoinExchange(void) {}
void parseData(const std::string filename);
void str(std::string input);
private:
void parseData(const std::string filename);
std::map<Date, double> _data;
};

10
ex00/input.txt Normal file
View File

@@ -0,0 +1,10 @@
date | value
2011-01-03 | 3
2011-01-03 | 2
2011-01-03 | 1
2011-01-03 | 1.2
2011-01-09 | 1
2012-01-11 | -1
2001-42-42
2012-01-11 | 1
2012-01-11 | 2147483648

View File

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

Binary file not shown.