From 3503abc649daf6ca60726c0633598f6ff49919af Mon Sep 17 00:00:00 2001 From: Leo Firmin Date: Wed, 14 Jan 2026 04:08:51 +0100 Subject: [PATCH] first commits --- ex00/Main.cpp | 0 ex00/ScalarConverter.cpp | 90 ++++++++++++++++++++++++++++++++++++++++ ex00/ScalarConverter.hpp | 28 +++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 ex00/Main.cpp create mode 100644 ex00/ScalarConverter.cpp create mode 100644 ex00/ScalarConverter.hpp diff --git a/ex00/Main.cpp b/ex00/Main.cpp new file mode 100644 index 0000000..e69de29 diff --git a/ex00/ScalarConverter.cpp b/ex00/ScalarConverter.cpp new file mode 100644 index 0000000..9a6f5e0 --- /dev/null +++ b/ex00/ScalarConverter.cpp @@ -0,0 +1,90 @@ +#include "ScalarConverter.hpp" + +bool ischar(const std::string& input) +{ + return (input.length() == 1); +} + +bool isInt(const std::string& input) +{ + if (input.empty()) + return false; + size_t i = 0; + if (input[0] == '+' || input[0] == '-') + i = 1; + if (i >= input.length()) + return false; + while (i < input.length()) + { + if (!std::isdigit(input[i])) + return false; + i++; + } + return true; +} + +bool isFloat(const std::string& input) +{ + if (input.empty()) + return false; + size_t i = 0; + bool hasPoint = false; + bool hasDigit = false; + if (input[0] == '+' || input[0] == '-') + i = 1; + while (i < input.length()) + { + if (std::isdigit(input[i])) + hasDigit = true; + else if (input[i] == '.' && !hasPoint) + hasPoint = true; + else if (input[i] == 'f' && i == input.length() - 1 && hasDigit) + return true; + else + return false; + i++; + } + return (hasDigit && hasPoint); +} + +bool isDouble(const std::string& input) +{ + if (input.empty()) + return false; + size_t i = 0; + bool hasPoint = false; + bool hasDigit = false; + if (input[0] == '+' || input[0] == '-') + i = 1; + while (i < input.length()) + { + if (std::isdigit(input[i])) + hasDigit = true; + else if (input[i] == '.' && !hasPoint) + hasPoint = true; + else + return false; + i++; + } + return (hasDigit && hasPoint); +} + +void ScalarConverter::toChar(const std::string &input) +{ + char c = input[0]; + int i = static_cast(c); + float f = static_cast(c); + double d = static_cast(c); + + if (input.size() == 3) + if (input[0] == '\'' && input[2] == '\''); + c = input[1]; + + if (isprint(c)) + std::cout << "char: \t'" << c << "'" << std::endl; + else + std::cout << "char: \t" << "non displayable" << std::endl; + 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; +} \ No newline at end of file diff --git a/ex00/ScalarConverter.hpp b/ex00/ScalarConverter.hpp new file mode 100644 index 0000000..08e9a9c --- /dev/null +++ b/ex00/ScalarConverter.hpp @@ -0,0 +1,28 @@ +#ifndef SCALARCONVERTER_HPP +#define SCALARCONVERTER_HPP + +#include +#include +#include +#include + +class ScalarConverter +{ + private: + ScalarConverter(void); + ScalarConverter(ScalarConverter const &src); + ~ScalarConverter(void); + ScalarConverter &operator=(ScalarConverter const &rhs); + + public: + void convert(const std::string& input); + + static bool isChar(const std::string& input); + 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); + +}; + +#endif \ No newline at end of file