style: adding the function in other file
This commit is contained in:
parent
5d93eb2fd3
commit
ebbd489d9b
2 changed files with 71 additions and 0 deletions
31
cpp06/ex00/sources/char.cpp
Normal file
31
cpp06/ex00/sources/char.cpp
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* char.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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<int>(toChar);
|
||||
float toFloat = static_cast<float>(toChar);
|
||||
double toDouble = static_cast<double>(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;
|
||||
}
|
||||
40
cpp06/ex00/sources/integer.cpp
Normal file
40
cpp06/ex00/sources/integer.cpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* integer.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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<char>(integer) << "'" << std::endl;
|
||||
else
|
||||
std::cout << "Non displayable" << std::endl;
|
||||
std::cout << "int: " << integer << std::endl;
|
||||
std::cout << "float: " << static_cast<float>(integer) << ".0f" << std::endl;
|
||||
std::cout << "double: " << static_cast<double>(integer) << ".0" << std::endl;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue