commit 40f419d4e5ca63ad098bf0d89631bbaa5f76a0a1 Author: whaffman Date: Tue Mar 25 10:08:06 2025 +0100 ex01 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bab68c5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "iosfwd": "cpp" + } +} \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b1953c0 --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/21 14:50:00 by whaffman #+# #+# # +# Updated: 2025/03/21 14:58:40 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +EX = $(shell find . -maxdepth 1 -type d -name 'ex*' -exec basename {} \;) + +all: + for ex in $(EX); do \ + $(MAKE) -C $$ex; \ + done + +clean: + for ex in $(EX); do \ + $(MAKE) -C $$ex clean; \ + done + +fclean: + for ex in $(EX); do \ + $(MAKE) -C $$ex fclean; \ + done + +re: + for ex in $(EX); do \ + $(MAKE) -C $$ex re; \ + done + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/common.mk b/common.mk new file mode 100644 index 0000000..4bfab0b --- /dev/null +++ b/common.mk @@ -0,0 +1,35 @@ +# **************************************************************************** # +# # +# :::::::: # +# common.mk :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/21 15:00:16 by whaffman #+# #+# # +# Updated: 2025/03/21 15:04:44 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +INC = -I./inc +VPATH = src +OBJ = $(SRC:.cpp=.o) +CC = c++ +CFLAGS = -Wall -Wextra -Werror + +all: $(NAME) + +$(NAME): $(OBJ) + $(CC) $(CFLAGS) $(OBJ) -o $(NAME) + +%.o: %.cpp + $(CC) $(CFLAGS) $(INC) -c $< -o $@ + +clean: + rm -f $(OBJ) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..a6b0d0d --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,16 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/24 15:14:58 by whaffman #+# #+# # +# Updated: 2025/03/24 15:28:10 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +NAME= Fixed00 +SRC= Fixed.cpp main.cpp + +-include ../common.mk diff --git a/ex00/inc/Fixed.hpp b/ex00/inc/Fixed.hpp new file mode 100644 index 0000000..9b98d91 --- /dev/null +++ b/ex00/inc/Fixed.hpp @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Fixed.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ +/* Updated: 2025/03/24 15:24:23 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +class Fixed { +public: + Fixed(); + Fixed(const Fixed &fixed); + ~Fixed(); + Fixed &operator=(const Fixed &fixed); + int getRawBits(void) const; + void setRawBits(int const raw); + +private: + int fixedPointValue; + static const int fractionalBits = 8; +}; \ No newline at end of file diff --git a/ex00/src/Fixed.cpp b/ex00/src/Fixed.cpp new file mode 100644 index 0000000..431e396 --- /dev/null +++ b/ex00/src/Fixed.cpp @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Fixed.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ +/* Updated: 2025/03/24 15:31:12 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include +#include "Fixed.hpp" + +Fixed::Fixed() +{ + fixedPointValue = 0; + std::cout << "Default constructor called" << std::endl; +} + +Fixed::Fixed(const Fixed &fixed) +{ + std::cout << "Copy constructor called" << std::endl; + *this = fixed; +} + +Fixed::~Fixed() +{ + std::cout << "Destructor called" << std::endl; +} +Fixed &Fixed::operator=(const Fixed &fixed) +{ + std::cout << "Copy assignment operator called" << std::endl; + fixedPointValue = fixed.getRawBits(); + return *this; +} + +int Fixed::getRawBits(void) const +{ + std::cout << "getRawBits member function called" << std::endl; + return fixedPointValue; +} + +void Fixed::setRawBits(int const raw) +{ + std::cout << "setRawBits member function called" << std::endl; + fixedPointValue = raw; +} diff --git a/ex00/src/main.cpp b/ex00/src/main.cpp new file mode 100644 index 0000000..f16a541 --- /dev/null +++ b/ex00/src/main.cpp @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* main.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */ +/* Updated: 2025/03/24 15:32:04 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include +#include "Fixed.hpp" + +int main(void) +{ + Fixed a; + Fixed b(a); + Fixed c; + c = b; + std::cout << a.getRawBits() << std::endl; + std::cout << b.getRawBits() << std::endl; + std::cout << c.getRawBits() << std::endl; + return (0); +} \ No newline at end of file diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..79bbc24 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,19 @@ +NAME={project_name} +SRC={source_files} +include ../common.mk +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/24 15:14:58 by whaffman #+# #+# # +# Updated: 2025/03/24 15:15:17 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +NAME= Fixed01 +SRC= Fixed.cpp main.cpp + +-include ../common.mk diff --git a/ex01/inc/Fixed.hpp b/ex01/inc/Fixed.hpp new file mode 100644 index 0000000..7c9c88e --- /dev/null +++ b/ex01/inc/Fixed.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Fixed.hpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ +/* Updated: 2025/03/25 10:05:17 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +class Fixed { +public: + Fixed(); + Fixed(const int value); + Fixed(const float value); + Fixed(const Fixed &fixed); + ~Fixed(); + + Fixed &operator=(const Fixed &fixed); + + int getRawBits(void) const; + void setRawBits(int const raw); + float toFloat(void) const; + int toInt(void) const; + + +private: + int fixedPointValue; + static const int fractionalBits = 8; +}; + +std::ostream &operator<<(std::ostream &out, const Fixed &fixed); \ No newline at end of file diff --git a/ex01/src/Fixed.cpp b/ex01/src/Fixed.cpp new file mode 100644 index 0000000..84376b9 --- /dev/null +++ b/ex01/src/Fixed.cpp @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Fixed.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ +/* Updated: 2025/03/25 10:05:11 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include +#include "Fixed.hpp" + +Fixed::Fixed() +{ + fixedPointValue = 0; + std::cout << "Default constructor called" << std::endl; +} + +Fixed::Fixed(const Fixed &fixed) +{ + std::cout << "Copy constructor called" << std::endl; + *this = fixed; +} + +Fixed::~Fixed() +{ + std::cout << "Destructor called" << std::endl; +} +Fixed &Fixed::operator=(const Fixed &fixed) +{ + std::cout << "Copy assignment operator called" << std::endl; + fixedPointValue = fixed.getRawBits(); + return *this; +} + +std::ostream &operator<<(std::ostream &out, const Fixed &fixed) +{ + out << fixed.toFloat(); +} + +int Fixed::getRawBits(void) const +{ + std::cout << "getRawBits member function called" << std::endl; + return fixedPointValue; +} + +void Fixed::setRawBits(int const raw) +{ + std::cout << "setRawBits member function called" << std::endl; + fixedPointValue = raw; +} + +float Fixed::toFloat(void) const +{ + return (float)fixedPointValue / (1 << fractionalBits); +} + +int Fixed::toInt(void) const +{ + return (int)fixedPointValue >> fractionalBits; +} \ No newline at end of file diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp new file mode 100644 index 0000000..5ada932 --- /dev/null +++ b/ex01/src/main.cpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* main.cpp :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */ +/* Updated: 2025/03/25 09:59:15 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include +#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; +} \ No newline at end of file diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..3f8d362 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,16 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/24 15:14:58 by whaffman #+# #+# # +# Updated: 2025/03/24 15:15:48 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +NAME= +SRC= + +-include ../common.mk diff --git a/ex03/Makefile b/ex03/Makefile new file mode 100644 index 0000000..78c1e66 --- /dev/null +++ b/ex03/Makefile @@ -0,0 +1,16 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: whaffman +#+ # +# +#+ # +# Created: 2025/03/24 15:14:58 by whaffman #+# #+# # +# Updated: 2025/03/24 15:15:55 by whaffman ######## odam.nl # +# # +# **************************************************************************** # + +NAME= +SRC= + +-include ../common.mk