This commit is contained in:
2024-02-20 15:35:19 +01:00
parent a5816720e4
commit 93c619a3c7
267 changed files with 10113 additions and 0 deletions

View 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
View 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
View 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
View 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
View 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

Binary file not shown.

62
cpp02/ex01/Makefile Normal file
View 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

Binary file not shown.

57
cpp02/ex01/main.cpp Normal file
View 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

Binary file not shown.