mirror of
https://github.com/Ladebeze66/cpp-partie-2.git
synced 2025-12-16 05:57:57 +01:00
ex08
This commit is contained in:
parent
d36af81adb
commit
8be6db72f0
18
.vscode/c_cpp_properties.json
vendored
Normal file
18
.vscode/c_cpp_properties.json
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "linux-gcc-x64",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"compilerPath": "/usr/bin/gcc",
|
||||
"cStandard": "${default}",
|
||||
"cppStandard": "${default}",
|
||||
"intelliSenseMode": "linux-gcc-x64",
|
||||
"compilerArgs": [
|
||||
""
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
24
.vscode/launch.json
vendored
Normal file
24
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "C/C++ Runner: Debug Session",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"externalConsole": false,
|
||||
"cwd": "/home/fgras-ca/Bureau/CPP/cpp2/cpp05/ex02",
|
||||
"program": "/home/fgras-ca/Bureau/CPP/cpp2/cpp05/ex02/build/Debug/outDebug",
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
63
.vscode/settings.json
vendored
63
.vscode/settings.json
vendored
@ -48,6 +48,65 @@
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"cstring": "cpp"
|
||||
}
|
||||
"cstring": "cpp",
|
||||
"*.tpp": "c",
|
||||
"list": "cpp"
|
||||
},
|
||||
"C_Cpp_Runner.cCompilerPath": "gcc",
|
||||
"C_Cpp_Runner.cppCompilerPath": "g++",
|
||||
"C_Cpp_Runner.debuggerPath": "gdb",
|
||||
"C_Cpp_Runner.cStandard": "",
|
||||
"C_Cpp_Runner.cppStandard": "",
|
||||
"C_Cpp_Runner.msvcBatchPath": "",
|
||||
"C_Cpp_Runner.useMsvc": false,
|
||||
"C_Cpp_Runner.warnings": [
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Wpedantic",
|
||||
"-Wshadow",
|
||||
"-Wformat=2",
|
||||
"-Wcast-align",
|
||||
"-Wconversion",
|
||||
"-Wsign-conversion",
|
||||
"-Wnull-dereference"
|
||||
],
|
||||
"C_Cpp_Runner.msvcWarnings": [
|
||||
"/W4",
|
||||
"/permissive-",
|
||||
"/w14242",
|
||||
"/w14287",
|
||||
"/w14296",
|
||||
"/w14311",
|
||||
"/w14826",
|
||||
"/w44062",
|
||||
"/w44242",
|
||||
"/w14905",
|
||||
"/w14906",
|
||||
"/w14263",
|
||||
"/w44265",
|
||||
"/w14928"
|
||||
],
|
||||
"C_Cpp_Runner.enableWarnings": true,
|
||||
"C_Cpp_Runner.warningsAsError": false,
|
||||
"C_Cpp_Runner.compilerArgs": [],
|
||||
"C_Cpp_Runner.linkerArgs": [],
|
||||
"C_Cpp_Runner.includePaths": [],
|
||||
"C_Cpp_Runner.includeSearch": [
|
||||
"*",
|
||||
"**/*"
|
||||
],
|
||||
"C_Cpp_Runner.excludeSearch": [
|
||||
"**/build",
|
||||
"**/build/**",
|
||||
"**/.*",
|
||||
"**/.*/**",
|
||||
"**/.vscode",
|
||||
"**/.vscode/**"
|
||||
],
|
||||
"C_Cpp_Runner.useAddressSanitizer": false,
|
||||
"C_Cpp_Runner.useUndefinedSanitizer": false,
|
||||
"C_Cpp_Runner.useLeakSanitizer": false,
|
||||
"C_Cpp_Runner.showCompilationTime": false,
|
||||
"C_Cpp_Runner.useLinkTimeOptimization": false,
|
||||
"C_Cpp_Runner.msvcSecureNoWarnings": false
|
||||
}
|
||||
60
cpp08/ex00/Makefile
Normal file
60
cpp08/ex00/Makefile
Normal file
@ -0,0 +1,60 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
||||
# Updated: 2024/03/03 14:27:18 by fgras-ca ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
LOGO = @echo "🅻🅰🅳🅴🅱🅴🆉🅴";\
|
||||
|
||||
DEF_COLOR = \033[0m
|
||||
GRAY = \033[0;90m
|
||||
RED = \033[0;91m
|
||||
GREEN = \033[0;92m
|
||||
YELLOW = \033[0;93m
|
||||
BLUE = \033[0;94m
|
||||
MAGENTA = \033[0;95m
|
||||
CYAN = \033[0;96m
|
||||
WHITE = \033[0;97m
|
||||
ORANGE = \033[38;5;214m
|
||||
|
||||
NAME = find
|
||||
|
||||
SRC = main.cpp \
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||
RM = rm -f
|
||||
OBJS = ${SRC:.cpp=.o}
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
%.o: %.cpp
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(NAME) : $(OBJS)
|
||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./find" to execute the program!!$(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
clean :
|
||||
@echo "$(RED)Deleating files objects... $(DEF_COLOR)"
|
||||
$(RM) $(OBJS)
|
||||
@echo "$(GREEN)files deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
fclean : clean
|
||||
@echo "$(RED)Delete program name... $(DEF_COLOR)"
|
||||
$(RM) $(NAME)
|
||||
@echo "$(GREEN)File program deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
re : fclean all
|
||||
|
||||
.PHONY : all clean fclean re
|
||||
39
cpp08/ex00/easyfind.hpp
Normal file
39
cpp08/ex00/easyfind.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* easyfind.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/03 14:10:45 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/03/03 14:58:04 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef EASYFIND_HPP
|
||||
#define EASYFIND_HPP
|
||||
|
||||
#include <algorithm> //Pour std::find
|
||||
#include <iterator> //Pour std::distance
|
||||
#include <exception> //Pour std::exception
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
#define CYAN "\033[36m"
|
||||
#define WHITE "\033[37m"
|
||||
|
||||
template<typename T>
|
||||
void easyfind(T& container, int toFind);
|
||||
|
||||
#include "easyfind.tpp"
|
||||
|
||||
#endif
|
||||
30
cpp08/ex00/easyfind.tpp
Normal file
30
cpp08/ex00/easyfind.tpp
Normal file
@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* easyfind.tpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/03 14:14:40 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/03/03 15:10:40 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef EASYFIND_TPP
|
||||
#define EASYFIND_TPP
|
||||
|
||||
template<typename T>
|
||||
void easyfind(T& container, int toFind)
|
||||
{
|
||||
typename T::iterator it = std::find(container.begin(), container.end(), toFind);
|
||||
if (it == container.end())
|
||||
{
|
||||
std::cout << RED << "Not found." << RESET << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << GREEN << "Found at position: " << RESET << std::distance(container.begin(), it) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
68
cpp08/ex00/main.cpp
Normal file
68
cpp08/ex00/main.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/03 14:26:05 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/03/03 15:08:46 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "easyfind.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
// Exemple avec std::vector<int>
|
||||
std::vector<int> vecInt;
|
||||
vecInt.push_back(1);
|
||||
vecInt.push_back(2);
|
||||
vecInt.push_back(3);
|
||||
vecInt.push_back(4);
|
||||
vecInt.push_back(5);
|
||||
std::cout << MAGENTA << "Testing with std::vector<int>: (1, 2, 3, 4, 5) find 3 and 6" << RESET << std::endl;
|
||||
easyfind(vecInt, 3);
|
||||
easyfind(vecInt, 6);
|
||||
|
||||
// Exemple avec std::list<int>
|
||||
std::list<int> listInt;
|
||||
listInt.push_back(10);
|
||||
listInt.push_back(20);
|
||||
listInt.push_back(30);
|
||||
listInt.push_back(40);
|
||||
listInt.push_back(50);
|
||||
std::cout << MAGENTA << "\nTesting with std::list<int>: (10, 20, 30, 40, 50) find 30 and 60" << RESET << std::endl;
|
||||
easyfind(listInt, 30);
|
||||
easyfind(listInt, 60);
|
||||
|
||||
// Exemple avec std::vector<char>
|
||||
std::vector<char> vecChar;
|
||||
vecChar.push_back('a');
|
||||
vecChar.push_back('b');
|
||||
vecChar.push_back('c');
|
||||
vecChar.push_back('d');
|
||||
vecChar.push_back('e');
|
||||
std::cout << MAGENTA << "\nTesting with std::vector<char>: (a, b, c, d, e) find c and f" << RESET << std::endl;
|
||||
easyfind(vecChar, 'c'); // Note: 'c' est promu en int pour la comparaison
|
||||
easyfind(vecChar, 'f'); // Note: 'f' est promu en int pour la comparaison
|
||||
|
||||
// Partie dynamique avec entrée utilisateur
|
||||
std::vector<int> vecUserInput;
|
||||
std::string line;
|
||||
|
||||
std::cout << MAGENTA << "\nDynamic testing - Enter numbers (type 'end' to finish):" << RESET << std::endl;
|
||||
while (std::getline(std::cin, line) && line != "end")
|
||||
{
|
||||
vecUserInput.push_back(std::atoi(line.c_str()));
|
||||
}
|
||||
|
||||
std::cout << CYAN << "Enter value to find:" << RESET << std::endl;
|
||||
int toFind;
|
||||
std::cin >> toFind;
|
||||
|
||||
// Utilisation de easyfind avec le vecteur rempli dynamiquement
|
||||
easyfind(vecUserInput, toFind);
|
||||
|
||||
return (0);
|
||||
}
|
||||
61
cpp08/ex01/Makefile
Normal file
61
cpp08/ex01/Makefile
Normal file
@ -0,0 +1,61 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
||||
# Updated: 2024/03/03 15:48:48 by fgras-ca ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
LOGO = @echo "🅻🅰🅳🅴🅱🅴🆉🅴";\
|
||||
|
||||
DEF_COLOR = \033[0m
|
||||
GRAY = \033[0;90m
|
||||
RED = \033[0;91m
|
||||
GREEN = \033[0;92m
|
||||
YELLOW = \033[0;93m
|
||||
BLUE = \033[0;94m
|
||||
MAGENTA = \033[0;95m
|
||||
CYAN = \033[0;96m
|
||||
WHITE = \033[0;97m
|
||||
ORANGE = \033[38;5;214m
|
||||
|
||||
NAME = span
|
||||
|
||||
SRC = main.cpp \
|
||||
Span.cpp \
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||
RM = rm -f
|
||||
OBJS = ${SRC:.cpp=.o}
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
%.o: %.cpp
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(NAME) : $(OBJS)
|
||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./span" to execute the program!!$(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
clean :
|
||||
@echo "$(RED)Deleating files objects... $(DEF_COLOR)"
|
||||
$(RM) $(OBJS)
|
||||
@echo "$(GREEN)files deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
fclean : clean
|
||||
@echo "$(RED)Delete program name... $(DEF_COLOR)"
|
||||
$(RM) $(NAME)
|
||||
@echo "$(GREEN)File program deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
re : fclean all
|
||||
|
||||
.PHONY : all clean fclean re
|
||||
84
cpp08/ex01/Span.cpp
Normal file
84
cpp08/ex01/Span.cpp
Normal file
@ -0,0 +1,84 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Span.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/03 16:20:25 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/03/03 18:28:26 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Span.hpp"
|
||||
|
||||
Span::Span(unsigned int N): capacity(N)
|
||||
{
|
||||
std::cout << GREEN << "Span constructor called!" << RESET << std::endl;
|
||||
}
|
||||
|
||||
Span::Span(const Span& other): numbers(other.numbers), capacity(other.capacity)
|
||||
{
|
||||
std::cout << CYAN << "Copy constructor called for Span." << RESET << std::endl;
|
||||
}
|
||||
|
||||
Span& Span::operator=(const Span& other)
|
||||
{
|
||||
std::cout << YELLOW << "Copy assignment Span operator called" << RESET << std::endl;
|
||||
if (this != &other)
|
||||
{
|
||||
numbers = other.numbers;
|
||||
capacity = other.capacity;
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Span::~Span()
|
||||
{
|
||||
std::cout << RED << "Span is destroyed!" << RESET << std::endl;
|
||||
}
|
||||
|
||||
void Span::addNumber(int number)
|
||||
{
|
||||
if (numbers.size() >= capacity)
|
||||
throw std::length_error("Span is full.");
|
||||
numbers.push_back(number);
|
||||
}
|
||||
|
||||
int Span::shortestSpan()
|
||||
{
|
||||
if (numbers.size() < 2)
|
||||
throw std::logic_error("Not enough elements for span.");
|
||||
std::sort(numbers.begin(), numbers.end());
|
||||
int minSpan = std::numeric_limits<int>::max();
|
||||
for (size_t i = 1; i < numbers.size(); ++i)
|
||||
{
|
||||
int span = numbers[i] - numbers[i - 1];
|
||||
if (span < minSpan)
|
||||
minSpan = span;
|
||||
}
|
||||
return (minSpan);
|
||||
}
|
||||
|
||||
int Span::longestSpan()
|
||||
{
|
||||
if (numbers.size() < 2)
|
||||
throw std::logic_error("Not enough elements for span.");
|
||||
|
||||
// Utilisation de std::min_element et std::max_element séparément
|
||||
std::vector<int>::iterator minIt = std::min_element(numbers.begin(), numbers.end());
|
||||
std::vector<int>::iterator maxIt = std::max_element(numbers.begin(), numbers.end());
|
||||
|
||||
// Calcul de la span la plus longue
|
||||
return (*maxIt - *minIt);
|
||||
}
|
||||
|
||||
template<typename Iterator>
|
||||
void Span::addRange(Iterator begin, Iterator end)
|
||||
{
|
||||
while (begin != end)
|
||||
{
|
||||
addNumber(*begin);
|
||||
++begin;
|
||||
}
|
||||
}
|
||||
51
cpp08/ex01/Span.hpp
Normal file
51
cpp08/ex01/Span.hpp
Normal file
@ -0,0 +1,51 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Span.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/03 15:53:40 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/03/03 16:49:04 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SPAN_HPP
|
||||
#define SPAN_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <exception>
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <iostream>
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define BLACK "\033[30m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
#define CYAN "\033[36m"
|
||||
#define WHITE "\033[37m"
|
||||
|
||||
class Span
|
||||
{
|
||||
public:
|
||||
Span(unsigned int N); //constructor
|
||||
Span(const Span& other); //copy constructor
|
||||
Span& operator=(const Span& other); //operateur d'assignation
|
||||
~Span(); //destructor
|
||||
|
||||
void addNumber(int number);
|
||||
int shortestSpan();
|
||||
int longestSpan();
|
||||
template<typename Iterator>
|
||||
void addRange(Iterator begin, Iterator end);
|
||||
|
||||
private:
|
||||
std::vector<int> numbers;
|
||||
unsigned int capacity;
|
||||
};
|
||||
|
||||
#endif
|
||||
91
cpp08/ex01/main.cpp
Normal file
91
cpp08/ex01/main.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/03 17:00:49 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/03/03 17:59:58 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Span.hpp"
|
||||
#include <cstdlib> // Pour std::rand et std::srand
|
||||
#include <ctime> // Pour std::time
|
||||
#include <set>
|
||||
|
||||
int main()
|
||||
{
|
||||
// Test initial
|
||||
Span sp = Span(5);
|
||||
sp.addNumber(6);
|
||||
sp.addNumber(3);
|
||||
sp.addNumber(17);
|
||||
sp.addNumber(9);
|
||||
sp.addNumber(11);
|
||||
std::cout << sp.shortestSpan() << std::endl;
|
||||
std::cout << sp.longestSpan() << std::endl;
|
||||
|
||||
// Ajout de tests pour les exceptions
|
||||
try
|
||||
{
|
||||
Span emptySpan(10);
|
||||
emptySpan.shortestSpan();
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cout << RED << "\nException caught for shortestSpan with less than 2 elements: " << RESET << e.what() << std::endl;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Span singleElementSpan(10);
|
||||
singleElementSpan.addNumber(1);
|
||||
singleElementSpan.longestSpan();
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cout << RED << "Exception caught for longestSpan with less than 2 elements: " << RESET << e.what() << std::endl;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Span fullSpan(1);
|
||||
fullSpan.addNumber(1);
|
||||
fullSpan.addNumber(2); // Devrait lancer une exception
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cout << RED << "Exception caught for adding to a full Span: " << RESET << e.what() << std::endl;
|
||||
}
|
||||
|
||||
// Interaction en ligne de commande
|
||||
unsigned int N;
|
||||
std::cout << YELLOW << "\nEnter the number of elements to generate: " << RESET;
|
||||
std::cin >> N;
|
||||
|
||||
Span userSpan(N);
|
||||
std::set<int> uniqueNumbers;
|
||||
while(uniqueNumbers.size() < N)
|
||||
{
|
||||
int number = std::rand() % 100000; // Générer un nombre aléatoire entre 0 et 999
|
||||
if(uniqueNumbers.insert(number).second)
|
||||
{ // Vérifie si l'insertion est réussie (élément n'était pas déjà présent)
|
||||
userSpan.addNumber(number);
|
||||
std::cout << BLUE << "Generated number: " << RESET << number << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (N > 1)
|
||||
{
|
||||
std::cout << CYAN << "Shortest span (userSpan): " << RESET << userSpan.shortestSpan() << std::endl;
|
||||
std::cout << CYAN << "Longest span (userSpan): " << RESET << userSpan.longestSpan() << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << RED << "Not enough elements to calculate spans." << RESET << std::endl;
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
60
cpp08/ex02/Makefile
Normal file
60
cpp08/ex02/Makefile
Normal file
@ -0,0 +1,60 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
||||
# Updated: 2024/03/03 18:13:54 by fgras-ca ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
LOGO = @echo "🅻🅰🅳🅴🅱🅴🆉🅴";\
|
||||
|
||||
DEF_COLOR = \033[0m
|
||||
GRAY = \033[0;90m
|
||||
RED = \033[0;91m
|
||||
GREEN = \033[0;92m
|
||||
YELLOW = \033[0;93m
|
||||
BLUE = \033[0;94m
|
||||
MAGENTA = \033[0;95m
|
||||
CYAN = \033[0;96m
|
||||
WHITE = \033[0;97m
|
||||
ORANGE = \033[38;5;214m
|
||||
|
||||
NAME = mutant
|
||||
|
||||
SRC = main.cpp \
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||
RM = rm -f
|
||||
OBJS = ${SRC:.cpp=.o}
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
%.o: %.cpp
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(NAME) : $(OBJS)
|
||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./mutant" to execute the program!!$(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
clean :
|
||||
@echo "$(RED)Deleating files objects... $(DEF_COLOR)"
|
||||
$(RM) $(OBJS)
|
||||
@echo "$(GREEN)files deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
fclean : clean
|
||||
@echo "$(RED)Delete program name... $(DEF_COLOR)"
|
||||
$(RM) $(NAME)
|
||||
@echo "$(GREEN)File program deleted!! $(DEF_COLOR)"
|
||||
$(LOGO)
|
||||
|
||||
re : fclean all
|
||||
|
||||
.PHONY : all clean fclean re
|
||||
42
cpp08/ex02/MutantStack.hpp
Normal file
42
cpp08/ex02/MutantStack.hpp
Normal file
@ -0,0 +1,42 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* MutantStack.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/03 18:22:10 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/03/03 18:35:45 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MUTANTSTACK_HPP
|
||||
#define MUTANTSTACK_HPP
|
||||
|
||||
#include <stack>
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
|
||||
template<typename T, typename Container = std::deque<T> >
|
||||
class MutantStack : public std::stack<T, Container>
|
||||
{
|
||||
public:
|
||||
MutantStack(); // Constructeur par défaut
|
||||
MutantStack(const MutantStack& other); // Constructeur de copie
|
||||
~MutantStack(); // Destructeur
|
||||
MutantStack& operator=(const MutantStack& other); // Opérateur d'assignation
|
||||
|
||||
typedef typename Container::iterator iterator;
|
||||
typedef typename Container::const_iterator const_iterator;
|
||||
|
||||
iterator begin();
|
||||
iterator end();
|
||||
const_iterator begin() const;
|
||||
const_iterator end() const;
|
||||
|
||||
private:
|
||||
using std::stack<T, Container>::c; // Utilisez 'c' pour accéder au conteneur sous-jacent
|
||||
};
|
||||
|
||||
#include "MutantStack.tpp"
|
||||
#endif
|
||||
65
cpp08/ex02/MutantStack.tpp
Normal file
65
cpp08/ex02/MutantStack.tpp
Normal file
@ -0,0 +1,65 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* MutantStack.tpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/03 18:23:49 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/03/03 18:37:30 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
// Constructeur par défaut
|
||||
template<typename T, typename Container>
|
||||
MutantStack<T, Container>::MutantStack() : std::stack<T, Container>()
|
||||
{
|
||||
std::cout << "MutantStack constructor called!" << std::endl;
|
||||
}
|
||||
// Constructeur de copie
|
||||
template<typename T, typename Container>
|
||||
MutantStack<T, Container>::MutantStack(const MutantStack& other) : std::stack<T, Container>(other)
|
||||
{
|
||||
std::cout << "Copy constructor called for MutantStack." << std::endl;
|
||||
}
|
||||
// Destructeur
|
||||
template<typename T, typename Container>
|
||||
MutantStack<T, Container>::~MutantStack()
|
||||
{
|
||||
std::cout << "MutantStack is destroyed!" << std::endl;
|
||||
}
|
||||
// Opérateur d'assignation
|
||||
template<typename T, typename Container>
|
||||
MutantStack<T, Container>& MutantStack<T, Container>::operator=(const MutantStack& other)
|
||||
{
|
||||
std::cout << "Copy assignment MutantStack operator called" << std::endl;
|
||||
if (this != &other)
|
||||
{
|
||||
std::stack<T, Container>::operator=(other);
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
// Début de l'itérateur
|
||||
template<typename T, typename Container>
|
||||
typename MutantStack<T, Container>::iterator MutantStack<T, Container>::begin()
|
||||
{
|
||||
return (this->c.begin());
|
||||
}
|
||||
// Fin de l'itérateur
|
||||
template<typename T, typename Container>
|
||||
typename MutantStack<T, Container>::iterator MutantStack<T, Container>::end()
|
||||
{
|
||||
return (this->c.end());
|
||||
}
|
||||
// Début de l'itérateur constant
|
||||
template<typename T, typename Container>
|
||||
typename MutantStack<T, Container>::const_iterator MutantStack<T, Container>::begin() const
|
||||
{
|
||||
return (this->c.begin());
|
||||
}
|
||||
// Fin de l'itérateur constant
|
||||
template<typename T, typename Container>
|
||||
typename MutantStack<T, Container>::const_iterator MutantStack<T, Container>::end() const
|
||||
{
|
||||
return (this->c.end());
|
||||
}
|
||||
39
cpp08/ex02/main.cpp
Normal file
39
cpp08/ex02/main.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/03 18:29:10 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/03/03 18:30:06 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "MutantStack.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
MutantStack<int> mstack;
|
||||
mstack.push(5);
|
||||
mstack.push(17);
|
||||
std::cout << mstack.top() << std::endl;
|
||||
mstack.pop();
|
||||
std::cout << mstack.size() << std::endl;
|
||||
mstack.push(3);
|
||||
mstack.push(5);
|
||||
mstack.push(737);
|
||||
//[...]
|
||||
mstack.push(0);
|
||||
MutantStack<int>::iterator it = mstack.begin();
|
||||
MutantStack<int>::iterator ite = mstack.end();
|
||||
++it;
|
||||
--it;
|
||||
while (it != ite)
|
||||
{
|
||||
std::cout << *it << std::endl;
|
||||
++it;
|
||||
}
|
||||
std::stack<int> s(mstack);
|
||||
return (0);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user