cpp06 first try

This commit is contained in:
Alexandre POMMIER 2022-08-06 19:24:08 +02:00
parent d6876c53bb
commit f77b0eda83
13 changed files with 473 additions and 0 deletions

37
cpp06/ex00/Makefile Normal file
View File

@ -0,0 +1,37 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/08/06 13:51:28 by apommier #+# #+# #
# Updated: 2022/08/06 18:31:33 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = a.out
SRCS = main.cpp
OBJS = ${SRCS:.cpp=.o}
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
RM = rm -rf
.cpp.o:
$(CC) ${CFLAGS} -c $< -o $(<:.cpp=.o)
${NAME}: ${OBJS}
${CC} ${LIB} ${OBJS} -o ${NAME}
all: ${NAME}
clean:
@${RM} ${OBJS}
fclean: clean
@${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

91
cpp06/ex00/main.cpp Normal file
View File

@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/06 16:53:32 by apommier #+# #+# */
/* Updated: 2022/08/06 18:29:37 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include <sstream>
#include <iostream>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <iomanip>
void printNan(std::string str)
{
std::cout << "char: impossible" << std::endl;
std::cout << "int: impossible" << std::endl;
if (str == "-inf" || str == "+inf" || str == "nan")
str += "f";
std::cout << "float: " << str << std::endl;
if (str == "-inff" || str == "+inff" || str == "nanf")
str.erase(str.end()-1);
std::cout << "double: " << str << std::endl;
}
void printConversion(double nbr)
{
//char cast
if (nbr <= CHAR_MAX && nbr >= CHAR_MIN && nbr <= 31)
std::cout << "char: Non displayable\n";
else if (nbr < CHAR_MAX && nbr > CHAR_MIN)
std::cout << "char: " << static_cast<char>(nbr) << std::endl;
else
std::cout << "char: impossible\n";
//int cast
if (nbr <= INT_MAX && nbr >= INT_MIN)
std::cout << "int: " << static_cast<int>(nbr) << std::endl;
else
std::cout << "int: impossible\n";
//float cast
if (nbr <= FLT_MAX && nbr >= -FLT_MAX)
{
std::cout << "float: " << static_cast<float>(nbr);
if (static_cast<float>(nbr) == floor(nbr))
std::cout << ".0";
std::cout << "f" << std::endl;
}
else
std::cout << "float: impossible\n";
//double cast
std::cout << "double: " << static_cast<double>(nbr);
if (static_cast<double>(nbr) == floor(nbr))
std::cout << ".0";
std::cout << std::endl;
}
int main(int ac, char **av)
{
double nbr;
std::stringstream ss;
std::string nbrStr;
if (ac != 2)
{
std::cout << "Wrong number of arguments\n";
return (0);
}
nbrStr = av[1];
if (nbrStr == "-inf" || nbrStr == "+inf" || nbrStr == "nan" || nbrStr == "-inff" || nbrStr == "+inff" || nbrStr == "nanf")
{
printNan(nbrStr);
return (0);
}
ss << nbrStr;
ss >> nbr;
if(ss.fail())
{
std::cout << "Bad entry ! Try to insert a good number\n";
return (0);
}
printConversion(nbr);
}

23
cpp06/ex01/Data.cpp Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Data.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/06 18:27:17 by apommier #+# #+# */
/* Updated: 2022/08/06 18:35:39 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Data.hpp"
uintptr_t serialize(Data* ptr)
{
return (reinterpret_cast<uintptr_t>(ptr));
}
Data* deserialize(uintptr_t raw)
{
return (reinterpret_cast<Data *>(raw));
}

21
cpp06/ex01/Data.hpp Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Data.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/06 18:23:39 by apommier #+# #+# */
/* Updated: 2022/08/06 18:33:43 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include <string>
#include <stdint.h>
struct Data{
std::string proofOfMyExistence;
};
uintptr_t serialize(Data* ptr);
Data* deserialize(uintptr_t raw);

38
cpp06/ex01/Makefile Normal file
View File

@ -0,0 +1,38 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/08/06 18:23:03 by apommier #+# #+# #
# Updated: 2022/08/06 18:31:58 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = a.out
SRCS = main.cpp\
Data.cpp
OBJS = ${SRCS:.cpp=.o}
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
RM = rm -rf
.cpp.o:
$(CC) ${CFLAGS} -c $< -o $(<:.cpp=.o)
${NAME}: ${OBJS}
${CC} ${LIB} ${OBJS} -o ${NAME}
all: ${NAME}
clean:
@${RM} ${OBJS}
fclean: clean
@${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

37
cpp06/ex01/main.cpp Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/06 18:23:10 by apommier #+# #+# */
/* Updated: 2022/08/06 19:22:37 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Data.hpp"
#include <iostream>
int main()
{
std::cout << "==============Reinterpret cast===============\n\n";
Data *ptr = new Data;
uintptr_t intPtr;
std::cout << "Initialization of Data string to \"Hello im right here!\"\n";
ptr->proofOfMyExistence = "Hello im right here!\n";
std::cout << "Data ptr = " << ptr << std::endl;
std::cout << "\n---Calling serialize()---\n";
intPtr = serialize(ptr);
std::cout << "Int ptr = " << &intPtr << std::endl;
std::cout << "Int value = " << intPtr << std::endl;
std::cout << "\n---Calling deserialize()---\n";
Data *ptr2 = deserialize(intPtr);
std::cout << "Data ptr = " << ptr2 << std::endl;
std::cout << "String in data = " << ptr2->proofOfMyExistence;
delete ptr; //ptr2 == ptr
return (0);
}

17
cpp06/ex02/A.hpp Normal file
View File

@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* A.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/06 18:50:05 by apommier #+# #+# */
/* Updated: 2022/08/06 18:50:40 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Base.hpp"
class A : public Base{
};

17
cpp06/ex02/B.hpp Normal file
View File

@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* B.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/06 18:50:02 by apommier #+# #+# */
/* Updated: 2022/08/06 18:50:48 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Base.hpp"
class B : public Base{
};

18
cpp06/ex02/Base.cpp Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Base.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/06 19:14:08 by apommier #+# #+# */
/* Updated: 2022/08/06 19:14:42 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Base.hpp"
Base::~Base()
{
}

22
cpp06/ex02/Base.hpp Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Base.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/06 18:48:01 by apommier #+# #+# */
/* Updated: 2022/08/06 18:59:50 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BASE_HPP
# define BASE_HPP
class Base{
public :
virtual ~Base();
};
#endif

17
cpp06/ex02/C.hpp Normal file
View File

@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* C.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/06 18:49:59 by apommier #+# #+# */
/* Updated: 2022/08/06 18:50:53 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Base.hpp"
class C : public Base{
};

38
cpp06/ex02/Makefile Normal file
View File

@ -0,0 +1,38 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/08/06 18:47:18 by apommier #+# #+# #
# Updated: 2022/08/06 19:15:07 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = a.out
SRCS = main.cpp\
Base.cpp
OBJS = ${SRCS:.cpp=.o}
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
RM = rm -rf
.cpp.o:
$(CC) ${CFLAGS} -c $< -o $(<:.cpp=.o)
${NAME}: ${OBJS}
${CC} ${LIB} ${OBJS} -o ${NAME}
all: ${NAME}
clean:
@${RM} ${OBJS}
fclean: clean
@${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

97
cpp06/ex02/main.cpp Normal file
View File

@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/06 18:47:29 by apommier #+# #+# */
/* Updated: 2022/08/06 19:20:39 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "A.hpp"
#include "B.hpp"
#include "C.hpp"
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <unistd.h>//for usleep (rand() seed is based on time)
Base * generate(void)
{
Base *ret = 0;
std::srand(time(NULL));
int chooseClass = std::rand() % 3;
switch(chooseClass)
{
case 0:
ret = (new A);
std::cout << "A class generated\n";
break;
case 1:
ret = (new B);
std::cout << "B class generated\n";
break;
case 2:
ret = (new C);
std::cout << "C class generated\n";
break;
}
return (ret);
}
void identify(Base& p)
{
Base test;
try
{
test = dynamic_cast<A&>(p);
std::cout << "The class is of type A\n";
}
catch(std::exception &e){}
try
{
test = dynamic_cast<B&>(p);
std::cout << "The class is of type B\n";
}
catch(std::exception &e){}
try
{
test = dynamic_cast<C&>(p);
std::cout << "The class is of type C\n";
}
catch(std::exception &e){}
}
void identify(Base* p)
{
if (dynamic_cast<A*>(p))
std::cout << "The class is of type A\n";
if (dynamic_cast<B*>(p))
std::cout << "The class is of type B\n";
if (dynamic_cast<C*>(p))
std::cout << "The class is of type C\n";
}
int main()
{
Base *test;
for (int i = 0; i < 10; i++)
{
usleep(1017500);
std::cout << "\n======================New class======================\n\n";
std::cout << "Generating random class\n";
test = generate();
std::cout << "\nCalling identify (pointer)\n";
identify(test);
std::cout << "\nCalling identify (reference)\n";
identify(*test);
delete test;
}
}