「✨」 feat: finished ex00
This commit is contained in:
@ -6,14 +6,19 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/21 10:39:45 by adjoly #+# #+# */
|
/* 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 <BitcoinExchange.hpp>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <exception>
|
||||||
|
#include <fstream>
|
||||||
|
#include <functional>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#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)
|
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) {
|
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;
|
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");
|
_log("➕", "CsvParser", "", "copy constructor called");
|
||||||
if (&cpy != this) {
|
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) {
|
CsvParser &CsvParser::operator=(const CsvParser &cpy) {
|
||||||
_log("➕", "CsvParser", "", "copy assignement constructor called");
|
_log("➕", "CsvParser", "", "copy assignement constructor called");
|
||||||
if (&cpy != this) {
|
if (&cpy != this) {
|
||||||
|
_csv = cpy._csv;
|
||||||
|
_filename = cpy._filename;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<Date, float> CsvParser::getCsv(void) const { return _csv; }
|
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;
|
||||||
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/21 10:38:49 by adjoly #+# #+# */
|
/* Created: 2025/05/21 10:38:49 by adjoly #+# #+# */
|
||||||
/* Updated: 2025/05/30 20:31:26 by adjoly ### ########.fr */
|
/* Updated: 2025/06/03 11:22:28 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -16,6 +16,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#define range(x) \
|
#define range(x) \
|
||||||
x.begin(); \
|
x.begin(); \
|
||||||
@ -89,13 +90,31 @@ class CsvParser {
|
|||||||
class BitcoinExchange {
|
class BitcoinExchange {
|
||||||
public:
|
public:
|
||||||
BitcoinExchange(void);
|
BitcoinExchange(void);
|
||||||
|
BitcoinExchange(char *av);
|
||||||
BitcoinExchange(const BitcoinExchange &);
|
BitcoinExchange(const BitcoinExchange &);
|
||||||
~BitcoinExchange(void);
|
~BitcoinExchange(void);
|
||||||
|
|
||||||
BitcoinExchange &operator=(const BitcoinExchange &);
|
BitcoinExchange &operator=(const BitcoinExchange &);
|
||||||
|
|
||||||
|
void print(void);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
private:
|
private:
|
||||||
void _nextToken(void);
|
std::ifstream _is;
|
||||||
std::string _peekToken(void) const;
|
std::string _filename;
|
||||||
|
std::string _line;
|
||||||
|
CsvParser *_csv;
|
||||||
|
|
||||||
|
std::pair<Date, float> _parseLine(void);
|
||||||
|
void _printPair(std::pair<Date, float>);
|
||||||
|
};
|
||||||
|
|
||||||
|
class CompareDate {
|
||||||
|
public:
|
||||||
|
CompareDate(Date);
|
||||||
|
|
||||||
|
bool operator()(const std::pair<Date, float> &pair) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Date _date;
|
||||||
};
|
};
|
||||||
|
@ -6,23 +6,26 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/05/21 10:34:50 by adjoly #+# #+# */
|
/* Created: 2025/05/21 10:34:50 by adjoly #+# #+# */
|
||||||
/* Updated: 2025/05/30 20:30:28 by adjoly ### ########.fr */
|
/* Updated: 2025/06/03 11:31:50 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <BitcoinExchange.hpp>
|
#include <BitcoinExchange.hpp>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
int main(int ac, char **av) {
|
int main(int ac, char **av) {
|
||||||
(void)av;
|
(void)av;
|
||||||
if (ac > 1) {
|
if (ac > 1) {
|
||||||
CsvParser *parser;
|
BitcoinExchange *btc;
|
||||||
try {
|
try {
|
||||||
parser = new CsvParser();
|
btc = new BitcoinExchange(av[1]);
|
||||||
} catch (std::exception &e) {
|
btc->print();
|
||||||
std::cerr << "Error: " << e.what() << std::endl;
|
} catch (std::runtime_error &e) {
|
||||||
|
std::cout << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
|
delete btc;
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Error: could not open file." << std::endl;
|
std::cout << "Error: could not open file." << std::endl;
|
||||||
}
|
}
|
||||||
|
12
flake.lock
generated
12
flake.lock
generated
@ -20,11 +20,11 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1747744144,
|
"lastModified": 1748693115,
|
||||||
"narHash": "sha256-W7lqHp0qZiENCDwUZ5EX/lNhxjMdNapFnbErcbnP11Q=",
|
"narHash": "sha256-StSrWhklmDuXT93yc3GrTlb0cKSS0agTAxMGjLKAsY8=",
|
||||||
"owner": "nixos",
|
"owner": "nixos",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "2795c506fe8fb7b03c36ccb51f75b6df0ab2553f",
|
"rev": "910796cabe436259a29a72e8d3f5e180fc6dfacc",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@ -54,11 +54,11 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1745683407,
|
"lastModified": 1748259577,
|
||||||
"narHash": "sha256-KBxdhcU39pctJx1yVSxiWVRNazzBRtwch7NYjwstf5s=",
|
"narHash": "sha256-U2VwP0orjL10tyBhqVWOne+jySF7Chv2q8qkdtNU6jI=",
|
||||||
"owner": "y-syo",
|
"owner": "y-syo",
|
||||||
"repo": "pogit",
|
"repo": "pogit",
|
||||||
"rev": "16adbe5cc1f39314761ec53d348cb86b60c901af",
|
"rev": "a6789248e58aabfb94d49319646bbb06dfe2bfae",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
Reference in New Issue
Block a user