diff --git a/ex00/Fixed.hpp b/ex00/Fixed.hpp index d24e84c..dbbd429 100644 --- a/ex00/Fixed.hpp +++ b/ex00/Fixed.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/09 19:23:08 by adjoly #+# #+# */ -/* Updated: 2024/11/12 21:49:21 by adjoly ### ########.fr */ +/* Updated: 2024/11/19 11:47:29 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ class Fixed { private: int _number; - static const int _factBit = 8; + static const int _fractBit = 8; public: Fixed(void); Fixed(const Fixed& cpy); diff --git a/ex01/Fixed b/ex01/Fixed new file mode 100755 index 0000000..e167aa7 Binary files /dev/null and b/ex01/Fixed differ diff --git a/ex01/Fixed.cpp b/ex01/Fixed.cpp new file mode 100644 index 0000000..d1bed80 --- /dev/null +++ b/ex01/Fixed.cpp @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/12 21:14:55 by adjoly #+# #+# */ +/* Updated: 2024/11/19 16:29:28 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" +#include +#include + +Fixed::Fixed(void) : _number(0) { + std::cout << "Default constructor called" << std::endl; +} + +Fixed::Fixed(const int nbr) { + std::cout << "Int constructor called" << std::endl; + _number = nbr << _fractBit; +} + +Fixed::Fixed(const float nbr) { + std::cout << "Float constructor called" << std::endl; + _number = roundf(nbr * (1 << _fractBit)); +} + +Fixed::Fixed(const Fixed& cpy) { + std::cout << "Copy constructor called" << std::endl; + *this = cpy; +} + +Fixed::~Fixed(void) { + std::cout << "Destructor called" << std::endl; +} + +Fixed &Fixed::operator=(const Fixed& cpy) { + std::cout << "Copy assignment operator called" << std::endl; + if (this != &cpy) + _number = cpy.getRawBits(); + return (*this); +} + +int Fixed::toInt(void) const { + return (_number >> _fractBit); +} + +float Fixed::toFloat(void) const { + return ((float)_number / (1 << _fractBit)); +} + +int Fixed::getRawBits(void) const { + return (_number); +} + +void Fixed::setRawBits(int const raw) { + _number = raw; +} + +std::ostream &operator<<(std::ostream &file, Fixed const &fixed) { + file << fixed.toFloat(); + return (file); +} diff --git a/ex01/Fixed.hpp b/ex01/Fixed.hpp new file mode 100644 index 0000000..2e76ce8 --- /dev/null +++ b/ex01/Fixed.hpp @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/09 19:23:08 by adjoly #+# #+# */ +/* Updated: 2024/11/19 16:27:15 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +class Fixed { + private: + int _number; + static const int _fractBit = 8; + public: + Fixed(void); + Fixed(const int nbr); + Fixed(const float nbr); + Fixed(const Fixed& cpy); + Fixed& operator=(const Fixed&); + + ~Fixed(void); + + int toInt(void) const; + float toFloat(void) const; + + int getRawBits( void ) const; + void setRawBits( int const raw ); +}; + +std::ostream &operator<<(std::ostream &file, Fixed const &fixed); diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..c333ff1 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,55 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: adjoly +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/10/25 16:09:27 by adjoly #+# #+# # +# Updated: 2024/11/12 21:50:23 by adjoly ### ########.fr # +# # +# **************************************************************************** # + +NAME = Fixed + +CC = c++ + +OBJSDIR = obj/ + +SRCS = main.cpp \ + Fixed.cpp + +OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o)) + +FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP + +RED = \033[0;31m +GREEN = \033[0;32m +YELLOW = \033[1;33m +PURPLE = \e[0;35m +NC = \033[0m +DELETE = \x1B[2K\r + +all: $(NAME) + +$(NAME): $(OBJS) + @$(CC) $(FLAGS) $(OBJS) -o $(NAME) + @printf "$(YELLOW)「✨」($(NAME)) Program compiled\n" + +$(OBJSDIR)%.o: %.cpp + @mkdir -p $(@D) + @$(CC) $(FLAGS) -c $< -o $@ + @printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n" + +clean: + @rm -f $(OBJS) + @printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n" + +fclean: clean + @rm -f $(NAME) + @rm -Rf $(OBJSDIR) + @printf "$(RED)「🗑️」($(NAME)) Program deleted\n" + +re: fclean all + +.PHONY: clean fclean all re diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..168a9dc --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/13 20:04:36 by adjoly #+# #+# */ +/* Updated: 2024/11/19 12:53:28 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Fixed.hpp" +#include + +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; +} diff --git a/flake.nix b/flake.nix index 3ce3ed9..d7c2a58 100644 --- a/flake.nix +++ b/flake.nix @@ -1,7 +1,7 @@ { description = "A Nix-flake-based C/C++ development environment"; inputs = { - nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11"; pogit = { url = "github:y-syo/pogit"; inputs.nixpkgs.follows = "nixpkgs"; @@ -10,7 +10,7 @@ outputs = inputs@{ nixpkgs, ... }: let - supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; + supportedSystems = [ "x86_64-linux" ]; forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f { pkgs = import nixpkgs { inherit system; }; });