aboutsummaryrefslogtreecommitdiff
path: root/cpp06/ex00
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-11-12 10:54:47 +0100
committerCharles Cabergs <me@cacharle.xyz>2020-11-12 10:54:47 +0100
commit96dcf214a8c40529b251ea31ef037868583dd1da (patch)
treeecbb74fd1982829b550f5b5f5451497245db1358 /cpp06/ex00
parent2e0cf63a219d24ef07412deca9b0e6cb02882b46 (diff)
downloadpiscine_cpp-96dcf214a8c40529b251ea31ef037868583dd1da.tar.gz
piscine_cpp-96dcf214a8c40529b251ea31ef037868583dd1da.tar.bz2
piscine_cpp-96dcf214a8c40529b251ea31ef037868583dd1da.zip
Fixing cpp03, moved more common logic in ClapTrap, Added better main
Diffstat (limited to 'cpp06/ex00')
-rw-r--r--cpp06/ex00/DynamicConverter.cpp51
-rw-r--r--cpp06/ex00/DynamicConverter.hpp44
-rw-r--r--cpp06/ex00/detect.cpp13
-rw-r--r--cpp06/ex00/main.cpp93
-rw-r--r--cpp06/ex00/optional.hpp28
5 files changed, 84 insertions, 145 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* 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<int> m_int;
- Optional<char> m_char;
- Optional<float> m_float;
- Optional<double> 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <iostream>
#include <sstream>
+#include <string>
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<float>(d);
+ i = static_cast<int>(d);
+ c = static_cast<char>(d);
+ }
+ if (s == "nanf" || s == "inff")
+ {
+ f = s == "nanf" ? NaN : inff;
+ if (negative)
+ f = -f;
+ d = static_cast<double>(f);
+ i = static_cast<int>(f);
+ c = static_cast<char>(f);
+ }
+
+ if (isdigit(s[0]))
+ {
+ std::stringstream ss(s);
+
+ if (s.find(".") == -1)
+ {
+ ss >> i;
+ d = static_cast<double>(i);
+ f = static_cast<float>(i);
+ c = static_cast<char>(i);
+ }
+ else if (s.find("f") != -1)
+ {
+ ss >> f;
+ d = static_cast<double>(f);
+ i = static_cast<int>(f);
+ c = static_cast<char>(f);
+ }
+ else
+ {
+ ss >> d;
+ f = static_cast<float>(d);
+ i = static_cast<int>(d);
+ c = static_cast<char>(d);
+ }
+ }
+ else if (!negative && s.length == 1)
+ {
+ c = s[0];
+ d = static_cast<double>(c);
+ f = static_cast<float>(c);
+ i = static_cast<int>(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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* 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<typename T>
-class Optional
-{
-public:
- Optional();
- bool hasValue() const;
- T& getValue();
-
-private:
- T m_value;
-};
-
-#endif