mirror of
https://github.com/Ladebeze66/cpp-partie-1.git
synced 2026-11-09 13:17:15 +01:00
ccp
This commit is contained in:
18
cpp02/ex00/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp02/ex00/.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
cpp02/ex00/.vscode/launch.json
vendored
Normal file
24
cpp02/ex00/.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/fgras-ca/CPP/cpp02/ex00",
|
||||
"program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp02/ex00/build/Debug/outDebug",
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
59
cpp02/ex00/.vscode/settings.json
vendored
Normal file
59
cpp02/ex00/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"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
|
||||
}
|
||||
50
cpp02/ex00/Fixed.cpp
Normal file
50
cpp02/ex00/Fixed.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Fixed.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 19:02:33 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 19:23:02 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Fixed.hpp"
|
||||
|
||||
Fixed::Fixed() : value(0)
|
||||
{
|
||||
std::cout << "Default constructor called" << std::endl;
|
||||
}
|
||||
|
||||
Fixed::Fixed(const Fixed &other)
|
||||
{
|
||||
std::cout << "Copy contructor called" << std::endl;
|
||||
*this = other;
|
||||
}
|
||||
|
||||
Fixed &Fixed::operator=(const Fixed &other)
|
||||
{
|
||||
std::cout << "Copy assignment called" << std::endl;
|
||||
if (this != &other)
|
||||
{
|
||||
this->value = other.getRawBits();
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Fixed::~Fixed()
|
||||
{
|
||||
std::cout << "Destructor called" << std::endl;
|
||||
}
|
||||
|
||||
int Fixed::getRawBits(void) const
|
||||
{
|
||||
std::cout << "getRawBits member function called" << std::endl;
|
||||
return (this->value);
|
||||
}
|
||||
|
||||
void Fixed::setRawBits(int const raw)
|
||||
{
|
||||
this->value = raw;
|
||||
}
|
||||
35
cpp02/ex00/Fixed.hpp
Normal file
35
cpp02/ex00/Fixed.hpp
Normal file
@@ -0,0 +1,35 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Fixed.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 18:53:10 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 19:02:13 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FIXED_HPP
|
||||
# define FIXED_HPP
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Fixed
|
||||
{
|
||||
public:
|
||||
Fixed(); // Constructeur par défaut
|
||||
Fixed(const Fixed &other); // Constructeur de recopie
|
||||
Fixed & operator=(const Fixed &other); // Opérateur d'affectation
|
||||
~Fixed(); // Destructeur
|
||||
|
||||
|
||||
int getRawBits(void) const; // Retourne la valeur brute
|
||||
void setRawBits(int const raw); // Initialise la valeur brute
|
||||
|
||||
private:
|
||||
int value; // Valeur du nombre à virgule fixe
|
||||
static const int fractionalBits = 8; // Nombre de bits de la partie fractionnaire
|
||||
};
|
||||
|
||||
#endif
|
||||
62
cpp02/ex00/Makefile
Normal file
62
cpp02/ex00/Makefile
Normal file
@@ -0,0 +1,62 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/12/27 19:17:28 by fgras-ca #+# #+# #
|
||||
# Updated: 2023/12/27 19:18:51 by fgras-ca ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\
|
||||
echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\
|
||||
echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\
|
||||
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 = fixed
|
||||
|
||||
SRC = Fixed.cpp \
|
||||
main.cpp \
|
||||
|
||||
OBJS = ${SRC:.cpp=.o}
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||
RM = rm -f
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
$(NAME) : $(OBJS)
|
||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./fixed" 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
|
||||
31
cpp02/ex00/main.cpp
Normal file
31
cpp02/ex00/main.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 19:12:29 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 19:21:28 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Fixed.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "Création de l'objet a avec le constructeur par défaut." << std::endl;
|
||||
Fixed a;
|
||||
std::cout << "Valeur de a après création : " << a.getRawBits() << std::endl;
|
||||
|
||||
std::cout << "\nCréation de l'objet b par copie de l'objet a." << std::endl;
|
||||
Fixed b(a);
|
||||
std::cout << "Valeur de b après la copie : " << b.getRawBits() << std::endl;
|
||||
|
||||
std::cout << "\nAssignation de l'objet c à l'objet b." << std::endl;
|
||||
Fixed c;
|
||||
c = b;
|
||||
std::cout << "Valeur de c après l'assignation : " << c.getRawBits() << std::endl << std::endl;
|
||||
|
||||
return (0);
|
||||
}
|
||||
18
cpp02/ex01/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp02/ex01/.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
cpp02/ex01/.vscode/launch.json
vendored
Normal file
24
cpp02/ex01/.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/fgras-ca/CPP/cpp02/ex01",
|
||||
"program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp02/ex01/build/Debug/outDebug",
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
105
cpp02/ex01/.vscode/settings.json
vendored
Normal file
105
cpp02/ex01/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
{
|
||||
"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,
|
||||
"files.associations": {
|
||||
"system_error": "cpp",
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"bit": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"cctype": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"compare": "cpp",
|
||||
"concepts": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"string": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"numeric": "cpp",
|
||||
"random": "cpp",
|
||||
"string_view": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"new": "cpp",
|
||||
"numbers": "cpp",
|
||||
"ostream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
}
|
||||
}
|
||||
67
cpp02/ex01/Fixed.cpp
Normal file
67
cpp02/ex01/Fixed.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Fixed.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 20:32:07 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 20:54:35 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Fixed.hpp"
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define CYAN "\033[36m"
|
||||
|
||||
Fixed::Fixed() : value(0)
|
||||
{
|
||||
std::cout << GREEN << "Default constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
Fixed::Fixed(const int intValue) : value(intValue << fractionalBits)//Ce décalage vers la gauche revient à multiplier l'entier par 2^fractionalBits, plaçant ainsi la virgule virtuellement après les 8 bits de droite.
|
||||
{
|
||||
std::cout << CYAN << "Int constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
Fixed::Fixed(const float floatValue) : value(roundf(floatValue * (1 << fractionalBits)))//Vous multipliez le flottant par 2^fractionalBits pour décaler la virgule vers la droite de 8 positions, puis arrondissez le résultat à l'entier le plus proche pour ne pas perdre la précision.
|
||||
{
|
||||
std::cout << CYAN << "Float constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
Fixed::Fixed(const Fixed &other) : value(other.value)
|
||||
{
|
||||
std::cout << YELLOW << "Copy constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
Fixed &Fixed::operator=(const Fixed &other)
|
||||
{
|
||||
std::cout << YELLOW << "Copy assignment operator called" << RESET << std::endl;
|
||||
value = other.value;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Fixed::~Fixed()
|
||||
{
|
||||
std::cout << RED << "Destructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
float Fixed::toFloat(void) const
|
||||
{
|
||||
return ((float)value / (float)(1 << fractionalBits)); //Vous divisez la valeur entière représentant le nombre à virgule fixe par 2^fractionalBits. Cela décale la virgule virtuellement vers la gauche de 8 positions, vous donnant la valeur flottante originale.
|
||||
}
|
||||
|
||||
int Fixed::toInt(void) const
|
||||
{
|
||||
return (value / (1 << fractionalBits)); // Convertit en entier
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &stream, const Fixed &fixed)
|
||||
{
|
||||
stream << fixed.toFloat(); // Affiche en tant que flottant
|
||||
return (stream);
|
||||
}
|
||||
39
cpp02/ex01/Fixed.hpp
Normal file
39
cpp02/ex01/Fixed.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Fixed.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 20:12:09 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 20:28:25 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FIXED_H
|
||||
#define FIXED_H
|
||||
|
||||
#include <iostream>
|
||||
#include <cmath> // Pour roundf
|
||||
|
||||
class Fixed
|
||||
{
|
||||
public:
|
||||
Fixed(); // Constructeur par défaut
|
||||
Fixed(const int intValue); // Constructeur avec un entier
|
||||
Fixed(const float floatValue); // Constructeur avec un flottant
|
||||
Fixed(const Fixed &other); // Constructeur de recopie
|
||||
Fixed &operator=(const Fixed &other); // Opérateur d'affectation
|
||||
~Fixed(); // Destructeur
|
||||
|
||||
float toFloat(void) const; // Convertit en flottant
|
||||
int toInt(void) const; // Convertit en entier
|
||||
|
||||
private:
|
||||
int value; // Valeur du nombre à virgule fixe
|
||||
static const int fractionalBits = 8; // Nombre de bits de la partie fractionnaire
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &stream, const Fixed &fixed); // Surcharge de l'opérateur d’insertion << ésentation en virgule flottante du nombre à virgule fixe dans le flux de sortie (objet output stream)
|
||||
|
||||
#endif
|
||||
BIN
cpp02/ex01/Fixed.o
Normal file
BIN
cpp02/ex01/Fixed.o
Normal file
Binary file not shown.
62
cpp02/ex01/Makefile
Normal file
62
cpp02/ex01/Makefile
Normal file
@@ -0,0 +1,62 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
||||
# Updated: 2023/12/27 20:58:29 by fgras-ca ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\
|
||||
echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\
|
||||
echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\
|
||||
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 = fixed
|
||||
|
||||
SRC = Fixed.cpp \
|
||||
main.cpp \
|
||||
|
||||
OBJS = ${SRC:.cpp=.o}
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||
RM = rm -f
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
$(NAME) : $(OBJS)
|
||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./fixed" 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
|
||||
BIN
cpp02/ex01/fixed
Executable file
BIN
cpp02/ex01/fixed
Executable file
Binary file not shown.
57
cpp02/ex01/main.cpp
Normal file
57
cpp02/ex01/main.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 20:46:29 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 21:06:18 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Fixed.hpp"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
Fixed a;
|
||||
Fixed const b(10);
|
||||
Fixed const c(42.42f);
|
||||
Fixed const d(b);
|
||||
a = Fixed(1234.4321f);
|
||||
|
||||
std::cout << "a is " << a << std::endl;
|
||||
std::cout << "b is " << b << std::endl;
|
||||
std::cout << "c is " << c << std::endl;
|
||||
std::cout << "d is " << d << std::endl;
|
||||
|
||||
std::cout << "a is " << a.toInt() << " as integer" << std::endl;
|
||||
std::cout << "b is " << b.toInt() << " as integer" << std::endl;
|
||||
std::cout << "c is " << c.toInt() << " as integer" << std::endl;
|
||||
std::cout << "d is " << d.toInt() << " as integer" << std::endl;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*a) a is 1234.43
|
||||
a a été initialisé avec la valeur 1234.4321f (un flottant).
|
||||
Lors de la conversion en virgule fixe, cette valeur est multipliée par 2^8 (256, car 8 bits sont utilisés pour la partie fractionnaire) et arrondie à l'entier le plus proche pour stocker sous forme entière.
|
||||
Lors de la conversion pour l'affichage (.toFloat() utilisée dans la surcharge de l'opérateur <<), cette valeur entière est divisée par 256 pour la reconvertir en flottant. Le résultat est 1234.43 après l'arrondi pour l'affichage.
|
||||
b) b is 10
|
||||
b a été initialisé avec la valeur entière 10.
|
||||
Comme c'est un entier sans partie fractionnaire, sa représentation à virgule fixe est juste 10 * 256 = 2560. C'est la valeur stockée en interne.
|
||||
Lors de la conversion pour l'affichage, 2560 est divisé par 256, ce qui redonne 10. Aucun arrondi n'est nécessaire car c'était une valeur entière pour commencer.
|
||||
c) c is 42.4219
|
||||
c a été initialisé avec la valeur flottante 42.42f.
|
||||
Lors de la conversion en virgule fixe, cette valeur est multipliée par 256 et arrondie. Cela peut entraîner une petite imprécision due à l'arrondi.
|
||||
Lors de la conversion pour l'affichage, cette valeur arrondie est divisée par 256, résultant en 42.4219 après l'arrondi pour l'affichage.
|
||||
d) d is 10
|
||||
d est une copie de b, donc il a exactement les mêmes valeurs internes que b.
|
||||
Ainsi, tout comme b, d affiche 10 lorsqu'il est converti en flottant pour l'affichage.
|
||||
Conversion en entiers :
|
||||
Lorsque vous convertissez ces valeurs en entiers (toInt()), la partie fractionnaire est simplement supprimée (en divisant par 256).
|
||||
|
||||
a as integer: 1234 provient de 1234.43 avec la partie fractionnaire supprimée.
|
||||
b as integer: 10 est déjà un entier, donc pas de changement.
|
||||
c as integer: 42 provient de 42.4219 avec la partie fractionnaire supprimée.
|
||||
d as integer: Identique à b, donc 10.*/
|
||||
BIN
cpp02/ex01/main.o
Normal file
BIN
cpp02/ex01/main.o
Normal file
Binary file not shown.
18
cpp02/ex02/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp02/ex02/.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
cpp02/ex02/.vscode/launch.json
vendored
Normal file
24
cpp02/ex02/.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/fgras-ca/CPP/cpp02/ex02",
|
||||
"program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp02/ex02/build/Debug/outDebug",
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
59
cpp02/ex02/.vscode/settings.json
vendored
Normal file
59
cpp02/ex02/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"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
|
||||
}
|
||||
180
cpp02/ex02/Fixed.cpp
Normal file
180
cpp02/ex02/Fixed.cpp
Normal file
@@ -0,0 +1,180 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Fixed.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 20:32:07 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 21:41:52 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Fixed.hpp"
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define CYAN "\033[36m"
|
||||
#define BLUE "\033[34m"
|
||||
|
||||
// Constructeur par défaut
|
||||
Fixed::Fixed() : value(0)
|
||||
{
|
||||
std::cout << GREEN << "Default constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
// Constructeur avec un entier
|
||||
Fixed::Fixed(const int intValue) : value(intValue << fractionalBits)
|
||||
{
|
||||
std::cout << CYAN << "Int constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
// Constructeur avec un flottant
|
||||
Fixed::Fixed(const float floatValue) : value(roundf(floatValue * (1 << fractionalBits)))
|
||||
{
|
||||
std::cout << CYAN << "Float constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
// Constructeur de recopie
|
||||
Fixed::Fixed(const Fixed &other) : value(other.value)
|
||||
{
|
||||
std::cout << YELLOW << "Copy constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
// Opérateur d'affectation
|
||||
Fixed &Fixed::operator=(const Fixed &other)
|
||||
{
|
||||
std::cout << YELLOW << "Copy assignment operator called" << RESET << std::endl;
|
||||
if (this != &other)
|
||||
{
|
||||
this->value = other.value;
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
// Destructeur
|
||||
Fixed::~Fixed()
|
||||
{
|
||||
std::cout << RED << "Destructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
// Conversion en flottant
|
||||
float Fixed::toFloat(void) const
|
||||
{
|
||||
return ((float)value / (float)(1 << fractionalBits));
|
||||
}
|
||||
|
||||
// Conversion en entier
|
||||
int Fixed::toInt(void) const
|
||||
{
|
||||
return (value >> fractionalBits);
|
||||
}
|
||||
|
||||
// Opérateur de sortie
|
||||
std::ostream &operator<<(std::ostream &stream, const Fixed &fixed)
|
||||
{
|
||||
stream << fixed.toFloat();
|
||||
return (stream);
|
||||
}
|
||||
|
||||
// Opérateurs de comparaison
|
||||
bool Fixed::operator>(const Fixed &other) const
|
||||
{
|
||||
return (this->value > other.value);
|
||||
}
|
||||
|
||||
bool Fixed::operator<(const Fixed &other) const
|
||||
{
|
||||
return (this->value < other.value);
|
||||
}
|
||||
|
||||
bool Fixed::operator<=(const Fixed &other) const
|
||||
{
|
||||
return (this->value <= other.value);
|
||||
}
|
||||
|
||||
bool Fixed::operator>=(const Fixed &other) const
|
||||
{
|
||||
return (this->value >= other.value);
|
||||
}
|
||||
|
||||
bool Fixed::operator==(const Fixed &other) const
|
||||
{
|
||||
return (this->value == other.value);
|
||||
}
|
||||
|
||||
bool Fixed::operator!=(const Fixed &other) const
|
||||
{
|
||||
return (this->value != other.value);
|
||||
}
|
||||
|
||||
// Opérateurs arithmétiques (exemple avec +, -, *, /)
|
||||
Fixed Fixed::operator+(const Fixed &other) const
|
||||
{
|
||||
return (Fixed(this->toFloat() + other.toFloat()));
|
||||
}
|
||||
|
||||
Fixed Fixed::operator-(const Fixed &other) const
|
||||
{
|
||||
return (Fixed(this->toFloat() - other.toFloat()));
|
||||
}
|
||||
|
||||
Fixed Fixed::operator*(const Fixed &other) const
|
||||
{
|
||||
return (Fixed(this->toFloat() * other.toFloat()));
|
||||
}
|
||||
|
||||
Fixed Fixed::operator/(const Fixed &other) const
|
||||
{
|
||||
return (Fixed(this->toFloat() / other.toFloat()));
|
||||
}
|
||||
|
||||
// Opérateurs d'incrémentation et de décrémentation
|
||||
Fixed Fixed::operator++()
|
||||
{
|
||||
this->value++;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Fixed Fixed::operator++(int)
|
||||
{
|
||||
Fixed temp = *this;
|
||||
++(*this);
|
||||
return (temp);
|
||||
}
|
||||
|
||||
Fixed Fixed::operator--()
|
||||
{
|
||||
this->value--;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Fixed Fixed::operator--(int)
|
||||
{
|
||||
Fixed temp = *this;
|
||||
--(*this);
|
||||
return (temp);
|
||||
}
|
||||
|
||||
// Fonctions membres statiques min/max
|
||||
Fixed &Fixed::min(Fixed &a, Fixed &b)
|
||||
{
|
||||
return ((a < b) ? a : b);
|
||||
}
|
||||
|
||||
const Fixed &Fixed::min(const Fixed &a, const Fixed &b)
|
||||
{
|
||||
return ((a < b) ? a : b);
|
||||
}
|
||||
|
||||
Fixed &Fixed::max(Fixed &a, Fixed &b)
|
||||
{
|
||||
return ((a > b) ? a : b);
|
||||
}
|
||||
|
||||
const Fixed &Fixed::max(const Fixed &a, const Fixed &b)
|
||||
{
|
||||
return ((a > b) ? a : b);
|
||||
}
|
||||
64
cpp02/ex02/Fixed.hpp
Normal file
64
cpp02/ex02/Fixed.hpp
Normal file
@@ -0,0 +1,64 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Fixed.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 20:12:09 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 21:26:38 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FIXED_H
|
||||
#define FIXED_H
|
||||
|
||||
#include <iostream>
|
||||
#include <cmath> // Pour roundf
|
||||
|
||||
class Fixed
|
||||
{
|
||||
public:
|
||||
Fixed(); // Constructeur par défaut
|
||||
Fixed(const int intValue); // Constructeur avec un entier
|
||||
Fixed(const float floatValue); // Constructeur avec un flottant
|
||||
Fixed(const Fixed &other); // Constructeur de recopie
|
||||
Fixed &operator=(const Fixed &other); // Opérateur d'affectation
|
||||
~Fixed(); // Destructeur
|
||||
|
||||
float toFloat(void) const; // Convertit en flottant
|
||||
int toInt(void) const; // Convertit en entier
|
||||
// Surcharge des opérateurs de comparaison
|
||||
bool operator>(const Fixed &other) const;
|
||||
bool operator<(const Fixed &other) const;
|
||||
bool operator<=(const Fixed &other) const;
|
||||
bool operator>=(const Fixed &other) const;
|
||||
bool operator==(const Fixed &other) const;
|
||||
bool operator!=(const Fixed &other) const;
|
||||
|
||||
// Surcharge des opérateurs arithmétiques
|
||||
Fixed operator+(const Fixed &other) const;
|
||||
Fixed operator-(const Fixed &other) const;
|
||||
Fixed operator*(const Fixed &other) const;
|
||||
Fixed operator/(const Fixed &other) const;
|
||||
|
||||
// Surcharge des opérateurs d'incrémentation et de décrémentation
|
||||
Fixed operator++(); // Pré-incrémentation
|
||||
Fixed operator++(int); // Post-incrémentation
|
||||
Fixed operator--(int);
|
||||
Fixed operator--();
|
||||
|
||||
// Fonctions membres statiques
|
||||
static Fixed &min(Fixed &a, Fixed &b);
|
||||
static const Fixed &min(const Fixed &a, const Fixed &b);
|
||||
static Fixed &max(Fixed &a, Fixed &b);
|
||||
static const Fixed &max(const Fixed &a, const Fixed &b);
|
||||
|
||||
private:
|
||||
int value; // Valeur du nombre à virgule fixe
|
||||
static const int fractionalBits = 8; // Nombre de bits de la partie fractionnaire
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &stream, const Fixed &fixed); // Surcharge de l'opérateur d’insertion << ésentation en virgule flottante du nombre à virgule fixe dans le flux de sortie (objet output stream)
|
||||
|
||||
#endif
|
||||
BIN
cpp02/ex02/Fixed.o
Normal file
BIN
cpp02/ex02/Fixed.o
Normal file
Binary file not shown.
62
cpp02/ex02/Makefile
Normal file
62
cpp02/ex02/Makefile
Normal file
@@ -0,0 +1,62 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
||||
# Updated: 2023/12/27 20:58:29 by fgras-ca ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\
|
||||
echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\
|
||||
echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\
|
||||
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 = fixed
|
||||
|
||||
SRC = Fixed.cpp \
|
||||
main.cpp \
|
||||
|
||||
OBJS = ${SRC:.cpp=.o}
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||
RM = rm -f
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
$(NAME) : $(OBJS)
|
||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./fixed" 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
|
||||
BIN
cpp02/ex02/fixed
Executable file
BIN
cpp02/ex02/fixed
Executable file
Binary file not shown.
27
cpp02/ex02/main.cpp
Normal file
27
cpp02/ex02/main.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 21:42:40 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 21:43:55 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Fixed.hpp"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
Fixed a;
|
||||
Fixed const b( Fixed( 5.05f ) * Fixed( 2 ) );
|
||||
std::cout << a << std::endl;
|
||||
std::cout << ++a << std::endl;
|
||||
std::cout << a << std::endl;
|
||||
std::cout << a++ << std::endl;
|
||||
std::cout << a << std::endl;
|
||||
std::cout << b << std::endl;
|
||||
std::cout << Fixed::max( a, b ) << std::endl;
|
||||
return (0);
|
||||
}
|
||||
BIN
cpp02/ex02/main.o
Normal file
BIN
cpp02/ex02/main.o
Normal file
Binary file not shown.
18
cpp02/ex03/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp02/ex03/.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
cpp02/ex03/.vscode/launch.json
vendored
Normal file
24
cpp02/ex03/.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/fgras-ca/CPP/cpp02/ex03",
|
||||
"program": "/home/fgras-ca/Bureau/fgras-ca/CPP/cpp02/ex03/build/Debug/outDebug",
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
59
cpp02/ex03/.vscode/settings.json
vendored
Normal file
59
cpp02/ex03/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"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
|
||||
}
|
||||
171
cpp02/ex03/Fixed.cpp
Normal file
171
cpp02/ex03/Fixed.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Fixed.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 20:32:07 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 22:42:03 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Fixed.hpp"
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define CYAN "\033[36m"
|
||||
#define BLUE "\033[34m"
|
||||
|
||||
// Constructeur par défaut
|
||||
Fixed::Fixed() : value(0)
|
||||
{
|
||||
std::cout << GREEN << "Default constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
// Constructeur avec un entier
|
||||
Fixed::Fixed(const int intValue) : value(intValue << fractionalBits)
|
||||
{
|
||||
std::cout << CYAN << "Int constructor called" << RESET << std::endl;
|
||||
}
|
||||
|
||||
// Constructeur avec un flottant
|
||||
Fixed::Fixed(const float floatValue) : value(roundf(floatValue * (1 << fractionalBits))) {}
|
||||
|
||||
// Constructeur de recopie
|
||||
Fixed::Fixed(const Fixed &other) : value(other.value) {}
|
||||
|
||||
// Opérateur d'affectation
|
||||
Fixed &Fixed::operator=(const Fixed &other)
|
||||
{
|
||||
std::cout << YELLOW << "Copy assignment operator called" << RESET << std::endl;
|
||||
if (this != &other)
|
||||
{
|
||||
this->value = other.value;
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
// Destructeur
|
||||
Fixed::~Fixed() {}
|
||||
|
||||
// Conversion en flottant
|
||||
float Fixed::toFloat(void) const
|
||||
{
|
||||
return ((float)value / (float)(1 << fractionalBits));
|
||||
}
|
||||
|
||||
// Conversion en entier
|
||||
int Fixed::toInt(void) const
|
||||
{
|
||||
return (value >> fractionalBits);
|
||||
}
|
||||
|
||||
// Opérateur de sortie
|
||||
std::ostream &operator<<(std::ostream &stream, const Fixed &fixed)
|
||||
{
|
||||
stream << fixed.toFloat();
|
||||
return (stream);
|
||||
}
|
||||
|
||||
// Opérateurs de comparaison
|
||||
bool Fixed::operator>(const Fixed &other) const
|
||||
{
|
||||
return (this->value > other.value);
|
||||
}
|
||||
|
||||
bool Fixed::operator<(const Fixed &other) const
|
||||
{
|
||||
return (this->value < other.value);
|
||||
}
|
||||
|
||||
bool Fixed::operator<=(const Fixed &other) const
|
||||
{
|
||||
return (this->value <= other.value);
|
||||
}
|
||||
|
||||
bool Fixed::operator>=(const Fixed &other) const
|
||||
{
|
||||
return (this->value >= other.value);
|
||||
}
|
||||
|
||||
bool Fixed::operator==(const Fixed &other) const
|
||||
{
|
||||
return (this->value == other.value);
|
||||
}
|
||||
|
||||
bool Fixed::operator!=(const Fixed &other) const
|
||||
{
|
||||
return (this->value != other.value);
|
||||
}
|
||||
|
||||
// Opérateurs arithmétiques (exemple avec +, -, *, /)
|
||||
Fixed Fixed::operator+(const Fixed &other) const
|
||||
{
|
||||
return (Fixed(this->toFloat() + other.toFloat()));
|
||||
}
|
||||
|
||||
Fixed Fixed::operator-(const Fixed &other) const
|
||||
{
|
||||
return (Fixed(this->toFloat() - other.toFloat()));
|
||||
}
|
||||
|
||||
Fixed Fixed::operator*(const Fixed &other) const
|
||||
{
|
||||
return (Fixed(this->toFloat() * other.toFloat()));
|
||||
}
|
||||
|
||||
Fixed Fixed::operator/(const Fixed &other) const
|
||||
{
|
||||
return (Fixed(this->toFloat() / other.toFloat()));
|
||||
}
|
||||
|
||||
// Opérateurs d'incrémentation et de décrémentation
|
||||
Fixed Fixed::operator++()
|
||||
{
|
||||
this->value++;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Fixed Fixed::operator++(int)
|
||||
{
|
||||
Fixed temp = *this;
|
||||
++(*this);
|
||||
return (temp);
|
||||
}
|
||||
|
||||
Fixed Fixed::operator--()
|
||||
{
|
||||
this->value--;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Fixed Fixed::operator--(int)
|
||||
{
|
||||
Fixed temp = *this;
|
||||
--(*this);
|
||||
return (temp);
|
||||
}
|
||||
|
||||
// Fonctions membres statiques min/max
|
||||
Fixed &Fixed::min(Fixed &a, Fixed &b)
|
||||
{
|
||||
return ((a < b) ? a : b);
|
||||
}
|
||||
|
||||
const Fixed &Fixed::min(const Fixed &a, const Fixed &b)
|
||||
{
|
||||
return ((a < b) ? a : b);
|
||||
}
|
||||
|
||||
Fixed &Fixed::max(Fixed &a, Fixed &b)
|
||||
{
|
||||
return ((a > b) ? a : b);
|
||||
}
|
||||
|
||||
const Fixed &Fixed::max(const Fixed &a, const Fixed &b)
|
||||
{
|
||||
return ((a > b) ? a : b);
|
||||
}
|
||||
64
cpp02/ex03/Fixed.hpp
Normal file
64
cpp02/ex03/Fixed.hpp
Normal file
@@ -0,0 +1,64 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Fixed.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 20:12:09 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 21:26:38 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FIXED_H
|
||||
#define FIXED_H
|
||||
|
||||
#include <iostream>
|
||||
#include <cmath> // Pour roundf
|
||||
|
||||
class Fixed
|
||||
{
|
||||
public:
|
||||
Fixed(); // Constructeur par défaut
|
||||
Fixed(const int intValue); // Constructeur avec un entier
|
||||
Fixed(const float floatValue); // Constructeur avec un flottant
|
||||
Fixed(const Fixed &other); // Constructeur de recopie
|
||||
Fixed &operator=(const Fixed &other); // Opérateur d'affectation
|
||||
~Fixed(); // Destructeur
|
||||
|
||||
float toFloat(void) const; // Convertit en flottant
|
||||
int toInt(void) const; // Convertit en entier
|
||||
// Surcharge des opérateurs de comparaison
|
||||
bool operator>(const Fixed &other) const;
|
||||
bool operator<(const Fixed &other) const;
|
||||
bool operator<=(const Fixed &other) const;
|
||||
bool operator>=(const Fixed &other) const;
|
||||
bool operator==(const Fixed &other) const;
|
||||
bool operator!=(const Fixed &other) const;
|
||||
|
||||
// Surcharge des opérateurs arithmétiques
|
||||
Fixed operator+(const Fixed &other) const;
|
||||
Fixed operator-(const Fixed &other) const;
|
||||
Fixed operator*(const Fixed &other) const;
|
||||
Fixed operator/(const Fixed &other) const;
|
||||
|
||||
// Surcharge des opérateurs d'incrémentation et de décrémentation
|
||||
Fixed operator++(); // Pré-incrémentation
|
||||
Fixed operator++(int); // Post-incrémentation
|
||||
Fixed operator--(int);
|
||||
Fixed operator--();
|
||||
|
||||
// Fonctions membres statiques
|
||||
static Fixed &min(Fixed &a, Fixed &b);
|
||||
static const Fixed &min(const Fixed &a, const Fixed &b);
|
||||
static Fixed &max(Fixed &a, Fixed &b);
|
||||
static const Fixed &max(const Fixed &a, const Fixed &b);
|
||||
|
||||
private:
|
||||
int value; // Valeur du nombre à virgule fixe
|
||||
static const int fractionalBits = 8; // Nombre de bits de la partie fractionnaire
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &stream, const Fixed &fixed); // Surcharge de l'opérateur d’insertion << ésentation en virgule flottante du nombre à virgule fixe dans le flux de sortie (objet output stream)
|
||||
|
||||
#endif
|
||||
BIN
cpp02/ex03/Fixed.o
Normal file
BIN
cpp02/ex03/Fixed.o
Normal file
Binary file not shown.
64
cpp02/ex03/Makefile
Normal file
64
cpp02/ex03/Makefile
Normal file
@@ -0,0 +1,64 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
|
||||
# Updated: 2023/12/27 22:09:54 by fgras-ca ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
LOGO = @echo " __, ____, ____, ____, ____ ____, ___, ____,";\
|
||||
echo "(-| (-/_| (-| \ (-|_, (-|__) (-|_, (- / (-|_,";\
|
||||
echo " _|__, _/ |, _|__/ _|__, _|__) _|__, _/__, _|__,";\
|
||||
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 = bsp
|
||||
|
||||
SRC = Fixed.cpp \
|
||||
bsp.cpp \
|
||||
Point.cpp \
|
||||
main.cpp \
|
||||
|
||||
OBJS = ${SRC:.cpp=.o}
|
||||
|
||||
CC = c++
|
||||
CFLAGS = -Wall -Werror -Wextra -std=c++98
|
||||
RM = rm -f
|
||||
|
||||
all : $(NAME)
|
||||
|
||||
$(NAME) : $(OBJS)
|
||||
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
|
||||
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)
|
||||
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./bsp" 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
|
||||
63
cpp02/ex03/Point.cpp
Normal file
63
cpp02/ex03/Point.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Point.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 22:06:12 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 22:38:12 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Point.hpp"
|
||||
|
||||
// Définitions de couleur pour les logs
|
||||
#define RESET "\033[0m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define CYAN "\033[36m"
|
||||
|
||||
// Constructeur par défaut
|
||||
Point::Point() : x(0), y(0)
|
||||
{
|
||||
std::cout << GREEN << "Default constructor called (Point):\n x(0), y(0)" << RESET << std::endl;
|
||||
}
|
||||
|
||||
// Constructeur qui initialise x et y avec des flottants
|
||||
Point::Point(float const xVal, float const yVal) : x(xVal), y(yVal)
|
||||
{
|
||||
std::cout << CYAN << "Float constructor called (Point):\n x(" << xVal << "), y(" << yVal << ")" << RESET << std::endl;
|
||||
}
|
||||
|
||||
// Constructeur de recopie
|
||||
Point::Point(Point const &other) : x(other.getX()), y(other.getY())
|
||||
{
|
||||
std::cout << YELLOW << "Copy constructor called (Point):\n x(" << other.getX() << "), y(" << other.getY() << ")" << RESET << std::endl;
|
||||
}
|
||||
|
||||
// Opérateur d'affectation
|
||||
Point &Point::operator=(Point const &other)
|
||||
{
|
||||
// Comme x et y sont constants, ils ne peuvent être réaffectés.
|
||||
std::cout << RED << "Attempt to use copy assignment operator on Point, which is not allowed due to const members." << RESET << std::endl;
|
||||
return (*this); // Retourne une référence à l'instance actuelle
|
||||
}
|
||||
|
||||
// Destructeur
|
||||
Point::~Point()
|
||||
{
|
||||
std::cout << RED << "Destructor called (Point):\n x(" << this->x << "), y(" << this->y << ")" << RESET << std::endl;
|
||||
}
|
||||
|
||||
// Getters pour accéder aux coordonnées x et y
|
||||
Fixed Point::getX() const
|
||||
{
|
||||
return (this->x);
|
||||
}
|
||||
|
||||
Fixed Point::getY() const
|
||||
{
|
||||
return (this->y);
|
||||
}
|
||||
36
cpp02/ex03/Point.hpp
Normal file
36
cpp02/ex03/Point.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Point.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 21:52:45 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 22:23:40 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef POINT_HPP
|
||||
#define POINT_HPP
|
||||
|
||||
#include "Fixed.hpp"
|
||||
|
||||
class Point
|
||||
{
|
||||
public:
|
||||
Point(); // Constructeur par défaut
|
||||
Point(float const x, float const y); // Constructeur avec deux flottants
|
||||
Point(Point const &other); // Constructeur de recopie
|
||||
Point &operator=(Point const &other); // Opérateur d'affectation
|
||||
~Point(); // Destructeur
|
||||
|
||||
// Getters pour accéder aux coordonnées x et y
|
||||
Fixed getX() const;
|
||||
Fixed getY() const;
|
||||
|
||||
private:
|
||||
Fixed const x; // Coordonnée x du point
|
||||
Fixed const y; // Coordonnée y du point
|
||||
};
|
||||
|
||||
#endif
|
||||
BIN
cpp02/ex03/Point.o
Normal file
BIN
cpp02/ex03/Point.o
Normal file
Binary file not shown.
BIN
cpp02/ex03/bsp
Executable file
BIN
cpp02/ex03/bsp
Executable file
Binary file not shown.
34
cpp02/ex03/bsp.cpp
Normal file
34
cpp02/ex03/bsp.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* bsp.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 21:59:43 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 22:22:35 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "bsp.hpp"
|
||||
|
||||
float sign(Point p1, Point p2, Point p3)
|
||||
{
|
||||
Fixed result = (p1.getX() - p3.getX()) * (p2.getY() - p3.getY()) - (p2.getX() - p3.getX()) * (p1.getY() - p3.getY());
|
||||
return (result.toFloat()); // Convertit le résultat Fixed en float
|
||||
}
|
||||
|
||||
bool bsp(Point const a, Point const b, Point const c, Point const point)
|
||||
{
|
||||
float d1, d2, d3;
|
||||
bool has_neg, has_pos;
|
||||
|
||||
d1 = sign(point, a, b);
|
||||
d2 = sign(point, b, c);
|
||||
d3 = sign(point, c, a);
|
||||
|
||||
has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0);
|
||||
has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0);
|
||||
|
||||
return (!(has_neg && has_pos));
|
||||
}
|
||||
21
cpp02/ex03/bsp.hpp
Normal file
21
cpp02/ex03/bsp.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* bsp.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 22:15:54 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 22:17:48 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef BSP_HPP
|
||||
# define BSP_HPP
|
||||
|
||||
#include "Point.hpp"
|
||||
|
||||
float sign(Point p1, Point p2, Point p3);
|
||||
bool bsp(Point const a, Point const b, Point const c, Point const point);
|
||||
|
||||
#endif
|
||||
BIN
cpp02/ex03/bsp.o
Normal file
BIN
cpp02/ex03/bsp.o
Normal file
Binary file not shown.
49
cpp02/ex03/main.cpp
Normal file
49
cpp02/ex03/main.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/27 22:11:13 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/27 22:42:40 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Point.hpp"
|
||||
#include "bsp.hpp"
|
||||
|
||||
// Fonction pour demander un point à l'utilisateur
|
||||
Point demanderPoint(const std::string &nomPoint)
|
||||
{
|
||||
float x, y;
|
||||
std::cout << "Entrez la coordonnée x de " << nomPoint << ": ";
|
||||
std::cin >> x;
|
||||
std::cout << "Entrez la coordonnée y de " << nomPoint << ": ";
|
||||
std::cin >> y;
|
||||
return (Point(x, y));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Test fixe : Créer un triangle et un point, puis tester avec la fonction bsp
|
||||
Point a(0.0f, 0.0f);
|
||||
Point b(1.0f, 0.0f);
|
||||
Point c(0.5f, 1.0f);
|
||||
Point pointFixe(0.5f, 0.5f);
|
||||
|
||||
std::cout << "Test fixe:" << std::endl;
|
||||
std::cout << "Point à l'intérieur du triangle (0.5, 0.5) : \n" << (bsp(a, b, c, pointFixe) ? "Oui" : "Non") << std::endl;
|
||||
|
||||
// Test dynamique : Demander à l'utilisateur de saisir les points
|
||||
std::cout << "\nTest dynamique:" << std::endl;
|
||||
Point aUser = demanderPoint("A");
|
||||
Point bUser = demanderPoint("B");
|
||||
Point cUser = demanderPoint("C");
|
||||
Point pointUser = demanderPoint("Point à tester");
|
||||
|
||||
// Afficher si le point est à l'intérieur du triangle ou non
|
||||
std::cout << "Le point est à l'intérieur du triangle : " << (bsp(aUser, bUser, cUser, pointUser) ? "Oui" : "Non") << std::endl;
|
||||
|
||||
return (0);
|
||||
}
|
||||
BIN
cpp02/ex03/main.o
Normal file
BIN
cpp02/ex03/main.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user