From ebbd489d9b0af581fc94168da655159db6b313b9 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sun, 23 Mar 2025 18:43:17 +0100 Subject: [PATCH] style: adding the function in other file --- cpp06/ex00/sources/char.cpp | 31 ++++++++++++++++++++++++++ cpp06/ex00/sources/integer.cpp | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 cpp06/ex00/sources/char.cpp create mode 100644 cpp06/ex00/sources/integer.cpp diff --git a/cpp06/ex00/sources/char.cpp b/cpp06/ex00/sources/char.cpp new file mode 100644 index 0000000..299611a --- /dev/null +++ b/cpp06/ex00/sources/char.cpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* char.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/22 22:06:48 by rparodi #+# #+# */ +/* Updated: 2025/03/22 22:10:24 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScalarConverter.hpp" + +bool isChar(std::string const &str) { + if (str.length() == 1 && !std::isdigit(str[0])) { + return true; + } + return false; +} + +void convertChar(std::string const &str) { + char toChar = str[0]; + int toInt = static_cast(toChar); + float toFloat = static_cast(toChar); + double toDouble = static_cast(toChar); + std::cout << "Char: '" << toChar << "'" << std::endl; + std::cout << "Int: " << toInt << std::endl; + std::cout << "Float: " << toFloat << ".0f" << std::endl; + std::cout << "Double: " << toDouble << ".0" << std::endl; +} diff --git a/cpp06/ex00/sources/integer.cpp b/cpp06/ex00/sources/integer.cpp new file mode 100644 index 0000000..ba90726 --- /dev/null +++ b/cpp06/ex00/sources/integer.cpp @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* integer.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/22 22:11:44 by rparodi #+# #+# */ +/* Updated: 2025/03/23 12:01:21 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ScalarConverter.hpp" + +bool isInt(std::string const &str) { + long long tmp = std::atoll(str.c_str()); + if (tmp > INT_MAX || tmp < INT_MIN) + return false; + if (str.empty() && (str.at(0) == '-' || str.at(0) == '+')) + return false; + for (size_t i = 0; i < str.length(); i++) { + if (str[0] == '-' || str[0] == '+') + i++; + else if (!std::isdigit(str[i])) + return false; + } + return true; +} + +void convertInt(std::string const &str) { + int integer = std::atoi(str.c_str()); + std::cout << "char: "; + if (std::isprint(integer)) + std::cout << "'" << static_cast(integer) << "'" << std::endl; + else + std::cout << "Non displayable" << std::endl; + std::cout << "int: " << integer << std::endl; + std::cout << "float: " << static_cast(integer) << ".0f" << std::endl; + std::cout << "double: " << static_cast(integer) << ".0" << std::endl; +}