🎉」 init: hello world !

This commit is contained in:
2025-08-15 15:11:43 +02:00
commit c9ae5bee78
13 changed files with 559 additions and 0 deletions

45
ex00/Makefile Normal file
View File

@@ -0,0 +1,45 @@
SHELL = bash
NAME = ScalarConverter
CC = c++
OBJSDIR = obj/
SRCS = $(shell find . -name '*.cpp')
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K\r
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I . -c $< -o $@
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
clean:
@rm -f $(OBJS)
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
re: fclean
@$(MAKE) -s all
.PHONY: clean fclean all re

128
ex00/PrintValue.hpp Normal file
View File

@@ -0,0 +1,128 @@
#pragma once
#include "ScalarConverter.hpp"
void _printChar(const std::string &s)
{
std::cout << "char: ";
if (!std::isprint(s[0]))
std::cout << "Non displayable" << std::endl;
else
std::cout << "'" << static_cast<char>(s[0]) << "'" << std::endl;
std::cout << "int: " << static_cast<int>(s[0]) << std::endl;
std::cout << "float: " << std::fixed << static_cast<float>(s[0]) << "f" << std::endl;
std::cout << "double: " << std::fixed << static_cast<double>(s[0]) << std::endl;
}
void _printInt(const std::string &s)
{
long int nb = std::strtol(s.c_str(), NULL, 10);
std::cout << "char: ";
if (nb < std::numeric_limits<char>::min() ||
nb > std::numeric_limits<char>::max())
std::cout << "impossible" << std::endl;
else if (!std::isprint(nb))
std::cout << "Non displayable" << std::endl;
else
std::cout << "'" << static_cast<char>(nb) << "'" << std::endl;
std::cout << "int: ";
if (nb < std::numeric_limits<int>::min() ||
nb > std::numeric_limits<int>::max())
std::cout << "impossible" << std::endl;
else
std::cout << static_cast<int>(nb) << std::endl;
std::cout << "float: " << std::fixed << static_cast<float>(nb) << "f" << std::endl;
std::cout << "double: " << std::fixed << static_cast<double>(nb) << std::endl;
}
void _printFloat(const std::string &s)
{
long int nb = std::strtod(s.c_str(), NULL);
std::cout << "char: ";
if (nb < std::numeric_limits<char>::min() ||
nb > std::numeric_limits<char>::max())
std::cout << "impossible" << std::endl;
else if (!std::isprint(nb))
std::cout << "Non displayable" << std::endl;
else
std::cout << "'" << static_cast<char>(nb) << "'" << std::endl;
std::cout << "int: ";
if (nb < std::numeric_limits<int>::min() ||
nb > std::numeric_limits<int>::max())
std::cout << "impossible" << std::endl;
else
std::cout << static_cast<int>(nb) << std::endl;
std::cout << "float: ";
if (nb < std::numeric_limits<float>::min() ||
nb > std::numeric_limits<float>::max())
std::cout << "impossible" << std::endl;
else
std::cout << std::fixed << static_cast<float>(nb) << "f" << std::endl;
std::cout << "double: " << std::fixed << static_cast<double>(nb) << std::endl;
}
void _printDouble(const std::string &s)
{
long int nb = std::strtod(s.c_str(), NULL);
std::cout << "char: ";
if (nb < std::numeric_limits<char>::min() ||
nb > std::numeric_limits<char>::max())
std::cout << "impossible" << std::endl;
else if (!std::isprint(nb))
std::cout << "Non displayable" << std::endl;
else
std::cout << "'" << static_cast<char>(nb) << "'" << std::endl;
std::cout << "int: ";
if (nb < std::numeric_limits<int>::min() ||
nb > std::numeric_limits<int>::max())
std::cout << "impossible" << std::endl;
else
std::cout << static_cast<int>(nb) << std::endl;
std::cout << "float: ";
if (nb < std::numeric_limits<float>::min() ||
nb > std::numeric_limits<float>::max())
std::cout << "impossible" << std::endl;
else
std::cout << std::fixed << static_cast<float>(nb) << "f" << std::endl;
std::cout << "double: " << std::fixed << static_cast<double>(nb) << std::endl;
}
void _printNan(const std::string &s)
{
std::string input = s;
if (input == "inf" || input == "inff")
input = "+inf";
if (input == "nan" || input == "-inf" || input == "+inf")
{
std::cout << "char: impossible\n";
std::cout << "int: impossible\n";
std::cout << "float: " << input << "f" << std::endl;
std::cout << "double: " << input << std::endl;
return;
}
if (input == "nanf" || input == "-inff" || input == "+inff")
{
std::cout << "char: impossible\n";
std::cout << "int: impossible\n";
std::cout << "float: " << input << std::endl;
std::cout << "double: ";
if (input == "nanf")
std::cout << "nan";
else if (input == "-inff")
std::cout << "-inf";
else
std::cout << "+inf";
std::cout << std::endl;
}
}

71
ex00/ScalarConverter.cpp Normal file
View File

@@ -0,0 +1,71 @@
#include "ScalarConverter.hpp"
#include "PrintValue.hpp"
ScalarConverter::ScalarConverter(void) {
}
ScalarConverter::ScalarConverter(const ScalarConverter &cpy) {
if (this != &cpy) {
*this = cpy;
}
}
ScalarConverter::~ScalarConverter(void) {
}
ScalarConverter &ScalarConverter::operator=(const ScalarConverter &) {
return *this;
}
bool _isNan(const std::string &str) {
if (str == "nan" || str == "nanf" || str == "-inf" || str == "-inff" ||
str == "+inf" || str == "+inff" || str == "inf" || str == "inff")
return true;
return false;
}
e_type _getType(const std::string &s)
{
if (_isNan(s))
return NAN;
if (s.length() == 1 && !isdigit(s[0]))
return CHAR;
char *err;
(void)strtol(s.c_str(), &err, 10);
if (*err == 0)
return INT;
(void)strtod(s.c_str(), &err);
if (*err == 0)
return DOUBLE;
if (*err == 'f' && *(err + 1) == 0)
return FLOAT;
return NONE;
}
void ScalarConverter::convert(const std::string &s)
{
switch (_getType(s))
{
case CHAR:
_printChar(s);
break;
case INT:
_printInt(s);
break;
case FLOAT:
_printFloat(s);
break;
case DOUBLE:
_printDouble(s);
break;
case NAN:
_printNan(s);
break;
default:
std::cerr << "'" << s << "' does not convert to any of the availables types" << std::endl;
}
}

29
ex00/ScalarConverter.hpp Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include <string>
#include <cctype>
#include <iostream>
#include <limits>
#include <sstream>
#include <cstdlib>
enum e_type {
CHAR,
INT,
FLOAT,
DOUBLE,
NAN,
NONE
};
class ScalarConverter {
public:
static void convert(const std::string &);
private:
ScalarConverter(void);
ScalarConverter(const ScalarConverter &);
~ScalarConverter(void);
ScalarConverter &operator=(const ScalarConverter &);
};

10
ex00/main.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include "ScalarConverter.hpp"
#include <iostream>
int main(int ac, char **av) {
if (ac < 2) {
std::cout << "needs an input ! D:" << std::endl;
return 1;
}
ScalarConverter::convert(av[1]);
}