1
0

」 feat: finished ex00

This commit is contained in:
2025-06-03 11:41:31 +02:00
parent 003d8663d0
commit de9bd7ed8d
5 changed files with 166 additions and 18 deletions

View File

@ -6,14 +6,19 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/21 10:39:45 by adjoly #+# #+# */
/* Updated: 2025/05/30 13:29:18 by adjoly ### ########.fr */
/* Updated: 2025/06/03 11:40:22 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <BitcoinExchange.hpp>
#include <algorithm>
#include <cctype>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <fstream>
#include <functional>
#include <ostream>
#include <sstream>
#include <stdexcept>
@ -69,7 +74,7 @@ Date::Date(std::string date) {
}
}
}
throw std::runtime_error("Error: bad input => " + date);
throw std::runtime_error("bad input => " + date);
}
Date::Date(const Date &cpy)
@ -126,7 +131,18 @@ bool Date::operator>=(const Date &other) const {
}
std::ostream &operator<<(std::ostream &os, const Date &date) {
os << date.getYear() << "-" << date.getMonth() << "-" << date.getDay();
std::stringstream month;
std::stringstream day;
if (date.getMonth() < 10)
month << "0" << date.getMonth();
else
month << date.getMonth();
if (date.getDay() < 10)
day << "0" << date.getDay();
else
day << date.getDay();
os << date.getYear() << "-" << month.str() << "-" << day.str();
return os;
}
@ -161,9 +177,11 @@ CsvParser::CsvParser(void) : _is("data.csv") {
}
}
CsvParser::CsvParser(const CsvParser &cpy) {
CsvParser::CsvParser(const CsvParser &cpy) : _is(cpy._filename.c_str()) {
_log("", "CsvParser", "", "copy constructor called");
if (&cpy != this) {
_filename = cpy._filename;
_csv = cpy._csv;
}
}
@ -172,8 +190,116 @@ CsvParser::~CsvParser(void) { _log("", "CsvParser", "", "destructor called");
CsvParser &CsvParser::operator=(const CsvParser &cpy) {
_log("", "CsvParser", "", "copy assignement constructor called");
if (&cpy != this) {
_csv = cpy._csv;
_filename = cpy._filename;
}
return *this;
}
std::map<Date, float> CsvParser::getCsv(void) const { return _csv; }
BitcoinExchange::BitcoinExchange(void)
: _is("input.txt"), _filename("input.txt") {
_log("", "BitcoinExchange", "", "default constructor called");
if (!_is.is_open())
throw std::runtime_error("could not open file - " + _filename);
}
BitcoinExchange::BitcoinExchange(char *av) : _is(av), _filename(av) {
_log("", "BitcoinExchange", "", "file name constructor called");
try {
_csv = new CsvParser();
} catch (std::exception &e) {
throw e;
}
if (!_is.is_open())
throw std::runtime_error("could not open file - " + _filename);
std::string buf;
std::getline(_is, buf);
if (buf != "date | value")
throw std::runtime_error("file has not the good format - " + buf);
}
BitcoinExchange::BitcoinExchange(const BitcoinExchange &) {
_log("", "BitcoinExchange", "", "copy constructor called");
}
BitcoinExchange &BitcoinExchange::operator=(const BitcoinExchange &) {
_log("", "BitcoinExchange", "", "copy assignement constructor called");
return *this;
}
BitcoinExchange::~BitcoinExchange(void) {
_log("", "BitcoinExchange", "", "destructor called");
delete _csv;
}
void BitcoinExchange::print(void) {
std::pair<Date, float> pair;
std::getline(_is, _line);
while (!_line.empty()) {
try {
pair = _parseLine();
} catch (std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
std::getline(_is, _line);
continue;
}
_printPair(pair);
std::getline(_is, _line);
}
}
std::pair<Date, float> BitcoinExchange::_parseLine(void) {
std::pair<Date, float> pair;
std::string buf;
size_t i;
for (i = 0;
i < _line.length() && (std::isdigit(_line[i]) || _line[i] == '-');
i++) {
buf.push_back(_line[i]);
}
try {
pair.first = Date(buf);
} catch (std::exception &e) {
throw std::runtime_error("bad input => " + _line);
}
if (_line[i] != ' ' || _line[i + 1] != '|' || _line[i + 2] != ' ') {
throw std::runtime_error("bad input => " + _line);
}
if (_line.substr(i + 3) == "0") {
pair.second = 0;
} else {
float f = std::atof(_line.substr(i + 3).c_str());
if (f == 0) {
throw std::runtime_error("bad input => " + _line);
} else if (f > 1000)
throw std::runtime_error("too large number.");
else if (f < 0)
throw std::runtime_error("not a positive number.");
pair.second = f;
}
return pair;
}
void BitcoinExchange::_printPair(std::pair<Date, float> pair) {
auto map = _csv->getCsv();
auto it = std::find_if(map.begin(), map.end(), CompareDate(pair.first));
if (it == map.end())
throw std::out_of_range("out of range");
float i = it->second * pair.second;
std::cout << pair.first << " => " << pair.second << " = " << i << std::endl;
}
CompareDate::CompareDate(Date date) : _date(date) {}
bool CompareDate::operator()(const std::pair<Date, float> &pair) const {
return pair.first >= _date;
}