ex00 and ex01
This commit is contained in:
43
ex00/Makefile
Normal file
43
ex00/Makefile
Normal file
@@ -0,0 +1,43 @@
|
||||
NAME = scalar
|
||||
|
||||
SRC = main.cpp \
|
||||
ScalarConverter/ScalarConverter.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
|
||||
201
ex00/ScalarConverter/ScalarConverter.cpp
Normal file
201
ex00/ScalarConverter/ScalarConverter.cpp
Normal file
@@ -0,0 +1,201 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ScalarConverter.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/28 20:03:23 by aortigos #+# #+# */
|
||||
/* Updated: 2026/02/28 20:03:23 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ScalarConverter.hpp"
|
||||
|
||||
//////////////////
|
||||
// Constructors //
|
||||
//////////////////
|
||||
|
||||
ScalarConverter::ScalarConverter()
|
||||
{
|
||||
// std::cout << "ScalarConverter default constructor called" << std::endl;
|
||||
}
|
||||
|
||||
ScalarConverter::ScalarConverter(const ScalarConverter &other)
|
||||
{
|
||||
*this = other;
|
||||
// std::cout << "ScalarConverter copy constructor called" << std::endl;
|
||||
}
|
||||
|
||||
ScalarConverter& ScalarConverter::operator=(const ScalarConverter &other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
// Copy attributes here
|
||||
}
|
||||
// std::cout << "ScalarConverter copy assignment operator called" << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
ScalarConverter::~ScalarConverter()
|
||||
{
|
||||
// std::cout << "ScalarConverter destructor called" << std::endl;
|
||||
}
|
||||
|
||||
// Main function
|
||||
void ScalarConverter::convert(const std::string &literal)
|
||||
{
|
||||
if (isSpecialCase(literal))
|
||||
convertFromSpecialCase(literal);
|
||||
else if (isChar(literal))
|
||||
convertFromChar(literal);
|
||||
else if(isFloat(literal))
|
||||
convertFromFloat(literal);
|
||||
else if(isDouble(literal))
|
||||
convertFromDouble(literal);
|
||||
else if (isInt(literal))
|
||||
convertFromInt(literal);
|
||||
else
|
||||
std::cout << "Error: invalid literal" << std::endl;
|
||||
}
|
||||
|
||||
// Checkers
|
||||
|
||||
bool ScalarConverter::isSpecialCase(const std::string &literal)
|
||||
{
|
||||
if (literal == "-inf" || literal == "+inf" || literal == "nan"
|
||||
|| literal == "-inff" || literal == "+inff" || literal == "nanf")
|
||||
return (true);
|
||||
return (false);
|
||||
}
|
||||
|
||||
bool ScalarConverter::isChar(const std::string &literal)
|
||||
{
|
||||
char c;
|
||||
|
||||
if (literal.length() != 3)
|
||||
return (false);
|
||||
c = literal[1];
|
||||
if (literal[0] == '\'' && literal[2] == '\''
|
||||
&& (c >= 32 && c < 127))
|
||||
{
|
||||
return (true);
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
bool ScalarConverter::isInt(const std::string &literal)
|
||||
{
|
||||
char *end;
|
||||
long nb;
|
||||
|
||||
nb = strtol(literal.c_str(), &end, 10);
|
||||
if (*end != '\0')
|
||||
return (false);
|
||||
if (nb > INT_MAX || nb < INT_MIN)
|
||||
return (false);
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool ScalarConverter::isDouble(const std::string &literal)
|
||||
{
|
||||
char *end;
|
||||
|
||||
if (literal.find('.') == std::string::npos)
|
||||
return (false);
|
||||
strtod(literal.c_str(), &end);
|
||||
if (*end != '\0')
|
||||
return (false);
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool ScalarConverter::isFloat(const std::string &literal)
|
||||
{
|
||||
if (literal[literal.length() - 1] != 'f')
|
||||
return (false);
|
||||
|
||||
std::string withoutF = literal.substr(0, literal.length() - 1);
|
||||
return isDouble(withoutF);
|
||||
}
|
||||
|
||||
// Converters
|
||||
|
||||
void ScalarConverter::convertFromSpecialCase(const std::string &literal)
|
||||
{
|
||||
std::cout << "char: impossible" << std::endl;
|
||||
std::cout << "int: impossible" << std::endl;
|
||||
if (literal == "nanf" || literal == "+inff" || literal == "-inff")
|
||||
{
|
||||
std::cout << "float: " << literal << std::endl;
|
||||
std::cout << "double: " << literal.substr(0, literal.length() - 1) << std::endl;
|
||||
} else {
|
||||
std::cout << "float: " << literal << "f" << std::endl;
|
||||
std::cout << "double: " << literal << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void ScalarConverter::convertFromChar(const std::string &literal)
|
||||
{
|
||||
char c;
|
||||
|
||||
c = literal[1];
|
||||
std::cout << "char: '" << c << "'" << std::endl;
|
||||
std::cout << "int: " << static_cast<int>(c) << std::endl;
|
||||
std::cout << std::fixed << std::setprecision(1);
|
||||
std::cout << "float: " << static_cast<float>(c) << "f" << std::endl;
|
||||
std::cout << "double: " << static_cast<double>(c) << std::endl;
|
||||
}
|
||||
|
||||
void ScalarConverter::convertFromInt(const std::string &literal)
|
||||
{
|
||||
int nb;
|
||||
char *end;
|
||||
|
||||
nb = static_cast<int>(std::strtol(literal.c_str(), &end, 10));
|
||||
if (nb < 0 || nb > 127)
|
||||
std::cout << "char: impossible" << std::endl;
|
||||
else if (nb < 32 || nb == 127)
|
||||
std::cout << "char: Non displayable" << std::endl;
|
||||
else
|
||||
std::cout << "char: '" << static_cast<char>(nb) << "'" << std::endl;
|
||||
std::cout << "int: " << nb << std::endl;
|
||||
std::cout << std::fixed << std::setprecision(1);
|
||||
std::cout << "float: " << static_cast<float>(nb) << "f" << std::endl;
|
||||
std::cout << "double: " << static_cast<double>(nb) << std::endl;
|
||||
}
|
||||
|
||||
void ScalarConverter::convertFromFloat(const std::string &literal)
|
||||
{
|
||||
float nb;
|
||||
char *end;
|
||||
|
||||
nb = std::strtof(literal.c_str(), &end);
|
||||
if (nb < 0 || nb > 127)
|
||||
std::cout << "char: impossible" << std::endl;
|
||||
else if (nb < 32 || nb == 127)
|
||||
std::cout << "char: Non displayable" << std::endl;
|
||||
else
|
||||
std::cout << "char: '" << static_cast<char>(nb) << "'" << std::endl;
|
||||
std::cout << "int: " << static_cast<int>(nb) << std::endl;
|
||||
std::cout << std::fixed << std::setprecision(1);
|
||||
std::cout << "float: " << nb << "f" << std::endl;
|
||||
std::cout << "double: " << static_cast<double>(nb) << std::endl;
|
||||
}
|
||||
|
||||
void ScalarConverter::convertFromDouble(const std::string &literal)
|
||||
{
|
||||
double nb;
|
||||
char *end;
|
||||
|
||||
nb = std::strtod(literal.c_str(), &end);
|
||||
if (nb < 0 || nb > 127)
|
||||
std::cout << "char: impossible" << std::endl;
|
||||
else if (nb < 32 || nb == 127)
|
||||
std::cout << "char: Non displayable" << std::endl;
|
||||
else
|
||||
std::cout << "char: '" << static_cast<char>(nb) << "'" << std::endl;
|
||||
std::cout << "int: " << static_cast<int>(nb) << std::endl;
|
||||
std::cout << std::fixed << std::setprecision(1);
|
||||
std::cout << "float: " << static_cast<float>(nb) << "f" << std::endl;
|
||||
std::cout << "double: " << nb << std::endl;
|
||||
}
|
||||
48
ex00/ScalarConverter/ScalarConverter.hpp
Normal file
48
ex00/ScalarConverter/ScalarConverter.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ScalarConverter.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/28 20:03:23 by aortigos #+# #+# */
|
||||
/* Updated: 2026/02/28 20:03:23 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SCALARCONVERTER_HPP
|
||||
|
||||
# define SCALARCONVERTER_HPP
|
||||
|
||||
# include <iostream> // STD::
|
||||
# include <climits> // INT_INT & INT_MAX
|
||||
# include <cstdlib> // strtol
|
||||
# include <iomanip>
|
||||
|
||||
class ScalarConverter
|
||||
{
|
||||
private:
|
||||
ScalarConverter();
|
||||
ScalarConverter(const ScalarConverter &other);
|
||||
ScalarConverter& operator=(const ScalarConverter &other);
|
||||
~ScalarConverter();
|
||||
public:
|
||||
// Main function
|
||||
static void convert(const std::string &literal);
|
||||
|
||||
// Checkers
|
||||
static bool isSpecialCase(const std::string &literal);
|
||||
static bool isChar(const std::string &literal);
|
||||
static bool isInt(const std::string &literal);
|
||||
static bool isFloat(const std::string &literal);
|
||||
static bool isDouble(const std::string &literal);
|
||||
|
||||
// Converters
|
||||
static void convertFromSpecialCase(const std::string &literal);
|
||||
static void convertFromChar(const std::string &literal);
|
||||
static void convertFromInt(const std::string &literal);
|
||||
static void convertFromFloat(const std::string &literal);
|
||||
static void convertFromDouble(const std::string &literal);
|
||||
};
|
||||
|
||||
#endif
|
||||
22
ex00/main.cpp
Normal file
22
ex00/main.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/28 19:56:55 by aortigos #+# #+# */
|
||||
/* Updated: 2026/02/28 20:10:35 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ScalarConverter/ScalarConverter.hpp"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc == 2)
|
||||
ScalarConverter::convert(argv[1]);
|
||||
else
|
||||
std::cout << "Error: no arguments." << std::endl;
|
||||
return (0);
|
||||
}
|
||||
43
ex01/Makefile
Normal file
43
ex01/Makefile
Normal file
@@ -0,0 +1,43 @@
|
||||
NAME = serialization
|
||||
|
||||
SRC = main.cpp \
|
||||
Serializer/Serializer.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
|
||||
53
ex01/Serializer/Serializer.cpp
Normal file
53
ex01/Serializer/Serializer.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Serializer.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/28 23:48:07 by aortigos #+# #+# */
|
||||
/* Updated: 2026/02/28 23:48:07 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Serializer.hpp"
|
||||
|
||||
//////////////////
|
||||
// Constructors //
|
||||
//////////////////
|
||||
|
||||
Serializer::Serializer()
|
||||
{
|
||||
// std::cout << "Serializer default constructor called" << std::endl;
|
||||
}
|
||||
|
||||
Serializer::Serializer(const Serializer &other)
|
||||
{
|
||||
*this = other;
|
||||
// std::cout << "Serializer copy constructor called" << std::endl;
|
||||
}
|
||||
|
||||
Serializer& Serializer::operator=(const Serializer &other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
// Copy attributes here
|
||||
}
|
||||
// std::cout << "Serializer copy assignment operator called" << std::endl;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Serializer::~Serializer()
|
||||
{
|
||||
// std::cout << "Serializer destructor called" << std::endl;
|
||||
}
|
||||
|
||||
uintptr_t Serializer::serialize(Data *ptr)
|
||||
{
|
||||
return (reinterpret_cast<uintptr_t>(ptr));
|
||||
}
|
||||
|
||||
Data *Serializer::deserialize(uintptr_t raw)
|
||||
{
|
||||
return (reinterpret_cast<Data*>(raw));
|
||||
}
|
||||
40
ex01/Serializer/Serializer.hpp
Normal file
40
ex01/Serializer/Serializer.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Serializer.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/28 23:48:07 by aortigos #+# #+# */
|
||||
/* Updated: 2026/02/28 23:48:07 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SERIALIZER_HPP
|
||||
|
||||
# define SERIALIZER_HPP
|
||||
|
||||
# include <iostream>
|
||||
# include <stdint.h>
|
||||
|
||||
struct Data
|
||||
{
|
||||
int age;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
class Serializer
|
||||
{
|
||||
private:
|
||||
Serializer();
|
||||
Serializer(const Serializer &other);
|
||||
Serializer& operator=(const Serializer &other);
|
||||
~Serializer();
|
||||
|
||||
public:
|
||||
static uintptr_t serialize(Data *ptr);
|
||||
static Data *deserialize(uintptr_t raw);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
34
ex01/main.cpp
Normal file
34
ex01/main.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/28 23:46:53 by aortigos #+# #+# */
|
||||
/* Updated: 2026/03/01 00:25:41 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Serializer/Serializer.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
Data *original = new Data();
|
||||
original->age = 42;
|
||||
original->name = "Hadi";
|
||||
|
||||
std::cout << "Puntero original: " << original << std::endl;
|
||||
|
||||
uintptr_t serialized = Serializer::serialize(original);
|
||||
std::cout << "Serializado (número): " << serialized << std::endl;
|
||||
|
||||
Data *recovered = Serializer::deserialize(serialized);
|
||||
std::cout << "Puntero recovered: " << recovered << std::endl;
|
||||
|
||||
std::cout << "Son iguales: " << (original == recovered) << std::endl;
|
||||
std::cout << "Age recovered: " << recovered->age << std::endl;
|
||||
std::cout << "Name recovered: " << recovered->name << std::endl;
|
||||
|
||||
delete original;
|
||||
}
|
||||
Reference in New Issue
Block a user