Implement ex00
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 22 Jan 2025 11:45:29 +0000 (12:45 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 22 Jan 2025 11:45:29 +0000 (12:45 +0100)
I despise this exercise. Hence there is a lot to be improved.

ex00/ScalarConverter.cpp [new file with mode: 0644]
ex00/ScalarConverter.h [new file with mode: 0644]
ex00/main.cpp [new file with mode: 0644]

diff --git a/ex00/ScalarConverter.cpp b/ex00/ScalarConverter.cpp
new file mode 100644 (file)
index 0000000..8b1d48e
--- /dev/null
@@ -0,0 +1,65 @@
+#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);
+}
diff --git a/ex00/ScalarConverter.h b/ex00/ScalarConverter.h
new file mode 100644 (file)
index 0000000..128c729
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef SCALARCONVERTER_H
+# define SCALARCONVERTER_H
+
+# include <string>
+
+class ScalarConverter
+{
+       public:
+               static bool     convert(const std::string &str);
+};
+
+#endif // SCALARCONVERTER_H
diff --git a/ex00/main.cpp b/ex00/main.cpp
new file mode 100644 (file)
index 0000000..6e2f5a7
--- /dev/null
@@ -0,0 +1,17 @@
+#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);
+}