starting ex00

This commit is contained in:
aortigos
2026-04-23 21:05:36 +02:00
commit 7afb0335ef
8 changed files with 1807 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* BitcoinExchange.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/23 19:54:20 by aortigos #+# #+# */
/* Updated: 2026/04/23 19:54:20 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "BitcoinExchange.hpp"
//////////////////
// Constructors //
//////////////////
BitcoinExchange::BitcoinExchange()
{
// std::cout << "BitcoinExchange default constructor called" << std::endl;
}
BitcoinExchange::BitcoinExchange(const BitcoinExchange &other)
{
*this = other;
// std::cout << "BitcoinExchange copy constructor called" << std::endl;
}
BitcoinExchange& BitcoinExchange::operator=(const BitcoinExchange &other)
{
if (this != &other)
{
this->data = other.data;
}
// std::cout << "BitcoinExchange copy assignment operator called" << std::endl;
return (*this);
}
BitcoinExchange::~BitcoinExchange()
{
// std::cout << "BitcoinExchange destructor called" << std::endl;
}
void BitcoinExchange::readDatabase(std::ifstream &db)
{
std::string line;
std::getline(db, line);
if (line != "date,exchange_rate")
throw std::exception();
size_t pos;
std::string date;
std::string price;
while (std::getline(db, line))
{
pos = line.find(',');
if (pos == std::string::npos)
continue ;
date = line.substr(0, pos);
price = line.substr(pos + 1);
this->data[date] = std::strtod(price.c_str(), NULL);
}
}

View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* BitcoinExchange.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/23 19:54:20 by aortigos #+# #+# */
/* Updated: 2026/04/23 19:54:20 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BITCOINEXCHANGE_HPP
# define BITCOINEXCHANGE_HPP
# include <iostream>
# include <fstream>
# include <map>
# include <cstdlib>
using std::cout;
using std::endl;
class BitcoinExchange
{
private:
std::map<std::string, double> data;
public:
BitcoinExchange();
BitcoinExchange(const BitcoinExchange &other);
BitcoinExchange& operator=(const BitcoinExchange &other);
~BitcoinExchange();
void readDatabase(std::ifstream &db);
};
#endif

Binary file not shown.

43
ex00/Makefile Normal file
View File

@@ -0,0 +1,43 @@
NAME = btc
SRC = main.cpp BitcoinExchange/BitcoinExchange.cpp
OBJ = $(SRC:.cpp=.o)
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
GREEN = \033[0;32m
RED = \033[0;31m
RESET = \033[0m
TOTAL := $(words $(SRC))
COUNT = 0
all: $(NAME)
$(NAME): $(OBJ)
@rm -f .build_start
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
@printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)"
%.o: %.cpp
@if [ ! -f .build_start ]; then printf "$(GREEN)Compilando archivos...\n$(RESET)"; touch .build_start; fi
@$(eval COUNT = $(shell echo $$(($(COUNT)+1))))
@PERCENT=$$(($(COUNT)*100/$(TOTAL))); \
BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \
SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \
printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL)
@$(CC) $(CFLAGS) -c $< -o $@
clean:
@printf "$(RED)Eliminando...\n$(RESET)"
@rm -f $(OBJ)
fclean: clean
@rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

BIN
ex00/btc Executable file

Binary file not shown.

1613
ex00/data.csv Normal file

File diff suppressed because it is too large Load Diff

43
ex00/main.cpp Normal file
View File

@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/23 19:58:34 by aortigos #+# #+# */
/* Updated: 2026/04/23 20:48:10 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "BitcoinExchange/BitcoinExchange.hpp"
int main(int argc, char **argv)
{
if (argc != 2)
{
cout << "Usage: ./btc <database>" << endl;
return (0);
}
std::ifstream inFile(argv[1]);
if (!inFile)
{
cout << "Error: could not open database." << endl;
return (1);
}
BitcoinExchange bc;
try {
bc.readDatabase(inFile);
} catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
return (0);
}

BIN
ex00/main.o Normal file

Binary file not shown.