「✨」 feat: should have finished ex00
This commit is contained in:
41
ex00/GetType.cpp
Normal file
41
ex00/GetType.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* GetType.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/09 09:05:44 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/09 09:18:37 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "GetType.hpp"
|
||||
#include <cctype>
|
||||
#include <cstdlib>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
nbType GetType(const std::string &str) {
|
||||
if (_isNan(str))
|
||||
return NAN;
|
||||
if (str.length() == 1 && !isdigit(str[0]))
|
||||
return CHAR;
|
||||
|
||||
char *err;
|
||||
|
||||
(void)strtol(str.c_str(), &err, 10);
|
||||
if (*err == 0)
|
||||
return INT;
|
||||
(void)strtod(str.c_str(), &err);
|
||||
if (*err == 0)
|
||||
return DOUBLE;
|
||||
if (*err == 'f' && *(err + 1) == 0)
|
||||
return FLOAT;
|
||||
return NONE;
|
||||
}
|
28
ex00/GetType.hpp
Normal file
28
ex00/GetType.hpp
Normal file
@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* GetType.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/09 09:04:39 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/09 09:55:16 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
enum nbType {
|
||||
CHAR,
|
||||
INT,
|
||||
FLOAT,
|
||||
DOUBLE,
|
||||
NAN,
|
||||
NONE
|
||||
};
|
||||
|
||||
bool _isNan(const std::string &);
|
||||
|
||||
nbType GetType(const std::string &);
|
@ -6,13 +6,13 @@
|
||||
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
|
||||
# Updated: 2025/03/30 15:14:32 by adjoly ### ########.fr #
|
||||
# Updated: 2025/04/09 10:23:51 by adjoly ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SHELL = bash
|
||||
|
||||
NAME = Form
|
||||
NAME = ScalarConverter
|
||||
|
||||
CC = c++
|
||||
|
||||
|
@ -6,23 +6,54 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/08 14:30:07 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/08 14:30:36 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/09 10:32:48 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
|
||||
template <typename T> void _printFloat(T nb) {
|
||||
std::cout << "float: " << static_cast<float>(nb) << "f" << std::endl;
|
||||
}
|
||||
template <typename T> void _printInt(T nb) {
|
||||
std::cout << "int: " << static_cast<int>(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;
|
||||
}
|
||||
template <typename T> void _printChar(T nb) {
|
||||
std::cout << "char: '" << static_cast<char>(nb) << "'" << std::endl;
|
||||
std::cout << "char: ";
|
||||
if (nb < std::numeric_limits<char>::min() ||
|
||||
nb > std::numeric_limits<char>::max())
|
||||
std::cout << "impossible" << std::endl;
|
||||
else if (!isprint(nb))
|
||||
std::cout << "Non displayable" << std::endl;
|
||||
else
|
||||
std::cout << "'" << static_cast<char>(nb) << "'" << std::endl;
|
||||
}
|
||||
template <typename T> void _printDouble(T nb) {
|
||||
std::cout << "double: " << static_cast<double>(nb) << std::endl;
|
||||
}
|
||||
template <typename T> T _ConvertTo(const std::string &str) {
|
||||
std::stringstream ss(str);
|
||||
T ret;
|
||||
|
||||
ss >> ret;
|
||||
return ret;
|
||||
}
|
||||
template <typename T> void _Convert(const std::string &str) {
|
||||
T nb = _ConvertTo<T>(str);
|
||||
|
||||
_printChar(nb);
|
||||
_printInt(nb);
|
||||
_printFloat(nb);
|
||||
_printDouble(nb);
|
||||
}
|
||||
|
BIN
ex00/ScalarConverter
Executable file
BIN
ex00/ScalarConverter
Executable file
Binary file not shown.
@ -6,11 +6,14 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/03 19:59:17 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/08 14:17:44 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/09 10:31:41 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ScalarConverter.hpp"
|
||||
#include "GetType.hpp"
|
||||
#include "PrintNb.hpp"
|
||||
#include "StrTo.hpp"
|
||||
|
||||
void _log(std::string emoji, std::string what, std::string who,
|
||||
std::string str) {
|
||||
@ -40,8 +43,47 @@ ScalarConverter::~ScalarConverter(void) {
|
||||
_log("➖", "ScalarConverter", "", "destructor called");
|
||||
}
|
||||
|
||||
ScalarConverter &ScalarConverter::operator=(const ScalarConverter &) { return *this; }
|
||||
|
||||
void ScalarConverter::convert(std::string &s) {
|
||||
|
||||
ScalarConverter &ScalarConverter::operator=(const ScalarConverter &) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
void _ConvertNan(const std::string &str) {
|
||||
std::string nb;
|
||||
|
||||
if (str[0] == 'n')
|
||||
nb = "nan";
|
||||
else if (str[0] == '-')
|
||||
nb = "-inf";
|
||||
else
|
||||
nb = "+inf";
|
||||
|
||||
std::cout << "char: impossible" << std::endl;
|
||||
std::cout << "int: impossible" << std::endl;
|
||||
std::cout << "float: " << nb << "f" << std::endl;
|
||||
std::cout << "double: " << nb << std::endl;
|
||||
}
|
||||
|
||||
void ScalarConverter::convert(const std::string &s) {
|
||||
nbType type = GetType(s);
|
||||
|
||||
switch (type) {
|
||||
case CHAR:
|
||||
_Convert<char>(s);
|
||||
break;
|
||||
case INT:
|
||||
_Convert<int>(s);
|
||||
break;
|
||||
case FLOAT:
|
||||
_Convert<float>(s);
|
||||
break;
|
||||
case DOUBLE:
|
||||
_Convert<double>(s);
|
||||
break;
|
||||
case NAN:
|
||||
_ConvertNan(s);
|
||||
break;
|
||||
case NONE:
|
||||
std::cout << s << " does not convert to any type" << std::endl;
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
@ -6,18 +6,17 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/03 19:38:39 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/08 14:29:28 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/09 10:31:49 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class ScalarConverter {
|
||||
public:
|
||||
static void convert(std::string &);
|
||||
static void convert(const std::string &);
|
||||
|
||||
private:
|
||||
ScalarConverter(void);
|
||||
@ -25,17 +24,4 @@ class ScalarConverter {
|
||||
~ScalarConverter(void);
|
||||
|
||||
ScalarConverter &operator=(const ScalarConverter &);
|
||||
|
||||
template <typename T> void _printFloat(T nb) {
|
||||
std::cout << "float: " << static_cast<float>(nb) << "f" << std::endl;
|
||||
}
|
||||
template <typename T> void _printInt(T nb) {
|
||||
std::cout << "int: " << static_cast<int>(nb) << std::endl;
|
||||
}
|
||||
template <typename T> void _printChar(T nb) {
|
||||
std::cout << "char: '" << static_cast<char>(nb) << "'" << std::endl;
|
||||
}
|
||||
template <typename T> void _printDouble(T nb) {
|
||||
std::cout << "double: " << static_cast<double>(nb) << std::endl;
|
||||
}
|
||||
};
|
||||
|
16
ex00/StrTo.cpp
Normal file
16
ex00/StrTo.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* StrTo.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/09 09:59:23 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/09 10:19:50 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "StrTo.hpp"
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
|
20
ex00/StrTo.hpp
Normal file
20
ex00/StrTo.hpp
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* StrTo.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/09 09:24:16 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/09 10:19:50 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
char _ToChar(const std::string &);
|
||||
int _ToInt(const std::string &);
|
||||
float _ToFloat(const std::string &);
|
||||
double _ToDouble(const std::string &);
|
@ -6,12 +6,17 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/03 19:38:09 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/03 19:51:28 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/09 10:31:55 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ScalarConverter.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main(void) {
|
||||
|
||||
int main(int ac, char **av) {
|
||||
if (ac < 2) {
|
||||
std::cout << "needs an input ! :D" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
ScalarConverter::convert(av[1]);
|
||||
}
|
||||
|
Reference in New Issue
Block a user