Fixed char without quotes

This commit is contained in:
aortigos
2026-03-18 15:16:11 +01:00
parent 6105b150f3
commit 570bee8104

View File

@@ -49,12 +49,12 @@ void ScalarConverter::convert(const std::string &literal)
convertFromSpecialCase(literal); convertFromSpecialCase(literal);
else if (isChar(literal)) else if (isChar(literal))
convertFromChar(literal); convertFromChar(literal);
else if(isInt(literal))
convertFromInt(literal);
else if(isFloat(literal)) else if(isFloat(literal))
convertFromFloat(literal); convertFromFloat(literal);
else if (isDouble(literal)) else if (isDouble(literal))
convertFromDouble(literal); convertFromDouble(literal);
else if(isInt(literal))
convertFromInt(literal);
else else
std::cout << "Error: invalid literal" << std::endl; std::cout << "Error: invalid literal" << std::endl;
} }
@@ -73,14 +73,20 @@ bool ScalarConverter::isChar(const std::string &literal)
{ {
char c; char c;
if (literal.length() != 3) if (literal.length() == 3)
return (false); {
c = literal[1]; c = literal[1];
if (literal[0] == '\'' && literal[2] == '\'' if (literal[0] == '\'' && literal[2] == '\''
&& (c >= 32 && c < 127)) && (c >= 32 && c < 127))
{ {
return (true); return (true);
} }
} else if (literal.length() == 1)
{
c = literal[0];
if (c >= 32 && c < 127 && !std::isdigit(c))
return (true);
}
return (false); return (false);
} }
@@ -101,6 +107,8 @@ bool ScalarConverter::isDouble(const std::string &literal)
{ {
char *end; char *end;
if (literal.find('.') == std::string::npos)
return (false);
strtod(literal.c_str(), &end); strtod(literal.c_str(), &end);
if (*end != '\0') if (*end != '\0')
return (false); return (false);
@@ -134,9 +142,12 @@ void ScalarConverter::convertFromSpecialCase(const std::string &literal)
void ScalarConverter::convertFromChar(const std::string &literal) void ScalarConverter::convertFromChar(const std::string &literal)
{ {
char c; char c = 0;
c = literal[1]; if (literal.length() == 3)
c = literal[1];
else if (literal.length() == 1)
c = literal[0];
std::cout << "char: '" << c << "'" << std::endl; std::cout << "char: '" << c << "'" << std::endl;
std::cout << "int: " << static_cast<int>(c) << std::endl; std::cout << "int: " << static_cast<int>(c) << std::endl;
std::cout << std::fixed << std::setprecision(1); std::cout << std::fixed << std::setprecision(1);