151 lines
3.8 KiB
C++
151 lines
3.8 KiB
C++
#include "ScalarConverter.hpp"
|
|
|
|
#include <climits>
|
|
|
|
ScalarConverter::ScalarConverter() {}
|
|
|
|
ScalarConverter::ScalarConverter(const ScalarConverter &sca) {
|
|
(void)sca;
|
|
}
|
|
|
|
ScalarConverter::~ScalarConverter() {}
|
|
|
|
ScalarConverter &ScalarConverter::operator=(const ScalarConverter &sca) {
|
|
(void) sca;
|
|
return *this;
|
|
}
|
|
|
|
bool isInt(const std::string& str) {
|
|
for (std::size_t i = 0; i < str.length(); ++i)
|
|
if (!std::isdigit(str[i]))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
size_t string_occurence(const std::string &str, char c) {
|
|
int nbr = 0;
|
|
for (size_t i = 0; i < str.size() ; i++)
|
|
if (str[i] == c)
|
|
nbr++;
|
|
return nbr;
|
|
}
|
|
|
|
void printNumber(double nbr, std::string str);
|
|
void printOther(std::string str);
|
|
|
|
void printChar(int nbr);
|
|
void printChar(float nbr);
|
|
void printChar(double nbr);
|
|
|
|
bool special(std::string str);
|
|
bool dot(std::string str);
|
|
|
|
void ScalarConverter::convert(std::string str) {
|
|
|
|
if (special(str))
|
|
return ;
|
|
|
|
if (dot(str))
|
|
return;
|
|
|
|
char* endPtrDouble;
|
|
double nbrDouble = std::strtod(str.c_str(), &endPtrDouble);
|
|
|
|
char* endPtrFloat;
|
|
std::string str_reduce = str.substr(0, str.size() - 1);
|
|
std::strtof(str_reduce.c_str(), &endPtrFloat);
|
|
|
|
if (isInt(str)
|
|
|| (*endPtrFloat == '\0' && str.size() > 1
|
|
&& string_occurence(str, 'f') == 1 &&
|
|
str[str.size() - 1] == 'f')
|
|
|| (*endPtrDouble == '\0'))
|
|
printNumber(nbrDouble, str);
|
|
else
|
|
printOther(str);
|
|
}
|
|
|
|
void printNumber(double nbr, std::string str) {
|
|
printChar(nbr);
|
|
long int numLong = std::strtol(str.c_str(), NULL, 10);
|
|
if (numLong > INT_MAX || numLong < INT_MIN)
|
|
std::cout << "int: impossible" << std::endl;
|
|
else
|
|
std::cout << "int: " << static_cast<int>(nbr) << std::endl;
|
|
if (static_cast<int>(nbr) == nbr) {
|
|
std::cout << "float: " << static_cast<float>(nbr) << ".0f" << std::endl;
|
|
std::cout << "double: " << nbr << ".0" << std::endl;
|
|
}
|
|
else {
|
|
std::cout << "float: " << static_cast<float>(nbr) << "f" << std::endl;
|
|
std::cout << "double: " << nbr << std::endl;
|
|
}
|
|
}
|
|
|
|
void printOther(std::string str) {
|
|
if (str.size() == 1)
|
|
printNumber(static_cast<int>(str[0]), str);
|
|
else {
|
|
std::cout << "char: impossible" << std::endl;
|
|
std::cout << "int: impossible" << std::endl;
|
|
std::cout << "float: impossible" << std::endl;
|
|
std::cout << "double: impossible" << std::endl;
|
|
}
|
|
}
|
|
|
|
void printChar(int nbr) {
|
|
if (std::isprint(nbr) && nbr <= 127 && nbr >= 32)
|
|
std::cout << "char: '" << static_cast<char>(nbr) << "'" << std::endl;
|
|
else
|
|
std::cout << "char: Non displayable" << std::endl;
|
|
}
|
|
|
|
void printChar(double nbr) {
|
|
if (nbr > 127 || nbr < -127)
|
|
std::cout << "char: impossible" << std::endl;
|
|
else if (static_cast<int>(nbr) == nbr)
|
|
printChar(static_cast<int>(nbr));
|
|
else
|
|
std::cout << "char: impossible" << std::endl;
|
|
}
|
|
|
|
void printChar(float nbr) {
|
|
printChar(static_cast<double>(nbr));
|
|
}
|
|
|
|
bool special(std::string str) {
|
|
if (str == "nan" || str == "nanf") {
|
|
std::cout << "char: impossible" << std::endl;
|
|
std::cout << "int: impossible" << std::endl;
|
|
std::cout << "float: nanf" << std::endl;
|
|
std::cout << "double: nan" << std::endl;
|
|
return true;
|
|
}
|
|
else if (str == "+inf" || str == "inf" || str == "+inff" || str == "inff") {
|
|
std::cout << "char: impossible" << std::endl;
|
|
std::cout << "int: impossible" << std::endl;
|
|
std::cout << "float: +inff" << std::endl;
|
|
std::cout << "double: +inf" << std::endl;
|
|
return true;
|
|
}
|
|
else if (str == "-inf" || str == "-inff") {
|
|
std::cout << "char: impossible" << std::endl;
|
|
std::cout << "int: impossible" << std::endl;
|
|
std::cout << "float: -inff" << std::endl;
|
|
std::cout << "double: -inf" << std::endl;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool dot(std::string str) {
|
|
if (str[str.size() - 1] == '.') {
|
|
std::cout << "char: impossible" << std::endl;
|
|
std::cout << "int: impossible" << std::endl;
|
|
std::cout << "float: impossible" << std::endl;
|
|
std::cout << "double: impossible" << std::endl;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|