From b9919ed1e6ecf41b5932972edc1c07df341c3977 Mon Sep 17 00:00:00 2001 From: y-syo Date: Wed, 27 Aug 2025 07:50:49 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat(ex00):=20bit?= =?UTF-8?q?coin=20exchange=20should=20work=20:D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex00/BitcoinExchange.cpp | 94 ++++++++++++++++++++++++++++++++++----- ex00/BitcoinExchange.hpp | 12 ++++- ex00/input.txt | 10 +++++ ex00/main.cpp | 18 +++++++- ex00/obj/main.o | Bin 2040 -> 0 bytes 5 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 ex00/input.txt delete mode 100644 ex00/obj/main.o diff --git a/ex00/BitcoinExchange.cpp b/ex00/BitcoinExchange.cpp index 8c1aa2c..6829de0 100644 --- a/ex00/BitcoinExchange.cpp +++ b/ex00/BitcoinExchange.cpp @@ -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::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(s[start]))) + ++start; + + std::string::size_type end = s.size(); + while (end > start && std::isspace(static_cast(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::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; } + } diff --git a/ex00/BitcoinExchange.hpp b/ex00/BitcoinExchange.hpp index c0cb495..6ac2700 100644 --- a/ex00/BitcoinExchange.hpp +++ b/ex00/BitcoinExchange.hpp @@ -1,5 +1,7 @@ +#include #include #include +#include #include #include #include @@ -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 _data; }; + + diff --git a/ex00/input.txt b/ex00/input.txt new file mode 100644 index 0000000..3c0b3ec --- /dev/null +++ b/ex00/input.txt @@ -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 diff --git a/ex00/main.cpp b/ex00/main.cpp index f03e0f4..f298732 100644 --- a/ex00/main.cpp +++ b/ex00/main.cpp @@ -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; } diff --git a/ex00/obj/main.o b/ex00/obj/main.o deleted file mode 100644 index c8646bea9e35108a0f7672906043a52facc227ca..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2040 zcmbtUO=uHA6rLp2YHQn26a3UVi&Sb}pF(W&gz0?T27lb&j7bPCv7Kv6m zaq3v_kxgpoH1QZf4%5V7`dM;n;IKylWWskDJYfhE`>%EUcfb?+F|mIp^n>b#H$?V` zU+uW>B@s!H5_#9sbWQ9SPusjzRoUtwW4xJ)Y-Wgy(Mz zYFzWk-$#SoU)vo$d zxvp9ha%q0S&s7~ymXv2+oFmWG>~ymmKLy_K&6Y2WvQw*>wlAbCx0F;wR>@de@QXPq zs}42@d@Z?RIVEMurgCM#8YR!uWgwN|`O5Vhbs=YqHzp^FQhJS&oRc+GwT*JUE}#g* zc6`&AonIWK@bUv;SXQ$pRi#3=aYbh{SLJ?Z^sBryE ze