#include "ScalarConverter.hpp" #include 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++; } char* end; long value = strtol(input.c_str(), &end, 10); if (value < std::numeric_limits::min() || value > std::numeric_limits::max()) return false; 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 && hasPoint) { float f = static_cast(atof(input.c_str())); if (std::isinf(f)) return false; return true; } else return false; i++; } return false; } 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++; } double d = atof(input.c_str()); if (std::isinf(d)) return false; 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]; if (input.size() == 3 && input[0] == '\'' && input[2] == '\'') c = input[1]; int i = static_cast(c); float f = static_cast(c); double d = static_cast(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) { char* end; long temp = strtol(input.c_str(), &end, 10); int i = static_cast(temp); if (i >= std::numeric_limits::min() && i <= std::numeric_limits::max()) { char c = static_cast(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(i) << "f" << std::endl; std::cout << "double: \t" << static_cast(i) << std::endl; } void ScalarConverter::toFloat(const std::string &input) { float f = static_cast(atof(input.c_str())); if (f >= std::numeric_limits::min() && f <= std::numeric_limits::max()) { char c = static_cast(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::min() && f <= std::numeric_limits::max()) std::cout << "int: \t" << static_cast(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(f) << std::endl; } void ScalarConverter::toDouble(const std::string &input) { double d = atof(input.c_str()); if (d >= std::numeric_limits::min() && d <= std::numeric_limits::max()) { char c = static_cast(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::min() && d <= std::numeric_limits::max()) std::cout << "int: \t" << static_cast(d) << std::endl; else std::cout << "int: \timpossible" << std::endl; float f = static_cast(d); if (std::isinf(f)) std::cout << "float: \timpossible" << std::endl; else 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 (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; }