From 96dcf214a8c40529b251ea31ef037868583dd1da Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Thu, 12 Nov 2020 10:54:47 +0100 Subject: Fixing cpp03, moved more common logic in ClapTrap, Added better main --- cpp06/ex00/DynamicConverter.cpp | 51 ---------------------- cpp06/ex00/DynamicConverter.hpp | 44 ------------------- cpp06/ex00/detect.cpp | 13 ------ cpp06/ex00/main.cpp | 93 +++++++++++++++++++++++++++++++++++++---- cpp06/ex00/optional.hpp | 28 ------------- 5 files changed, 84 insertions(+), 145 deletions(-) delete mode 100644 cpp06/ex00/DynamicConverter.cpp delete mode 100644 cpp06/ex00/DynamicConverter.hpp delete mode 100644 cpp06/ex00/detect.cpp delete mode 100644 cpp06/ex00/optional.hpp (limited to 'cpp06/ex00') diff --git a/cpp06/ex00/DynamicConverter.cpp b/cpp06/ex00/DynamicConverter.cpp deleted file mode 100644 index c22d469..0000000 --- a/cpp06/ex00/DynamicConverter.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* DynamicConverter.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/04/16 11:48:33 by charles #+# #+# */ -/* Updated: 2020/04/16 12:25:58 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "DynamicConverter.hpp" - -DynamicConverter(DynamicConverter const& other) -{ - *this = other; -} - -DynamicConverter& operator=(DynamicConverter const& other) -{ - if (*this == other) - return *this; - m_origin = other.m_origin; - m_type = other.m_type; - m_int = other.m_int; - m_char = other.m_char; - m_float = other.m_float; - m_double = other.m_double; - return *this; -} - -~DynamicConverter() -{} - -DynamicConverter(std::string const& origin) - : m_origin(origin) -{ - if (isCharLitteral(origin)) - m_type = DetectedTypeChar; - else if (isIntLitteral(origin)) - else if (isFloatLitteral(origin)) - else if (isDoubleLitteral(origin)) - - -} - -bool isCharLitteral(std::string const& s) -{ - if (!origin.length() == 3 && origin[0] == '\'' && origin[2] == '\''); -} diff --git a/cpp06/ex00/DynamicConverter.hpp b/cpp06/ex00/DynamicConverter.hpp deleted file mode 100644 index 3f400a2..0000000 --- a/cpp06/ex00/DynamicConverter.hpp +++ /dev/null @@ -1,44 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* DynamicConverter.hpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/04/16 11:35:47 by charles #+# #+# */ -/* Updated: 2020/04/16 11:56:10 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef INCLUDE_HPP -# define INCLUDE_HPP - -class DynamicConverter -{ -public: - DynamicConverter(DynamicConverter const& other); - DynamicConverter& operator=(DynamicConverter const& other); - ~DynamicConverter(); - - DynamicConverter(std::string const& origin); - - enum DetectedType - { - DetectedTypeInt, - DetectedTypeChar, - DetectedTypeFloat, - DetectedTypeDouble, - }; - -private: - DynamicConverter(); - - std::string m_origin; - DetectedType m_type; - Optional m_int; - Optional m_char; - Optional m_float; - Optional m_double; -}; - -#endif diff --git a/cpp06/ex00/detect.cpp b/cpp06/ex00/detect.cpp deleted file mode 100644 index 5dab8fb..0000000 --- a/cpp06/ex00/detect.cpp +++ /dev/null @@ -1,13 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* detect.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/04/16 11:32:53 by charles #+# #+# */ -/* Updated: 2020/04/16 11:45:28 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - - diff --git a/cpp06/ex00/main.cpp b/cpp06/ex00/main.cpp index 35d64b9..ec30bb8 100644 --- a/cpp06/ex00/main.cpp +++ b/cpp06/ex00/main.cpp @@ -6,27 +6,102 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/16 10:54:10 by charles #+# #+# */ -/* Updated: 2020/04/16 12:23:06 by charles ### ########.fr */ +/* Updated: 2020/11/11 09:21:08 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include #include +#include int main(int argc, char **argv) { - // if (argc != 2) - // { - // std::cout << "Usage: " << argv[0] << " litteral" << std::endl; - // return 1; - // } + if (argc != 2) + { + std::cerr << "Usage: " << argv[0] << " litteral" << std::endl; + return 1; + } - int i; - std::istringstream("bonjour") >> i; + std::string s(argv[1]); + if (s.empty()) + { + std::cerr << "Cannot be empty" << std::endl; + return 1; + } + + bool negative = false; + if (s[0] == '-' || s[0] == '+') + { + if (s[0] == '-') + negative = true; + s.erase(0, 1); + } + + char c; + int i; + float f; + double d; + + if (s == "nan" || s == "inf") + { + d = s == "nan" ? NaN : inf; + if (negative) + d = -d; + f = static_cast(d); + i = static_cast(d); + c = static_cast(d); + } + if (s == "nanf" || s == "inff") + { + f = s == "nanf" ? NaN : inff; + if (negative) + f = -f; + d = static_cast(f); + i = static_cast(f); + c = static_cast(f); + } + + if (isdigit(s[0])) + { + std::stringstream ss(s); + + if (s.find(".") == -1) + { + ss >> i; + d = static_cast(i); + f = static_cast(i); + c = static_cast(i); + } + else if (s.find("f") != -1) + { + ss >> f; + d = static_cast(f); + i = static_cast(f); + c = static_cast(f); + } + else + { + ss >> d; + f = static_cast(d); + i = static_cast(d); + c = static_cast(d); + } + } + else if (!negative && s.length == 1) + { + c = s[0]; + d = static_cast(c); + f = static_cast(c); + i = static_cast(c); + } + else + { + std::cout << "Parsing error" << std::endl; + return 1; + } - std::cout << i << std::endl; return 0; } diff --git a/cpp06/ex00/optional.hpp b/cpp06/ex00/optional.hpp deleted file mode 100644 index 2c77ae3..0000000 --- a/cpp06/ex00/optional.hpp +++ /dev/null @@ -1,28 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* optional.hpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/04/16 11:43:24 by charles #+# #+# */ -/* Updated: 2020/04/16 11:45:26 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef OPTIONAL_HPP -# define OPTIONAL_HPP - -template -class Optional -{ -public: - Optional(); - bool hasValue() const; - T& getValue(); - -private: - T m_value; -}; - -#endif -- cgit