#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(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::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; }