220 lines
5.8 KiB
C++
220 lines
5.8 KiB
C++
#include "ScalarConverter.hpp"
|
|
|
|
|
|
ScalarConverter::ScalarConverter()
|
|
{
|
|
}
|
|
|
|
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;
|
|
size_t i = 0;
|
|
if (input[0] == '+' || input[0] == '-')
|
|
i = 1;
|
|
if (i >= input.length())
|
|
return false;
|
|
while (i < input.length())
|
|
{
|
|
if (!std::isdigit(input[i]))
|
|
return false;
|
|
i++;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool ScalarConverter::isFloat(const std::string& input)
|
|
{
|
|
if (input.empty())
|
|
return false;
|
|
size_t i = 0;
|
|
bool hasPoint = false;
|
|
bool hasDigit = false;
|
|
if (input[0] == '+' || input[0] == '-')
|
|
i = 1;
|
|
while (i < input.length())
|
|
{
|
|
if (std::isdigit(input[i]))
|
|
hasDigit = true;
|
|
else if (input[i] == '.' && !hasPoint)
|
|
hasPoint = true;
|
|
else if (input[i] == 'f' && i == input.length() - 1 && hasDigit)
|
|
return true;
|
|
else
|
|
return false;
|
|
i++;
|
|
}
|
|
return (hasDigit && hasPoint);
|
|
}
|
|
|
|
bool ScalarConverter::isDouble(const std::string& input)
|
|
{
|
|
if (input.empty())
|
|
return false;
|
|
size_t i = 0;
|
|
bool hasPoint = false;
|
|
bool hasDigit = false;
|
|
if (input[0] == '+' || input[0] == '-')
|
|
i = 1;
|
|
while (i < input.length())
|
|
{
|
|
if (std::isdigit(input[i]))
|
|
hasDigit = true;
|
|
else if (input[i] == '.' && !hasPoint)
|
|
hasPoint = true;
|
|
else
|
|
return false;
|
|
i++;
|
|
}
|
|
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)
|
|
{
|
|
char c = input[0];
|
|
int i = static_cast<int>(c);
|
|
float f = static_cast<float>(c);
|
|
double d = static_cast<double>(c);
|
|
|
|
if (input.size() == 3)
|
|
if (input[0] == '\'' && input[2] == '\'')
|
|
c = input[1];
|
|
|
|
if (isprint(c))
|
|
std::cout << "char: \t'" << c << "'" << std::endl;
|
|
else
|
|
std::cout << "char: \t" << "non displayable" << std::endl;
|
|
std::cout << "int: \t" << i << std::endl;
|
|
std::cout << "float: \t" << std::fixed << std::setprecision(1) << f << "f" << std::endl;
|
|
std::cout << "double: \t" << d << std::endl;
|
|
}
|
|
|
|
void ScalarConverter::toInt(const std::string &input)
|
|
{
|
|
int i = atoi(input.c_str());
|
|
if (i >= std::numeric_limits<char>::min() && i <= std::numeric_limits<char>::max())
|
|
{
|
|
char c = static_cast<char>(i);
|
|
if (isprint(c))
|
|
std::cout << "char: \t'" << c << "'" << std::endl;
|
|
else
|
|
std::cout << "char: \tnon displayable" << std::endl;
|
|
}
|
|
else
|
|
std::cout << "char: \timpossible" << std::endl;
|
|
std::cout << "int: \t" << i << std::endl;
|
|
std::cout << "float: \t" << std::fixed << std::setprecision(1) << static_cast<float>(i) << "f" << std::endl;
|
|
std::cout << "double: \t" << static_cast<double>(i) << std::endl;
|
|
}
|
|
|
|
void ScalarConverter::toFloat(const std::string &input)
|
|
{
|
|
float f = static_cast<float>(atof(input.c_str()));
|
|
if (f >= std::numeric_limits<char>::min() && f <= std::numeric_limits<char>::max())
|
|
{
|
|
char c = static_cast<char>(f);
|
|
if (isprint(c))
|
|
std::cout << "char: \t'" << c << "'" << std::endl;
|
|
else
|
|
std::cout << "char: \tnon displayable" << std::endl;
|
|
}
|
|
else
|
|
std::cout << "char: \timpossible" << std::endl;
|
|
if (f >= std::numeric_limits<int>::min() && f <= std::numeric_limits<int>::max())
|
|
std::cout << "int: \t" << static_cast<int>(f) << std::endl;
|
|
else
|
|
std::cout << "int: \timpossible" << std::endl;
|
|
std::cout << "float: \t" << std::fixed << std::setprecision(1) << f << "f" << std::endl;
|
|
std::cout << "double: \t" << static_cast<double>(f) << std::endl;
|
|
}
|
|
|
|
void ScalarConverter::toDouble(const std::string &input)
|
|
{
|
|
double d = atof(input.c_str());
|
|
if (d >= std::numeric_limits<char>::min() && d <= std::numeric_limits<char>::max())
|
|
{
|
|
char c = static_cast<char>(d);
|
|
if (isprint(c))
|
|
std::cout << "char: \t'" << c << "'" << std::endl;
|
|
else
|
|
std::cout << "char: \tnon displayable" << std::endl;
|
|
}
|
|
else
|
|
std::cout << "char: \timpossible" << std::endl;
|
|
if (d >= std::numeric_limits<int>::min() && d <= std::numeric_limits<int>::max())
|
|
std::cout << "int: \t" << static_cast<int>(d) << std::endl;
|
|
else
|
|
std::cout << "int: \timpossible" << std::endl;
|
|
std::cout << "float: \t" << std::fixed << std::setprecision(1) << static_cast<float>(d) << "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 (isInt(input))
|
|
toInt(input);
|
|
else if (isChar(input))
|
|
toChar(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;
|
|
} |