From 1e9d90bdf9ef5fc05093d3449d883597c7f896de Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sun, 8 Nov 2020 13:39:12 +0100 Subject: Added cpp06 boilerplate --- .gitignore | 2 ++ cpp02/ex01/Fixed.cpp | 4 ++-- cpp02/ex01/Fixed.hpp | 6 +++-- cpp04/ex02/Squad.cpp | 6 ++--- cpp04/ex03/MateriaSource.cpp | 4 ++-- cpp06/ex00/DynamicConverter.cpp | 51 +++++++++++++++++++++++++++++++++++++++++ cpp06/ex00/DynamicConverter.hpp | 44 +++++++++++++++++++++++++++++++++++ cpp06/ex00/Makefile | 35 ++++++++++++++++++++++++++++ cpp06/ex00/detect.cpp | 13 +++++++++++ cpp06/ex00/main.cpp | 32 ++++++++++++++++++++++++++ cpp06/ex00/optional.hpp | 28 ++++++++++++++++++++++ cpp06/ex02/A.hpp | 22 ++++++++++++++++++ cpp06/ex02/B.hpp | 22 ++++++++++++++++++ cpp06/ex02/Base.hpp | 22 ++++++++++++++++++ cpp06/ex02/C.hpp | 22 ++++++++++++++++++ cpp06/ex02/Makefile | 35 ++++++++++++++++++++++++++++ cpp06/ex02/main.cpp | 36 +++++++++++++++++++++++++++++ 17 files changed, 375 insertions(+), 9 deletions(-) create mode 100644 cpp06/ex00/DynamicConverter.cpp create mode 100644 cpp06/ex00/DynamicConverter.hpp create mode 100644 cpp06/ex00/Makefile create mode 100644 cpp06/ex00/detect.cpp create mode 100644 cpp06/ex00/main.cpp create mode 100644 cpp06/ex00/optional.hpp create mode 100644 cpp06/ex02/A.hpp create mode 100644 cpp06/ex02/B.hpp create mode 100644 cpp06/ex02/Base.hpp create mode 100644 cpp06/ex02/C.hpp create mode 100644 cpp06/ex02/Makefile create mode 100644 cpp06/ex02/main.cpp diff --git a/.gitignore b/.gitignore index effcdeb..9b6809c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ replace a_few_functions iter array +identify_real_type +scalar_conversion diff --git a/cpp02/ex01/Fixed.cpp b/cpp02/ex01/Fixed.cpp index 755a6ae..a81c1cf 100644 --- a/cpp02/ex01/Fixed.cpp +++ b/cpp02/ex01/Fixed.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 11:45:18 by charles #+# #+# */ -/* Updated: 2020/10/19 11:29:39 by cacharle ### ########.fr */ +/* Updated: 2020/11/08 13:38:40 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,7 +64,7 @@ float Fixed::toFloat() const int Fixed::toInt() const { - return m_value >> m_fractionalBits; + return roundf(toFloat()); } int Fixed::getFractionalBits() diff --git a/cpp02/ex01/Fixed.hpp b/cpp02/ex01/Fixed.hpp index 726b810..a262c59 100644 --- a/cpp02/ex01/Fixed.hpp +++ b/cpp02/ex01/Fixed.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/13 11:40:49 by charles #+# #+# */ -/* Updated: 2020/10/19 11:29:30 by cacharle ### ########.fr */ +/* Updated: 2020/11/08 13:38:48 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,13 +30,15 @@ public: int getRawBits() const; void setRawBits(int const raw); + int getRawBits() const; + void setRawBits(int const raw); float toFloat() const; int toInt() const; static int getFractionalBits(); private: - int m_value; + int m_value; static int const m_fractionalBits = 8; }; diff --git a/cpp04/ex02/Squad.cpp b/cpp04/ex02/Squad.cpp index ba825b9..11c7862 100644 --- a/cpp04/ex02/Squad.cpp +++ b/cpp04/ex02/Squad.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 15:08:35 by charles #+# #+# */ -/* Updated: 2020/04/14 15:54:17 by charles ### ########.fr */ +/* Updated: 2020/04/15 10:06:04 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,13 +43,13 @@ int Squad::getCount() const ISpaceMarine* Squad::getUnit(int n) const { if (n < 0 || n >= m_size) - return nullptr; + return NULL; return m_units[n]; } int Squad::push(ISpaceMarine* spaceMarine) { - if (spaceMarine == nullptr) + if (spaceMarine == NULL) return (-1); for (int i = 0; i < m_size; i++) if (m_units[i] == spaceMarine) diff --git a/cpp04/ex03/MateriaSource.cpp b/cpp04/ex03/MateriaSource.cpp index bce6e5a..f5aaf60 100644 --- a/cpp04/ex03/MateriaSource.cpp +++ b/cpp04/ex03/MateriaSource.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 17:19:10 by charles #+# #+# */ -/* Updated: 2020/04/14 17:51:07 by charles ### ########.fr */ +/* Updated: 2020/04/15 10:05:47 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,5 +39,5 @@ void MateriaSource::learnMateria(AMateria* materia) AMateria* MateriaSource::createMateria(std::string const& type) { - return nullptr; + return NULL; } diff --git a/cpp06/ex00/DynamicConverter.cpp b/cpp06/ex00/DynamicConverter.cpp new file mode 100644 index 0000000..c22d469 --- /dev/null +++ b/cpp06/ex00/DynamicConverter.cpp @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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 new file mode 100644 index 0000000..3f400a2 --- /dev/null +++ b/cpp06/ex00/DynamicConverter.hpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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/Makefile b/cpp06/ex00/Makefile new file mode 100644 index 0000000..a5cff07 --- /dev/null +++ b/cpp06/ex00/Makefile @@ -0,0 +1,35 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/04/15 09:59:30 by charles #+# #+# # +# Updated: 2020/04/16 10:53:22 by charles ### ########.fr # +# # +# **************************************************************************** # + +NAME = scalar_conversion + +CXX = clang++ +CXXFLAGS = -std=c++98 -Wall -Wextra -Werror + +SRC = main.cpp +OBJ = $(SRC:.cpp=.o) + +all: $(NAME) + +$(NAME): $(OBJ) + $(CXX) -o $@ $< + +%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $^ + +clean: + rm -rf $(OBJ) + +fclean: clean + rm -rf $(NAME) + +re: fclean all diff --git a/cpp06/ex00/detect.cpp b/cpp06/ex00/detect.cpp new file mode 100644 index 0000000..5dab8fb --- /dev/null +++ b/cpp06/ex00/detect.cpp @@ -0,0 +1,13 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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 new file mode 100644 index 0000000..35d64b9 --- /dev/null +++ b/cpp06/ex00/main.cpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/16 10:54:10 by charles #+# #+# */ +/* Updated: 2020/04/16 12:23:06 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +int main(int argc, char **argv) +{ + // if (argc != 2) + // { + // std::cout << "Usage: " << argv[0] << " litteral" << std::endl; + // return 1; + // } + + int i; + + std::istringstream("bonjour") >> i; + + + std::cout << i << std::endl; + + return 0; +} diff --git a/cpp06/ex00/optional.hpp b/cpp06/ex00/optional.hpp new file mode 100644 index 0000000..2c77ae3 --- /dev/null +++ b/cpp06/ex00/optional.hpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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 diff --git a/cpp06/ex02/A.hpp b/cpp06/ex02/A.hpp new file mode 100644 index 0000000..30c68d5 --- /dev/null +++ b/cpp06/ex02/A.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* A.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/15 09:56:52 by charles #+# #+# */ +/* Updated: 2020/04/15 10:03:37 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef A_HPP +# define A_HPP + +# include "Base.hpp" + +class A : public Base +{ +}; + +#endif diff --git a/cpp06/ex02/B.hpp b/cpp06/ex02/B.hpp new file mode 100644 index 0000000..aeac82e --- /dev/null +++ b/cpp06/ex02/B.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* B.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/15 09:57:12 by charles #+# #+# */ +/* Updated: 2020/04/15 10:07:52 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_HPP +# define B_HPP + +# include "Base.hpp" + +class B : public Base +{ +}; + +#endif diff --git a/cpp06/ex02/Base.hpp b/cpp06/ex02/Base.hpp new file mode 100644 index 0000000..c70706a --- /dev/null +++ b/cpp06/ex02/Base.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Base.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/15 09:56:24 by charles #+# #+# */ +/* Updated: 2020/04/15 09:56:41 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BASE_HPP +# define BASE_HPP + +class Base +{ +public: + virtual ~Base(); +}; + +#endif diff --git a/cpp06/ex02/C.hpp b/cpp06/ex02/C.hpp new file mode 100644 index 0000000..537c0c5 --- /dev/null +++ b/cpp06/ex02/C.hpp @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* C.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/15 09:57:24 by charles #+# #+# */ +/* Updated: 2020/04/15 10:03:30 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef C_HPP +# define C_HPP + +# include "Base.hpp" + +class C : public Base +{ +}; + +#endif diff --git a/cpp06/ex02/Makefile b/cpp06/ex02/Makefile new file mode 100644 index 0000000..b68a586 --- /dev/null +++ b/cpp06/ex02/Makefile @@ -0,0 +1,35 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/04/15 09:59:30 by charles #+# #+# # +# Updated: 2020/04/15 10:00:05 by charles ### ########.fr # +# # +# **************************************************************************** # + +NAME = identify_real_type + +CXX = clang++ +CXXFLAGS = -std=c++98 -Wall -Wextra -Werror + +SRC = main.cpp +OBJ = $(SRC:.cpp=.o) + +all: $(NAME) + +$(NAME): $(OBJ) + $(CXX) -o $@ $< + +%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $^ + +clean: + rm -rf $(OBJ) + +fclean: clean + rm -rf $(NAME) + +re: fclean all diff --git a/cpp06/ex02/main.cpp b/cpp06/ex02/main.cpp new file mode 100644 index 0000000..17c0c3d --- /dev/null +++ b/cpp06/ex02/main.cpp @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/15 10:00:18 by charles #+# #+# */ +/* Updated: 2020/04/15 10:05:23 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "Base.hpp" +#include "A.hpp" +#include "B.hpp" +#include "C.hpp" + +Base* generate(void) +{ + switch (rand() % 3) + { + case 0: return new A(); + case 1: return new B(); + case 2: return new C(); + } + return NULL; +} + +int main() +{ + srand(time(NULL)); + + return 0; +} -- cgit