1
0

」 feat(Ex01): Finished

This commit is contained in:
2024-11-19 16:31:58 +01:00
parent 516baf57fe
commit a69f2503af
7 changed files with 196 additions and 4 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

BIN
ex01/Fixed Executable file

Binary file not shown.

66
ex01/Fixed.cpp Normal file
View File

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 21:14:55 by adjoly #+# #+# */
/* Updated: 2024/11/19 16:29:28 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
#include <cmath>
#include <iostream>
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);
}

37
ex01/Fixed.hpp Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fixed.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/09 19:23:08 by adjoly #+# #+# */
/* Updated: 2024/11/19 16:27:15 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <iostream>
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);

55
ex01/Makefile Normal file
View File

@ -0,0 +1,55 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

34
ex01/main.cpp Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/13 20:04:36 by adjoly #+# #+# */
/* Updated: 2024/11/19 12:53:28 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Fixed.hpp"
#include <iostream>
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;
}

View File

@ -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; };
});