diff --git a/cpp06/ex00/Makefile b/cpp06/ex00/Makefile new file mode 100644 index 0000000..1a92a5c --- /dev/null +++ b/cpp06/ex00/Makefile @@ -0,0 +1,61 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2024/02/25 19:53:36 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 = ScalarConverter + +SRC = ScalarConverter.cpp \ + main.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 "./ScalarConverter" 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 \ No newline at end of file diff --git a/cpp06/ex00/ScalarConverter.cpp b/cpp06/ex00/ScalarConverter.cpp new file mode 100644 index 0000000..98013ff --- /dev/null +++ b/cpp06/ex00/ScalarConverter.cpp @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScalarConverter.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/25 19:33:13 by fgras-ca #+# #+# */ +/* Updated: 2024/02/25 20:00:34 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScalarConverter.hpp" + +ScalarConverter::ScalarConverter() +{ + std::cout << GREEN << "ScalarConverter default constructor called!" << RESET << std::endl; +} + +ScalarConverter::ScalarConverter(const ScalarConverter&) +{ + std::cout << CYAN << "Copy constructor called for ScalarConverter." << RESET << std::endl; +} + +ScalarConverter& ScalarConverter::operator=(const ScalarConverter&) +{ + return (*this); +} + +ScalarConverter::~ScalarConverter() +{ + std::cout << RED << "ScalarConverter is destroyed!" << RESET << std::endl; +} + +char ScalarConverter::convertToChar(double num) +{ + if (num < CHAR_MIN || num > CHAR_MAX || std::isnan(num) || std::isinf(num)) + throw std::runtime_error("impossible"); + else if (!std::isprint(static_cast(num))) + throw std::runtime_error("Non displayable"); + return (static_cast(num)); +} + +int ScalarConverter::convertToInt(double num) +{ + if (num < INT_MIN || num > INT_MAX || std::isnan(num) || std::isinf(num)) + throw std::runtime_error("impossible"); + return (static_cast(num)); +} + +float ScalarConverter::convertToFloat(double num) +{ + return (static_cast(num)); +} + +double ScalarConverter::convertToDouble(const std::string& literal) +{ + return (std::strtod(literal.c_str(), NULL)); +} + +void ScalarConverter::convert(const std::string& literal) +{ + double num = convertToDouble(literal); + try + { + char c = convertToChar(num); + printAsChar(c); + } + catch (const std::exception& e) + { + std::cout << "char: " << e.what() << std::endl; + } + + try + { + int i = convertToInt(num); + printAsInt(i); + } + catch (const std::exception& e) + { + std::cout << "int: " << e.what() << std::endl; + } + + float f = convertToFloat(num); + printAsFloat(f); + + printAsDouble(num); +} + +void ScalarConverter::printAsChar(char c) +{ + std::cout << "char: '" << c << "'" << std::endl; +} + +void ScalarConverter::printAsInt(int i) +{ + std::cout << "int: " << i << std::endl; +} + +void ScalarConverter::printAsFloat(float f) +{ + std::cout << std::fixed << std::setprecision(1) << "float: " << f << 'f' << std::endl; +} + +void ScalarConverter::printAsDouble(double d) +{ + std::cout << std::fixed << std::setprecision(1) << "double: " << d << std::endl; +} \ No newline at end of file diff --git a/cpp06/ex00/ScalarConverter.hpp b/cpp06/ex00/ScalarConverter.hpp new file mode 100644 index 0000000..779f9a0 --- /dev/null +++ b/cpp06/ex00/ScalarConverter.hpp @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ScalarConverter.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/25 19:19:14 by fgras-ca #+# #+# */ +/* Updated: 2024/02/25 20:00:56 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SCALARCONVERTER_HPP +#define SCALARCONVERTER_HPP + +#include +#include +#include +#include +#include +#include +#include + +#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 ScalarConverter +{ +public: + static void convert(const std::string& literal); + ~ScalarConverter(); + +private: + ScalarConverter(); + ScalarConverter(const ScalarConverter&); + ScalarConverter& operator=(const ScalarConverter&); + + static char convertToChar(double num); + static int convertToInt(double num); + static float convertToFloat(double num); + static double convertToDouble(const std::string& literal); + static void printAsChar(char c); + static void printAsInt(int i); + static void printAsFloat(float f); + static void printAsDouble(double d); + +}; + +#endif \ No newline at end of file diff --git a/cpp06/ex00/main.cpp b/cpp06/ex00/main.cpp new file mode 100644 index 0000000..bc641c4 --- /dev/null +++ b/cpp06/ex00/main.cpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/25 19:50:26 by fgras-ca #+# #+# */ +/* Updated: 2024/02/25 19:52:52 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScalarConverter.hpp" + +int main(int argc, char** argv) +{ + if (argc < 2) + { + std::cerr << "Usage: " << argv[0] << " [ ...]" << std::endl; + return (1); + } + + for (int i = 1; i < argc; ++i) + { + std::cout << "Converting '" << argv[i] << "':" << std::endl; + ScalarConverter::convert(argv[i]); + std::cout << std::endl; // Ajoutez une ligne vide entre les conversions pour une meilleure lisibilité + } + return (0); +}