From ac048446e58ba733523e7dbd161654b51e216a40 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 19 Jan 2026 15:51:37 +0000 Subject: [PATCH] save --- ex00/ScalarConverter.cpp | 80 +++++++++++++++++++++++++++++++++++++--- ex00/ScalarConverter.hpp | 10 ++++- 2 files changed, 83 insertions(+), 7 deletions(-) diff --git a/ex00/ScalarConverter.cpp b/ex00/ScalarConverter.cpp index 9a6f5e0..ffa14f4 100644 --- a/ex00/ScalarConverter.cpp +++ b/ex00/ScalarConverter.cpp @@ -1,11 +1,36 @@ #include "ScalarConverter.hpp" -bool ischar(const std::string& input) + +ScalarConverter::ScalarConverter() { - return (input.length() == 1); } -bool isInt(const std::string& input) +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; @@ -23,7 +48,7 @@ bool isInt(const std::string& input) return true; } -bool isFloat(const std::string& input) +bool ScalarConverter::isFloat(const std::string& input) { if (input.empty()) return false; @@ -47,7 +72,7 @@ bool isFloat(const std::string& input) return (hasDigit && hasPoint); } -bool isDouble(const std::string& input) +bool ScalarConverter::isDouble(const std::string& input) { if (input.empty()) return false; @@ -68,6 +93,17 @@ bool isDouble(const std::string& input) } 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) { @@ -87,4 +123,38 @@ void ScalarConverter::toChar(const std::string &input) 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; } \ No newline at end of file diff --git a/ex00/ScalarConverter.hpp b/ex00/ScalarConverter.hpp index 08e9a9c..d4a44bc 100644 --- a/ex00/ScalarConverter.hpp +++ b/ex00/ScalarConverter.hpp @@ -12,7 +12,7 @@ class ScalarConverter ScalarConverter(void); ScalarConverter(ScalarConverter const &src); ~ScalarConverter(void); - ScalarConverter &operator=(ScalarConverter const &rhs); + ScalarConverter& operator=(ScalarConverter const &rhs); public: void convert(const std::string& input); @@ -21,8 +21,14 @@ class ScalarConverter static bool isInt(const std::string& input); static bool isFloat(const std::string& input); static bool isDouble(const std::string& input); - void ScalarConverter::toChar(const std::string &input); + static bool isOther(const std::string &input); + void ScalarConverter::toChar(const std::string &input); + void ScalarConverter::toOther(const std::string &input); + void ScalarConverter::toInt(const std::string &input); + void ScalarConverter::toFloat(const std::string &input); + void ScalarConverter::toDouble(const std::string &input); + }; #endif \ No newline at end of file