| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* BitcoinExchange.cpp :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: bchanot <bchanot@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2026/01/16 19:29:59 by bchanot #+# #+# */
- /* Updated: 2026/01/16 19:35:25 by bchanot ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "BitcoinExchange.hpp"
- #include <fstream>
- #include <climits>
- void ft_getcsv(std::map<Date, float> &Data)
- {
- std::string line;
- std::ifstream ifs;
- std::string token;
- float price;
- Date tmp;
- ifs.open("data.csv");
- if (!ifs) {
- std::cout << "Error: could not open data.csv file." << std::endl;
- throw std::exception();
- }
- std::getline(ifs, line);
- while (std::getline(ifs, line)) {
- token = line.substr(0, line.find(','));
- tmp.year = atoi(token.substr(0, 4).c_str());
- tmp.month = atoi(token.substr(5, 2).c_str());
- tmp.day = atoi(token.substr(8, 2).c_str());
- price = atof(line.substr(line.find(',') + 1, line.find('\n')).c_str());
- if (tmp.year < 0 || tmp.month < 0 || tmp.month > 12 || tmp.day < 0 || tmp.day > 31) {
- std::cout << "Error : Error in csv dates." << std::endl;
- throw std::exception();
- }
- if (price < 0 || price > INT_MAX) {
- std::cout << "Error : Error in csv price." << std::endl;
- throw std::exception();
- }
- Data[tmp] = price;
- }
- }
- void ft_process(char **av, std::map<Date, float> &Data) {
- std::ifstream ifs;
- std::string line;
- std::string token;
- std::map<Date, float>::iterator it;
- float amount;
- Date tmp;
- ifs.open(av[1]);
- if (!ifs) {
- std::cout << "Error: could not open " << av[1] << "." << std::endl;
- throw std::exception();
- }
- std::getline(ifs, line);
- while (std::getline(ifs, line)) {
- token = line.substr(0, line.find('|'));
- tmp.year = atoi(token.substr(0, 4).c_str());
- tmp.month = atoi(token.substr(5, 2).c_str());
- tmp.day = atoi(token.substr(8, 2).c_str());
- amount = atof(line.substr(line.find('|') + 1, line.find('\n')).c_str());
- if (tmp.year < 0 || tmp.month < 0 || tmp.month > 12 || tmp.day < 0 || tmp.day > 31) {
- std::cout << "Error : Bad date." << std::endl;
- continue ;
- }
- if (amount < 0 || amount > INT_MAX) {
- std::cout << "Error : Bad price." << std::endl;
- continue ;
- }
- it = Data.lower_bound(tmp);
- if (it->first != tmp) {
- if (it == Data.begin())
- std::cout << "Cannot find a price at the value " << tmp << "." << std::endl;
- else
- std::cout << tmp << " => " << amount << " = " << (--it)->second * amount << std::endl;
- } else
- std::cout << it->first << " => " << amount << " = " << it->second * amount << std::endl;
- }
- }
|