This commit is contained in:
root 2026-01-19 15:51:37 +00:00
parent 3503abc649
commit ac048446e5
2 changed files with 83 additions and 7 deletions

View File

@ -1,11 +1,36 @@
#include "ScalarConverter.hpp"
bool ischar(const std::string& input)
ScalarConverter::ScalarConverter()
{
return (input.length() == 1);
}
bool isInt(const std::string& input)
ScalarConverter::ScalarConverter(const ScalarConverter &src)
{
(void)src;
}
ScalarConverter& ScalarConverter::operator=(const ScalarConverter &src)
{
(void)src;
return (*this);
}
ScalarConverter::~ScalarConverter()
{
}
bool ScalarConverter::isChar(const std::string &input)
{
if (input.size() == 1)
return (true);
if (input.size() == 3)
if (input[0] == '\'' && input[2] == '\'')
return (true);
return (false);
}
bool ScalarConverter::isInt(const std::string& input)
{
if (input.empty())
return false;
@ -23,7 +48,7 @@ bool isInt(const std::string& input)
return true;
}
bool isFloat(const std::string& input)
bool ScalarConverter::isFloat(const std::string& input)
{
if (input.empty())
return false;
@ -47,7 +72,7 @@ bool isFloat(const std::string& input)
return (hasDigit && hasPoint);
}
bool isDouble(const std::string& input)
bool ScalarConverter::isDouble(const std::string& input)
{
if (input.empty())
return false;
@ -68,6 +93,17 @@ bool isDouble(const std::string& input)
}
return (hasDigit && hasPoint);
}
bool ScalarConverter::isOther(const std::string &input)
{
const std::string specials[] = { "-inff", "+inff", "nanf", "-inf", "+inf", "nan" };
for (int i = 0; i < 6; i++)
{
if (input == specials[i])
return (true);
}
return (false);
}
void ScalarConverter::toChar(const std::string &input)
{
@ -88,3 +124,37 @@ void ScalarConverter::toChar(const std::string &input)
std::cout << "float: \t" << std::fixed << std::setprecision(1) << f << "f" << std::endl;
std::cout << "double: \t" << d << std::endl;
}
void ScalarConverter::toOther(const std::string &input)
{
std::cout << "char: \t" << "impossible" << std::endl;
std::cout << "int: \t" << "impossible" << std::endl;
if (input == "-inff" || input == "+inff" || input == "nanf")
{
std::cout << "float: \t" << input << std::endl;
std::string inputFixed = input.substr(0, input.size() - 1);
std::cout << "double: \t" << inputFixed << std::endl;
}
else
{
std::cout << "float: \t" << input << "f" << std::endl;
std::cout << "double: \t" << input << std::endl;
}
}
void ScalarConverter::convert(const std::string &input)
{
if (isChar(input))
toChar(input);
else if (isInt(input))
toInt(input); ////////////////
else if (isFloat(input))
toFloat(input); //////////////
else if (isDouble(input))
toDouble(input); /////////////
else if (isOther(input))
toOther(input);
else
std::cout <<"Invalid input." << std::endl;
}

View File

@ -21,7 +21,13 @@ class ScalarConverter
static bool isInt(const std::string& input);
static bool isFloat(const std::string& input);
static bool isDouble(const std::string& input);
static bool isOther(const std::string &input);
void ScalarConverter::toChar(const std::string &input);
void ScalarConverter::toOther(const std::string &input);
void ScalarConverter::toInt(const std::string &input);
void ScalarConverter::toFloat(const std::string &input);
void ScalarConverter::toDouble(const std::string &input);
};