diff --git a/en.subject.pdf b/en.subject.pdf new file mode 100644 index 0000000..4226ebf Binary files /dev/null and b/en.subject.pdf differ diff --git a/ex00/BitcoinExchange.cpp b/ex00/BitcoinExchange.cpp index df535ac..0d2adae 100644 --- a/ex00/BitcoinExchange.cpp +++ b/ex00/BitcoinExchange.cpp @@ -16,3 +16,110 @@ BitcoinExchange &BitcoinExchange::operator=(const BitcoinExchange &other) this->_data = other._data; return (*this); } + +bool BitcoinExchange::isValidDate(const std::string &date) const +{ + if (date.length() != 10) + return (false); + if (date[4] != '-' || date[7] != '-') + return (false); + int year = std::atoi(date.substr(0, 4).c_str()); + int month = std::atoi(date.substr(5, 2).c_str()); + int day = std::atoi(date.substr(8, 2).c_str()); + if (month < 1 || month > 12 || day < 1) + return false; + if (month != 2 && day > (30 + (month + month / 8) % 2)) + return false; + if (month == 2) + { + if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) { + if (day > 29) + return false; + } else if (day > 28) + return false; + } + return true; +} + +void BitcoinExchange::loadDatabase(const std::string &filename) +{ + std::ifstream file(filename.c_str()); + + if (!file.is_open()) + return; + + std::string line; + float value = 0.0; + std::getline(file, line); //skip header + + while (std::getline(file, line)) + { + value = std::atof(line.substr(12).c_str()); + if (isValidDate(line.substr(0, 10)) && value >= 0) + _data[line.substr(0, 10)] = value; + } + file.close(); +} + +float BitcoinExchange::getRate(const std::string &date) const +{ + std::map::const_iterator it = _data.upper_bound(date); + if (it == _data.begin()) + return (-1); //not found + --it; + return (it->second); +} + +bool BitcoinExchange::is_valide_input(const std::string &line, float &value) +{ + if (line.length() < 14 || line.find('|') == std::string::npos) { + std::cerr << "Error: bad input => " << line << std::endl; + return false; + } + if (!isValidDate(line.substr(0, 10))) { + std::cerr << "Error: bad input => " << line << std::endl; + return false; + } + std::string valStr = line.substr(13); + char *end; + value = std::strtof(valStr.c_str(), &end); + if (*end != '\0') + { + std::cerr << "Error: bad input => " << line << std::endl; + return false; + } + if (value > 1000) { + std::cerr << "Error: too large a number." << std::endl; + return false; + } + if (value < 0) { + std::cerr << "Error: not a positive number." << std::endl; + return false; + } + return true; +} + +void BitcoinExchange::processInput(const std::string &filename) +{ + std::ifstream file(filename.c_str()); + + if (!file.is_open()) + return; + + float value = 0.0; + std::string line; + std::getline(file, line); // skip header + + while (std::getline(file, line)) { + if (!is_valide_input(line, value)) + continue; + std::string date = line.substr(0, 10); + float rate = getRate(date); + if (rate < 0) { + std::cerr << "Error: bad input => " << date << std::endl; + continue; + } + std::cout << date << " => " << value << " = " << value * rate << std::endl; + } + file.close(); +} diff --git a/ex00/BitcoinExchange.hpp b/ex00/BitcoinExchange.hpp index 24aff7f..075da5b 100644 --- a/ex00/BitcoinExchange.hpp +++ b/ex00/BitcoinExchange.hpp @@ -1,6 +1,9 @@ #ifndef BITCOINEXCHANGE_HPP #define BITCOINEXCHANGE_HPP +#include +#include +#include #include #include @@ -16,7 +19,7 @@ class BitcoinExchange void processInput(const std::string &filename); float getRate(const std::string &date) const; bool isValidDate(const std::string &date) const; - bool isValidValue(const std::string &value, float &out) const; + bool is_valide_input(const std::string &line, float &value); private: std::map _data; diff --git a/ex00/Main.cpp b/ex00/Main.cpp new file mode 100644 index 0000000..bf678b8 --- /dev/null +++ b/ex00/Main.cpp @@ -0,0 +1,15 @@ +#include "BitcoinExchange.hpp" +#include + +int main(int argc, char **argv) +{ + if (argc != 2) { + std::cerr << "Error: could not open file." << std::endl; + return (1); + } + BitcoinExchange btc; + btc.loadDatabase("data.csv"); + btc.processInput(argv[1]); + std::cout << btc.isValidDate("2000-02-29") << std::endl; + return (0); +} diff --git a/ex00/btc b/ex00/btc new file mode 100755 index 0000000..5d703c9 Binary files /dev/null and b/ex00/btc differ diff --git a/cpp_09/data.csv b/ex00/data.csv similarity index 100% rename from cpp_09/data.csv rename to ex00/data.csv diff --git a/ex00/input.txt b/ex00/input.txt new file mode 100644 index 0000000..1566218 --- /dev/null +++ b/ex00/input.txt @@ -0,0 +1,54 @@ +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 +2009-01-02 | 1 +2009-01-01 | 1 +2022-03-29 | 1000 +2022-03-29 | 1000.01 +2022-03-29 | 0 +2022-03-29 | -0.01 +2022-03-29 | 999.99 +2015-06-15 | 0.5 +2013-12-25 | 100 +2010-03-01 | 10 +abc | 1 +2011-01-03 | abc +2011-01-03 | +| 1 +2011-13-01 | 1 +2011-00-01 | 1 +2011-01-32 | 1 +2011-01-00 | 1 +2011-02-29 | 1 +2012-02-29 | 1 +2012-02-30 | 1 +2011-04-31 | 1 +2011-06-31 | 1 +2011-09-31 | 1 +2011-11-31 | 1 +2016-02-29 | 1 +2100-02-29 | 1 +2000-02-29 | 1 +1900-02-29 | 1 +2011-01-03 | -0 +2011-01-03 | 0.0 +2011-01-03 | 0.001 +2022-03-29 | 999 +hello world +2011-01-03|1 +2011/01/03 | 1 +01-01-2011 | 1 +2011-1-3 | 1 +2011-01-03 | 1.1.1 +2011-01-03 | 1e2 +2011-01-03 | +1 +2008-12-31 | 1 +2025-12-31 | 1 +2009-01-02 | 0.001 diff --git a/ex00/note.cpp b/ex00/note.cpp new file mode 100644 index 0000000..a8c3c7e --- /dev/null +++ b/ex00/note.cpp @@ -0,0 +1,3 @@ +int days = 30 + (month + month / 8) % 2; + +(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) diff --git a/ex00/obj/BitcoinExchange.o b/ex00/obj/BitcoinExchange.o new file mode 100644 index 0000000..9e3c508 Binary files /dev/null and b/ex00/obj/BitcoinExchange.o differ diff --git a/ex00/obj/Main.o b/ex00/obj/Main.o new file mode 100644 index 0000000..8de3843 Binary files /dev/null and b/ex00/obj/Main.o differ diff --git a/ex00/output.txt b/ex00/output.txt new file mode 100644 index 0000000..404aae1 --- /dev/null +++ b/ex00/output.txt @@ -0,0 +1,2 @@ +dsd +2000-02-29 | 1 diff --git a/test.c b/test.c new file mode 100644 index 0000000..e878e68 --- /dev/null +++ b/test.c @@ -0,0 +1,13 @@ +#include +#include + +int main(int ac, char **av) +{ + int month; + int days; + if (ac == 2) { + month = atoi(av[1]); + days = 30 + (month + month / 8) % 2; + printf("Nombre de jour : %d", days); + } +}