cpp09ex00

This commit is contained in:
Ladebeze66 2024-03-04 17:32:18 +01:00
parent 8be6db72f0
commit 281f93245e
7 changed files with 1941 additions and 10 deletions

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */
/* Updated: 2024/02/22 12:40:07 by fgras-ca ### ########.fr */
/* Updated: 2024/03/04 14:58:28 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,15 +17,15 @@
#include <exception>
#include <iostream>
#define RESET "\033[0m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
#define RESET "\033[0m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
class Bureaucrat
{

View File

@ -0,0 +1,126 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* BitcoinExchange.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/04 14:56:17 by fgras-ca #+# #+# */
/* Updated: 2024/03/04 16:52:42 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "BitcoinExchange.hpp"
BitcoinExchange::BitcoinExchange()
{
std::cout << GREEN << "BitcoinExchange default constructor called!" << RESET << std::endl;
}
BitcoinExchange::BitcoinExchange(const BitcoinExchange& other) : dataBase(other.dataBase)
{
std::cout << CYAN << "Copy constructor called for BitcoinExchange " << RESET << std::endl;
}
BitcoinExchange& BitcoinExchange::operator=(const BitcoinExchange& other)
{
std::cout << YELLOW << "Copy assignment BitcoinExchange operator called" << RESET << std::endl;
if (this != &other)
{
this->dataBase = other.dataBase;
}
return (*this);
}
BitcoinExchange::~BitcoinExchange()
{
std::cout << RED << "BitcoinExchange is destroyed!" << RESET << std::endl;
}
//charge les fichiers à partir d'un fichier CSV
bool BitcoinExchange::loadPriceData(const std::string& filename)
{
std::ifstream file(filename.c_str());
if (!file.is_open())
{
std::cerr << "Error: could not open price data file." << std::endl;
return false;
}
std::string line;
// Ignorer la première ligne (en-têtes)
std::getline(file, line);
std::string date;
float exchangeRate;
while (getline(file, line))
{
std::istringstream iss(line);
if (std::getline(iss, date, ',') && (iss >> exchangeRate))
{
dataBase[date] = exchangeRate;
}
else
{
std::cerr << "Warning: Malformed line skipped -> " << line << std::endl;
}
}
file.close();
// Optionnel: afficher les données chargées pour vérification
/*for (std::map<std::string, float>::const_iterator it = dataBase.begin(); it != dataBase.end(); ++it)
{
std::cout << "Date: " << it->first << ", Exchange Rate: " << it->second << std::endl;
}*/
return (true);
}
//Analyse une ligne d'entrée et extrait la date et la valeur
bool BitcoinExchange::parseLine(const std::string& line, std::string& date, float& value) const
{
std::istringstream iss(line);
std::string valueStr;
std::getline(iss, date, '|'); // Extrait la date jusqu'au délimiteur '|'.
// Supprime les espaces en début et en fin de 'date'.
date.erase(0, date.find_first_not_of(" \t"));
date.erase(date.find_last_not_of(" \t") + 1);
std::getline(iss, valueStr); // Extrait le reste de la ligne comme 'valueStr'.
valueStr.erase(0, valueStr.find_first_not_of(" \t")); // Supprime les espaces en début de 'valueStr'.
std::istringstream valueStream(valueStr); // Crée un istringstream basé sur 'valueStr' pour la conversion.
if (!(valueStream >> value) || valueStream.peek() != EOF)
{ // Vérifie la conversion et s'assure qu'il n'y a pas de caractères supplémentaires.
return (false);
}
return (true);
}
//Calcule la valeur d'échange du Bitcoin pour un montant donné à une date spécifique
float BitcoinExchange::getExchangeValue(const std::string& date, float value) const
{
std::map<std::string, float>::const_iterator it = dataBase.find(date);
if (it != dataBase.end())
{
// Si la date exacte est trouvée dans la base de données
return (it->second * value);
}
else
{
// Recherche de la date la plus proche inférieure
// std::map::lower_bound retourne un itérateur pointant vers la première clé qui n'est pas inférieure à 'date'
// On utilise donc std::prev pour reculer d'une position si l'itérateur n'est pas au début
std::map<std::string, float>::const_iterator closest = dataBase.lower_bound(date);
if (closest == dataBase.begin())
{
// Si 'closest' est au début, il n'y a pas de date antérieure disponible
return (-1.0); // Indique une erreur ou une date non trouvée
}
else
{
// Reculer d'une position pour trouver la date antérieure la plus proche
--closest;
return(closest->second * value);
}
}
}

View File

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* BitcoinExchange.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/04 14:16:51 by fgras-ca #+# #+# */
/* Updated: 2024/03/04 17:07:28 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BITCOINEXCHANGE_HPP
#define BITCOINEXCHANGE_HPP
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <cstdlib> // Pour utiliser exit correctement
#define RESET "\033[0m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
class BitcoinExchange
{
public:
BitcoinExchange();
BitcoinExchange(const BitcoinExchange& other);
BitcoinExchange& operator=(const BitcoinExchange& other);
~BitcoinExchange();
//charge les données de prix à partir d'un fichier CSV
bool loadPriceData(const std::string& filename);
//Analyse une ligne d'entrée et extrait la date et la valeur
bool parseLine(const std::string& line, std::string& date, float& value) const;
//Calcule la valeur d'échange du Bitcoin pour un montant donné à une date spécifique
float getExchangeValue(const std::string& date, float value) const;
private:
std::map<std::string, float> dataBase; //stocke les données de prix du Bitcoin, indexées par date
};
#endif

61
cpp09/ex00/Makefile Normal file
View File

@ -0,0 +1,61 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/03/04 14:11:41 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
LOGO = @echo "🅻🅰🅳🅴🅱🅴🆉🅴";\
DEF_COLOR = \033[0m
GRAY = \033[0;90m
RED = \033[0;91m
GREEN = \033[0;92m
YELLOW = \033[0;93m
BLUE = \033[0;94m
MAGENTA = \033[0;95m
CYAN = \033[0;96m
WHITE = \033[0;97m
ORANGE = \033[38;5;214m
NAME = btc
SRC = main.cpp \
BitcoinExchange.cpp \
CC = c++
CFLAGS = -Wall -Werror -Wextra -std=c++98
RM = rm -f
OBJS = ${SRC:.cpp=.o}
all : $(NAME)
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
$(NAME) : $(OBJS)
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./btc" to execute the program!!$(DEF_COLOR)"
$(LOGO)
clean :
@echo "$(RED)Deleating files objects... $(DEF_COLOR)"
$(RM) $(OBJS)
@echo "$(GREEN)files deleted!! $(DEF_COLOR)"
$(LOGO)
fclean : clean
@echo "$(RED)Delete program name... $(DEF_COLOR)"
$(RM) $(NAME)
@echo "$(GREEN)File program deleted!! $(DEF_COLOR)"
$(LOGO)
re : fclean all
.PHONY : all clean fclean re

1613
cpp09/ex00/data.csv Normal file

File diff suppressed because it is too large Load Diff

10
cpp09/ex00/input.txt Normal file
View File

@ -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

70
cpp09/ex00/main.cpp Normal file
View File

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/04 14:09:27 by fgras-ca #+# #+# */
/* Updated: 2024/03/04 17:31:07 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "BitcoinExchange.hpp"
int main(int argc, char **argv)
{
if (argc != 2)
{
std::cerr << RED << "Usage: ./btc <input_file>" << RESET << std::endl;
exit(1);
}
std::string inputFile = argv[1];
std::ifstream input(inputFile.c_str());
if (!input.is_open())
{
std::cerr << RED << "Error: could not open " << RESET << inputFile << std::endl;
exit(1);
}
BitcoinExchange exchange;
std::string priceDataFile = "data.csv";
if (!exchange.loadPriceData(priceDataFile))
{
std::cerr << RED << "Error loading price data from " << RESET << priceDataFile << std::endl;
exit(1);
}
std::string line;
while (getline(input, line))
{
std::string date;
float amount;
// Analyse la ligne pour extraire la date et la valeur
if (exchange.parseLine(line, date, amount))
{
// Vérifie que la valeur est dans la plage [0, 1000]
if (amount < 0 || amount > 1000)
{
std::cout << RED << "Error: Value out of range [0, 1000]." << RESET << std::endl;
continue; // Passe à la ligne suivante si la valeur est hors plage
}
// Calcule la valeur d'échange du Bitcoin pour la date et le montant donnés
float exchangeValue = exchange.getExchangeValue(date, amount);
if (exchangeValue >= 0)
{
std::cout << YELLOW << date << " => " << RESET << amount << " = " << GREEN << exchangeValue << RESET << std::endl;
}
else
{
std::cout << RED << "Error: Date " << RESET << date << " not found in the price database." << std::endl;
}
}
else
{
std::cerr << RED << "Error: bad input => " << RESET << line << std::endl;
}
}
return (0);
}