ex00: finish

This commit is contained in:
Camille Chauvet 2023-09-19 08:48:43 +00:00
parent 9be606322d
commit 16964a3add

View File

@ -8,7 +8,9 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <float.h>
#include <limits.h> #include <limits.h>
#include <string.h>
ScalarConverter::ScalarConverter() ScalarConverter::ScalarConverter()
{} {}
@ -31,10 +33,10 @@ ScalarConverter& ScalarConverter::operator=(const ScalarConverter& src)
double get_value(const std::string& str) double get_value(const std::string& str)
{ {
char *pos; char *pos;
if (str.length() == 3 && str[0] == '\'' && str[2] == '\'')
return str[1];
double value = std::strtod(str.c_str(), &pos); double value = std::strtod(str.c_str(), &pos);
if (pos) if (errno == ERANGE || (pos != NULL && strcmp(pos, "f")))
std::cout << pos << std::endl;
if (errno == ERANGE)
throw std::exception(); throw std::exception();
return value; return value;
} }
@ -42,7 +44,9 @@ double get_value(const std::string& str)
void convert2char(const double value) void convert2char(const double value)
{ {
std::cout << "char: "; std::cout << "char: ";
if (isprint(static_cast<char>(value))) if (value > CHAR_MAX || CHAR_MIN > value)
std::cout << "imposible";
else if (isprint(static_cast<char>(value)))
std::cout << static_cast<char>(value); std::cout << static_cast<char>(value);
else else
std::cout << "Non displayable"; std::cout << "Non displayable";
@ -52,13 +56,23 @@ void convert2char(const double value)
void convert2int(const double value) void convert2int(const double value)
{ {
std::cout << "int: "; std::cout << "int: ";
if (value > INT_MAX) if (value > INT_MAX || INT_MIN > value)
std::cout << "imposible"; std::cout << "imposible";
else else
std::cout << static_cast<int>(value); std::cout << static_cast<int>(value);
std::cout << std::endl; std::cout << std::endl;
} }
void convert2float(const double value)
{
std::cout << "float: ";
if (value > FLT_MAX || FLT_MIN > value)
std::cout << "imposible";
else
std::cout << static_cast<float>(value);
std::cout << std::endl;
}
void ScalarConverter::convert(const std::string &str) void ScalarConverter::convert(const std::string &str)
{ {
double value; double value;
@ -70,4 +84,6 @@ void ScalarConverter::convert(const std::string &str)
} }
convert2char(value); convert2char(value);
convert2int(value); convert2int(value);
convert2float(value);
std::cout << "double: " << value << std::endl;
} }