diff --git a/ex00/BitcoinExchange/BitcoinExchange.cpp b/ex00/BitcoinExchange/BitcoinExchange.cpp index cc0d884..6755c56 100644 --- a/ex00/BitcoinExchange/BitcoinExchange.cpp +++ b/ex00/BitcoinExchange/BitcoinExchange.cpp @@ -63,8 +63,116 @@ void BitcoinExchange::readDatabase(std::ifstream &db) date = line.substr(0, pos); price = line.substr(pos + 1); - this->data[date] = std::strtod(price.c_str(), NULL); } +} +void BitcoinExchange::execute(std::ifstream &db) +{ + std::string line; + std::getline(db, line); + + if (line != "date | value") + throw std::exception(); + + size_t pos; + std::string date; + std::string value; + + while (std::getline(db, line)) + { + pos = line.find('|'); + if (pos == std::string::npos) + { + cout << "Error: bad input => " << line << endl; + continue; + } + date = line.substr(0, pos - 1); + value = line.substr(pos + 2); + + if (!validateAll(date, value)) + continue ; + + std::map::iterator it = this->data.lower_bound(date); + if (it == this->data.begin() && it->first != date) + { + cout << "Error: bad input => " << date << endl; + continue; + } + if (it->first != date) + it--; + + cout << date << " => " << value << " = " << it->second * std::strtod(value.c_str(), NULL) << endl; + } +} + +bool BitcoinExchange::validateAll(const std::string &date, const std::string &value) +{ + + if (!validateDate(date)) + { + cout << "Error: bad input => " << date << endl; + return (false); + } + else if (validateValue(value) == 2) + { + cout << "Error: not a positive number." << endl; + return (false); + } + else if (validateValue(value) == 3) + { + cout << "Error: too large a number." << endl; + return (false); + } + return (true); +} + +bool BitcoinExchange::validateDate(const std::string &date) +{ + int maxDay = 31; + int year; + int month; + int day; + char dash; + bool bisiesto; + + + if (date.length() != 10 || date[4] != '-' || date[7] != '-') + return (false); + + std::istringstream ss(date); + ss >> year >> dash >> month >> dash >> day; + bisiesto = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); + + if (year < 2008) + return (false); + if (month < 1 || month > 12) + return (false); + if (month == 2) + { + if (bisiesto) + maxDay = 29; + else + maxDay = 28; + } + if (day < 1 || day > maxDay) + return (false); + + return (true); + +} + +int BitcoinExchange::validateValue(const std::string &value) +{ + char *endptr; + double val = std::strtod(value.c_str(), &endptr); + + if (*endptr != '\0') + return (1); + if (val < 0) + return (2); + else if (val > 1000) + return (3); + return (0); + } \ No newline at end of file diff --git a/ex00/BitcoinExchange/BitcoinExchange.hpp b/ex00/BitcoinExchange/BitcoinExchange.hpp index 0988260..65fd925 100644 --- a/ex00/BitcoinExchange/BitcoinExchange.hpp +++ b/ex00/BitcoinExchange/BitcoinExchange.hpp @@ -25,6 +25,10 @@ class BitcoinExchange { private: std::map data; + + bool validateAll(const std::string &date, const std::string &value); + bool validateDate(const std::string &date); + int validateValue(const std::string &value); public: BitcoinExchange(); @@ -33,6 +37,7 @@ class BitcoinExchange ~BitcoinExchange(); void readDatabase(std::ifstream &db); + void execute(std::ifstream &db); }; #endif diff --git a/ex00/BitcoinExchange/BitcoinExchange.o b/ex00/BitcoinExchange/BitcoinExchange.o deleted file mode 100644 index db721ec..0000000 Binary files a/ex00/BitcoinExchange/BitcoinExchange.o and /dev/null differ diff --git a/ex00/btc b/ex00/btc deleted file mode 100755 index 08e38be..0000000 Binary files a/ex00/btc and /dev/null differ diff --git a/ex00/input.txt b/ex00/input.txt new file mode 100644 index 0000000..f9b4147 --- /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 \ No newline at end of file diff --git a/ex00/main.cpp b/ex00/main.cpp index 0c24697..56a4918 100644 --- a/ex00/main.cpp +++ b/ex00/main.cpp @@ -6,7 +6,7 @@ /* By: aortigos