From f4bd73f10317f8ab1bba58a968f0b1a9221488c1 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 22 Mar 2025 15:53:20 +0100 Subject: [PATCH] adding the first of cpp06 --- cpp05/ex02/ShrubberyCreationForm.cpp | 2 +- cpp06/ex00/Makefile | 127 ++++++++++++++++++++++++ cpp06/ex00/includes/ScalarConverter.hpp | 25 +++++ cpp06/ex00/includes/color.hpp | 47 +++++++++ cpp06/ex00/main.cpp | 36 +++++++ cpp06/ex00/sources/ScalarConverter.cpp | 89 +++++++++++++++++ 6 files changed, 325 insertions(+), 1 deletion(-) create mode 100644 cpp06/ex00/Makefile create mode 100644 cpp06/ex00/includes/ScalarConverter.hpp create mode 100644 cpp06/ex00/includes/color.hpp create mode 100644 cpp06/ex00/main.cpp create mode 100644 cpp06/ex00/sources/ScalarConverter.cpp diff --git a/cpp05/ex02/ShrubberyCreationForm.cpp b/cpp05/ex02/ShrubberyCreationForm.cpp index b2ba03c..7398c73 100644 --- a/cpp05/ex02/ShrubberyCreationForm.cpp +++ b/cpp05/ex02/ShrubberyCreationForm.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/18 18:05:02 by rparodi #+# #+# */ -/* Updated: 2025/03/19 20:25:27 by rparodi ### ########.fr */ +/* Updated: 2025/03/20 15:31:02 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/cpp06/ex00/Makefile b/cpp06/ex00/Makefile new file mode 100644 index 0000000..8bb8c29 --- /dev/null +++ b/cpp06/ex00/Makefile @@ -0,0 +1,127 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: rparodi +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # +# Updated: 2025/03/21 15:09:04 by rparodi ### ########.fr # +# # +# **************************************************************************** # + +# Variables + +# Name +NAME = intern + +# Commands +CXX = c++ +RM = rm -rf + +# Flags +# Mandatory flags for 42 +CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/ +# Flags to have the dependences (can be removed for correction) +CXXFLAGS += -MMD +# Flags to have the debug (can be removed for correction) +DEBUG = -g3 +DEBUG += -fsanitize=address +CXXFLAGS += $(DEBUG) + +# Sources +SRC = sources/ScalarConverter.cpp \ + main.cpp + +# Objects +OBJDIRNAME = ./build +OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o)) + +# Colors +GREEN = \033[32m +GREY = \033[0;90m +RED = \033[0;31m +GOLD = \033[38;5;220m +END = \033[0m + +# Rules + +# All (make all) +all: header $(NAME) footer + +# Bonus (make bonus) +bonus: header $(OBJ) footer + @mkdir -p $(OBJDIRNAME) + @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' + @$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ) + +# Clean (make clean) +clean: + @printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n' + @printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n' + @$(RM) $(OBJDIRNAME) + +# Clean (make fclean) +fclean: clean + @printf '$(GREY) Removing $(END)$(RED)Program$(END)\n' + @$(RM) $(NAME) + @echo "" + +# Restart (make re) +re: header fclean all + +# Dependences for all +$(NAME): $(OBJ) + @mkdir -p $(OBJDIRNAME) + @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' + @$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ) + +# Creating the objects +$(OBJDIRNAME)/%.o: %.cpp + @mkdir -p $(dir $@) + @printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n' + @$(CXX) $(CXXFLAGS) -o $@ -c $< + +# Header +header: + @clear + @printf '\n\n' + @printf '$(GOLD) ******* ****** ******* $(END)\n' + @printf '$(GOLD) ****** *** ******* $(END)\n' + @printf '$(GOLD) ******* * ******* $(END)\n' + @printf '$(GOLD) ****** ******* $(END)\n' + @printf '$(GOLD) ******* ******* $(END)\n' + @printf '$(GOLD) ******************* ******* * $(END)\n' + @printf '$(GOLD) ******************* ******* *** $(END)\n' + @printf '$(GOLD) ****** ******* ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GREY) Made by rparodi$(END)\n\n' + +# Footer +footer: + @printf "\n" + @printf "$(GOLD) ,_ _,$(END)\n" + @printf "$(GOLD) | \\___//|$(END)\n" + @printf "$(GOLD) |=6 6=|$(END)\n" + @printf "$(GOLD) \\=._Y_.=/$(END)\n" + @printf "$(GOLD) ) \` ( ,$(END)\n" + @printf "$(GOLD) / \\ (('$(END)\n" + @printf "$(GOLD) | | ))$(END)\n" + @printf "$(GOLD) /| | | |\\_//$(END)\n" + @printf "$(GOLD) \\| |._.| |/-\`$(END)\n" + @printf "$(GOLD) '\"' '\"'$(END)\n" + @printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n' + +clangd: + @echo \ + "CompileFlags:\n" \ + " Add:\n" \ + " - \"-std=c++98 -Wall -Wextra -Werror\"\n" \ + " - \"-I"$(shell pwd)"/includes\"\n" \ + " - \"-xc++\"\n" \ + > .clangd + +# Phony +.PHONY: all bonus clean fclean re clangd +-include ${OBJ:.o=.d} diff --git a/cpp06/ex00/includes/ScalarConverter.hpp b/cpp06/ex00/includes/ScalarConverter.hpp new file mode 100644 index 0000000..ec63396 --- /dev/null +++ b/cpp06/ex00/includes/ScalarConverter.hpp @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScalarConverter.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/20 15:50:13 by rparodi #+# #+# */ +/* Updated: 2025/03/21 15:05:26 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +class ScalarConverter { + public: + ScalarConverter(); + ~ScalarConverter(); + ScalarConverter(ScalarConverter const ©); + ScalarConverter& operator = (ScalarConverter const &assign); + static void convert(std::string *input); + private: +}; diff --git a/cpp06/ex00/includes/color.hpp b/cpp06/ex00/includes/color.hpp new file mode 100644 index 0000000..e1c3f79 --- /dev/null +++ b/cpp06/ex00/includes/color.hpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* color.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 15:27:54 by rparodi #+# #+# */ +/* Updated: 2025/02/18 16:43:18 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COLOR_HPP + #define COLOR_HPP + + #define RESET "\033[0m" + + #define BLACK "\033[0;30m" + #define RED "\033[0;31m" + #define GREEN "\033[0;32m" + #define YELLOW "\033[0;33m" + #define BLUE "\033[0;34m" + #define MAGENTA "\033[0;35m" + #define CYAN "\033[0;36m" + #define WHITE "\033[0;37m" + #define GOLD "\033[38;5;220m" + #define GREY "\033[38;5;240m" + + #define L_BLACK "\033[0;90m" + #define L_RED "\033[0;91m" + #define L_GREEN "\033[0;92m" + #define L_YELLOW "\033[0;93m" + #define L_BLUE "\033[0;94m" + #define L_MAGENTA "\033[0;95m" + #define L_CYAN "\033[0;96m" + #define L_WHITE "\033[0;97m" + + #define B_BLACK "\033[1;30m" + #define B_RED "\033[1;31m" + #define B_GREEN "\033[1;32m" + #define B_YELLOW "\033[1;33m" + #define B_BLUE "\033[1;34m" + #define B_MAGENTA "\033[1;35m" + #define B_CYAN "\033[1;36m" + #define B_WHITE "\033[1;37m" + +#endif diff --git a/cpp06/ex00/main.cpp b/cpp06/ex00/main.cpp new file mode 100644 index 0000000..2df9ca7 --- /dev/null +++ b/cpp06/ex00/main.cpp @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/20 15:49:02 by rparodi #+# #+# */ +/* Updated: 2025/03/21 18:26:21 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScalarConverter.hpp" + +int main(void) { + ScalarConverter test; + test.convert(new std::string("a")); + std::cout << std::endl; + test.convert(new std::string("0")); + std::cout << std::endl; + test.convert(new std::string("4294967300")); + std::cout << std::endl; + test.convert(new std::string("42.42")); + std::cout << std::endl; + test.convert(new std::string("-inff")); + std::cout << std::endl; + test.convert(new std::string("+inff")); + std::cout << std::endl; + test.convert(new std::string("nanf")); + std::cout << std::endl; + test.convert(new std::string("-inf")); + std::cout << std::endl; + test.convert(new std::string("+inf")); + std::cout << std::endl; + test.convert(new std::string("nan")); +} diff --git a/cpp06/ex00/sources/ScalarConverter.cpp b/cpp06/ex00/sources/ScalarConverter.cpp new file mode 100644 index 0000000..253db2d --- /dev/null +++ b/cpp06/ex00/sources/ScalarConverter.cpp @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScalarConverter.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/20 16:41:16 by rparodi #+# #+# */ +/* Updated: 2025/03/21 15:49:00 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScalarConverter.hpp" +#include +#include +#include +#include + +ScalarConverter::ScalarConverter() { + std::cout << "[ScalarConverter] Default constructor called" << std::endl; +} + +ScalarConverter::ScalarConverter(ScalarConverter const ©) { + std::cout << "[ScalarConverter] Copy constructor called" << std::endl; + *this = copy; +} + +ScalarConverter::~ScalarConverter() { + std::cout << "[ScalarConverter] Destructor called" << std::endl; +} + +ScalarConverter& ScalarConverter::operator = (ScalarConverter const &assign) { + std::cout << "[ScalarConverter] Assignation operator called" << std::endl; + (void)assign; + return *this; +} + +bool isChar(std::string *input) { + if (input->length() == 1 && std::isprint(input->at(0))) + return true; + if (input->length() == 1 && std::isprint(input->at(0))) { + std::cerr << "Please note that non-displayable characters shouldn’t be used as inputs." << std::endl; + return false; + } + return false; +} + +bool isInt(std::string *input) { + for (size_t i = 0; i < input->length(); i++) { + if (!std::isdigit(input->at(i))) { + return false; + } + } + std::ostringstream oss; + try { + oss << std::stoi(*input); + } catch (std::exception &e) { + return false; + } + return true; +} + +bool isFloat(std::string *input) { + std::ostringstream oss; + try { + oss << std::stof(*input); + } catch (std::exception &e) { + return false; + } + return true; +} + +bool isDouble(std::string *input) { + std::ostringstream oss; + try { + oss << std::stod(*input); + } catch (std::exception &e) { + return false; + } + return true; +} + +void ScalarConverter::convert(std::string *input) { + std::cout << "Input:\t\t" << *input << std::endl; + std::cout << "IsChar:\t\t" << (isChar(input) ? "✅ | true" : "❌ | false") << std::endl; + std::cout << "IsInt:\t\t" << (isInt(input) ? "✅ | true" : "❌ | false") << std::endl; + std::cout << "IsFloat:\t" << (isFloat(input) ? "✅ | true" : "❌ | false") << std::endl; + std::cout << "IsDouble:\t" << (isDouble(input) ? "✅ | true" : "❌ | false") << std::endl; +}