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/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.
Reference in New Issue
Block a user