feat: adding the convert char / int
This commit is contained in:
parent
f4bd73f103
commit
5d93eb2fd3
3 changed files with 28 additions and 54 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
|
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
|
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
|
||||||
# Updated: 2025/03/21 15:09:04 by rparodi ### ########.fr #
|
# Updated: 2025/03/22 22:20:09 by rparodi ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
|
@ -30,7 +30,9 @@ DEBUG += -fsanitize=address
|
||||||
CXXFLAGS += $(DEBUG)
|
CXXFLAGS += $(DEBUG)
|
||||||
|
|
||||||
# Sources
|
# Sources
|
||||||
SRC = sources/ScalarConverter.cpp \
|
SRC = sources/char.cpp \
|
||||||
|
sources/ScalarConverter.cpp \
|
||||||
|
sources/integer.cpp \
|
||||||
main.cpp
|
main.cpp
|
||||||
|
|
||||||
# Objects
|
# Objects
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/20 15:50:13 by rparodi #+# #+# */
|
/* Created: 2025/03/20 15:50:13 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/03/21 15:05:26 by rparodi ### ########.fr */
|
/* Updated: 2025/03/22 22:20:50 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -14,6 +14,11 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
bool isInt(std::string const &str);
|
||||||
|
void convertInt(std::string const &str);
|
||||||
|
bool isChar(std::string const &str);
|
||||||
|
void convertChar(std::string const &str);
|
||||||
|
|
||||||
class ScalarConverter {
|
class ScalarConverter {
|
||||||
public:
|
public:
|
||||||
ScalarConverter();
|
ScalarConverter();
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,15 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/20 16:41:16 by rparodi #+# #+# */
|
/* Created: 2025/03/20 16:41:16 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/03/21 15:49:00 by rparodi ### ########.fr */
|
/* Updated: 2025/03/23 15:01:09 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "ScalarConverter.hpp"
|
#include "ScalarConverter.hpp"
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <limits>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
@ -35,55 +37,20 @@ ScalarConverter& ScalarConverter::operator = (ScalarConverter const &assign) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isChar(std::string *input) {
|
|
||||||
if (input->length() == 1 && std::isprint(input->at(0)))
|
|
||||||
return true;
|
|
||||||
if (input->length() == 1 && std::isprint(input->at(0))) {
|
|
||||||
std::cerr << "Please note that non-displayable characters shouldn’t be used as inputs." << std::endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isInt(std::string *input) {
|
|
||||||
for (size_t i = 0; i < input->length(); i++) {
|
|
||||||
if (!std::isdigit(input->at(i))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
std::ostringstream oss;
|
|
||||||
try {
|
|
||||||
oss << std::stoi(*input);
|
|
||||||
} catch (std::exception &e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isFloat(std::string *input) {
|
|
||||||
std::ostringstream oss;
|
|
||||||
try {
|
|
||||||
oss << std::stof(*input);
|
|
||||||
} catch (std::exception &e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isDouble(std::string *input) {
|
|
||||||
std::ostringstream oss;
|
|
||||||
try {
|
|
||||||
oss << std::stod(*input);
|
|
||||||
} catch (std::exception &e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ScalarConverter::convert(std::string *input) {
|
void ScalarConverter::convert(std::string *input) {
|
||||||
std::cout << "Input:\t\t" << *input << std::endl;
|
// std::cout << "Input:\t\t" << *input << std::endl;
|
||||||
std::cout << "IsChar:\t\t" << (isChar(input) ? "✅ | true" : "❌ | false") << std::endl;
|
if (isChar(input->c_str()))
|
||||||
std::cout << "IsInt:\t\t" << (isInt(input) ? "✅ | true" : "❌ | false") << std::endl;
|
convertChar(*input);
|
||||||
std::cout << "IsFloat:\t" << (isFloat(input) ? "✅ | true" : "❌ | false") << std::endl;
|
else if (isInt(input->c_str()))
|
||||||
std::cout << "IsDouble:\t" << (isDouble(input) ? "✅ | true" : "❌ | false") << std::endl;
|
convertInt(*input);
|
||||||
|
// else if (isFloat(input))
|
||||||
|
// convertFloat(*input);
|
||||||
|
// else if (isDouble(input))
|
||||||
|
// convertDouble(*input);
|
||||||
|
// else
|
||||||
|
// std::cout << "Error: Invalid input" << std::endl;
|
||||||
|
// std::cout << "IsChar:\t\t" << (isChar(input->c_str()) ? "✅ | true" : "❌ | false") << std::endl;
|
||||||
|
// std::cout << "IsInt:\t\t" << (isInt(input->c_str()) ? "✅ | true" : "❌ | false") << std::endl;
|
||||||
|
// std::cout << "IsFloat:\t" << (isFloat(input) ? "✅ | true" : "❌ | false") << std::endl;
|
||||||
|
// std::cout << "IsDouble:\t" << (isDouble(input) ? "✅ | true" : "❌ | false") << std::endl;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue