--- /dev/null
+#include "ScalarConverter.h"
+#include <cmath>
+#include <cstdlib>
+#include <climits>
+#include <iostream>
+#include <iomanip>
+#include <sstream>
+
+bool ScalarConverter::convert(const std::string &str)
+{
+ double d_rep;
+ int i_rep;
+ char *after_last_digit;
+ std::stringstream int_string_stream;
+
+ if (str.size() == 3 && str[0] == '\'' && str[2] == '\'')
+ {
+ i_rep = str[1];
+ d_rep = i_rep;
+ }
+ else if (str == "nan" || str == "nanf")
+ d_rep = NAN;
+ else if (str == "inf" || str == "inff" || str == "+inf" || str == "+inff")
+ d_rep = INFINITY;
+ else if (str == "-inf" || str == "-inff")
+ d_rep = -INFINITY;
+ else if (str.find('e') != std::string::npos || str.find('f') != std::string::npos || str.find('.') != std::string::npos)
+ {
+ d_rep = std::strtod(str.c_str(), &after_last_digit);
+ if (*after_last_digit && *after_last_digit == 'f')
+ d_rep = static_cast<float>(d_rep);
+ else if (*after_last_digit)
+ return (1);
+ i_rep = static_cast<int>(d_rep);
+ }
+ else
+ {
+ i_rep = std::atoi(str.c_str());
+ int_string_stream << i_rep;
+ if (int_string_stream.str() != str)
+ d_rep = NAN;
+ else
+ d_rep = i_rep;
+ }
+ if (d_rep < INT_MIN || d_rep > INT_MAX || std::isnan(d_rep))
+ {
+ std::cout
+ << "char: impossible\n"
+ << "int: impossible\n";
+ }
+ else
+ {
+ if (i_rep < 0 || i_rep > ((1 << 8) - 1))
+ std::cout << "char: impossible\n";
+ else if (!std::isprint(i_rep))
+ std::cout << "char: non-printable\n";
+ else
+ std::cout << "char: '" << static_cast<unsigned char>(i_rep) << "'\n";
+ std::cout << "int: " << i_rep << '\n';
+ }
+ std::cout
+ << "float: " << std::setprecision(10) << std::showpoint << std::fixed << static_cast<float>(d_rep) << "f\n"
+ << "double: " << std::setprecision(10) << std::showpoint << std::fixed << d_rep << '\n';
+ return (0);
+}
--- /dev/null
+#include "ScalarConverter.h"
+#include <iostream>
+
+int main(int argc, char **argv)
+{
+ if (argc != 2)
+ {
+ std::cout << "Incorrect number of arguments.\n";
+ return (1);
+ }
+ if (ScalarConverter::convert(argv[1]))
+ {
+ std::cout << "Incorrect input string.\n";
+ return (1);
+ }
+ return (0);
+}