ex01
This commit is contained in:
commit
40f419d4e5
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"iosfwd": "cpp"
|
||||||
|
}
|
||||||
|
}
|
||||||
35
Makefile
Normal file
35
Makefile
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# :::::::: #
|
||||||
|
# Makefile :+: :+: #
|
||||||
|
# +:+ #
|
||||||
|
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||||
|
# +#+ #
|
||||||
|
# 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
|
||||||
35
common.mk
Normal file
35
common.mk
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# :::::::: #
|
||||||
|
# common.mk :+: :+: #
|
||||||
|
# +:+ #
|
||||||
|
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||||
|
# +#+ #
|
||||||
|
# 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
|
||||||
16
ex00/Makefile
Normal file
16
ex00/Makefile
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# :::::::: #
|
||||||
|
# Makefile :+: :+: #
|
||||||
|
# +:+ #
|
||||||
|
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||||
|
# +#+ #
|
||||||
|
# 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
|
||||||
25
ex00/inc/Fixed.hpp
Normal file
25
ex00/inc/Fixed.hpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* Fixed.hpp :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* 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;
|
||||||
|
};
|
||||||
49
ex00/src/Fixed.cpp
Normal file
49
ex00/src/Fixed.cpp
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* Fixed.cpp :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */
|
||||||
|
/* Updated: 2025/03/24 15:31:12 by whaffman ######## odam.nl */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
26
ex00/src/main.cpp
Normal file
26
ex00/src/main.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* main.cpp :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */
|
||||||
|
/* Updated: 2025/03/24 15:32:04 by whaffman ######## odam.nl */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#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);
|
||||||
|
}
|
||||||
19
ex01/Makefile
Normal file
19
ex01/Makefile
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
NAME={project_name}
|
||||||
|
SRC={source_files}
|
||||||
|
include ../common.mk
|
||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# :::::::: #
|
||||||
|
# Makefile :+: :+: #
|
||||||
|
# +:+ #
|
||||||
|
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||||
|
# +#+ #
|
||||||
|
# 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
|
||||||
34
ex01/inc/Fixed.hpp
Normal file
34
ex01/inc/Fixed.hpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* Fixed.hpp :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* 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);
|
||||||
64
ex01/src/Fixed.cpp
Normal file
64
ex01/src/Fixed.cpp
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* Fixed.cpp :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */
|
||||||
|
/* Updated: 2025/03/25 10:05:11 by whaffman ######## odam.nl */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
35
ex01/src/main.cpp
Normal file
35
ex01/src/main.cpp
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* main.cpp :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */
|
||||||
|
/* Updated: 2025/03/25 09:59:15 by whaffman ######## odam.nl */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
16
ex02/Makefile
Normal file
16
ex02/Makefile
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# :::::::: #
|
||||||
|
# Makefile :+: :+: #
|
||||||
|
# +:+ #
|
||||||
|
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||||
|
# +#+ #
|
||||||
|
# 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
|
||||||
16
ex03/Makefile
Normal file
16
ex03/Makefile
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# :::::::: #
|
||||||
|
# Makefile :+: :+: #
|
||||||
|
# +:+ #
|
||||||
|
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||||
|
# +#+ #
|
||||||
|
# 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
|
||||||
Loading…
Reference in New Issue
Block a user