mirror of
https://github.com/Ladebeze66/cpp-partie-2.git
synced 2025-12-15 21:56:55 +01:00
ex00
This commit is contained in:
parent
57e94d216a
commit
f6d7defd9c
61
cpp06/ex00/Makefile
Normal file
61
cpp06/ex00/Makefile
Normal 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/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
|
||||
108
cpp06/ex00/ScalarConverter.cpp
Normal file
108
cpp06/ex00/ScalarConverter.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ScalarConverter.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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<char>(num)))
|
||||
throw std::runtime_error("Non displayable");
|
||||
return (static_cast<char>(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<int>(num));
|
||||
}
|
||||
|
||||
float ScalarConverter::convertToFloat(double num)
|
||||
{
|
||||
return (static_cast<float>(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;
|
||||
}
|
||||
56
cpp06/ex00/ScalarConverter.hpp
Normal file
56
cpp06/ex00/ScalarConverter.hpp
Normal file
@ -0,0 +1,56 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ScalarConverter.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <iostream>
|
||||
#include <string>
|
||||
#include <exception>
|
||||
#include <cstdlib>
|
||||
#include <climits>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
|
||||
#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
|
||||
30
cpp06/ex00/main.cpp
Normal file
30
cpp06/ex00/main.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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] << " <scalar value 1> [<scalar value 2> ...]" << 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);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user