]> git.dkaiser.de - 42/cpp02.git/commitdiff
Add ex01 main
authorDominik Kaiser <dkaiser@student.42heilbronn.de>
Thu, 13 Feb 2025 15:41:08 +0000 (16:41 +0100)
committerDominik Kaiser <dkaiser@student.42heilbronn.de>
Thu, 13 Feb 2025 15:41:08 +0000 (16:41 +0100)
ex01/Fixed.cpp [new file with mode: 0644]
ex01/Fixed.hpp [new file with mode: 0644]
ex01/Makefile [new file with mode: 0644]
ex01/main.cpp [new file with mode: 0644]

diff --git a/ex01/Fixed.cpp b/ex01/Fixed.cpp
new file mode 100644 (file)
index 0000000..ecd80c7
--- /dev/null
@@ -0,0 +1,87 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   Fixed.cpp                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/02/12 17:06:18 by dkaiser           #+#    #+#             */
+/*   Updated: 2025/02/13 16:23:59 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "Fixed.hpp"
+#include <cmath>
+#include <iostream>
+
+Fixed::Fixed(void) : raw_value(0)
+{
+    std::cout << "Default constructor called" << std::endl;
+}
+
+Fixed::Fixed(const int i)
+{
+    std::cout << "Int constructor called" << std::endl;
+    this->raw_value = i << this->fractional_bits;
+}
+
+Fixed::Fixed(const float f)
+{
+    std::cout << "Float constructor called" << std::endl;
+    float nbr = f;
+    for (int i = 0; i < this->fractional_bits; i++)
+    {
+        nbr *= 2;
+    }
+    this->raw_value = static_cast<int>(std::round(nbr));
+}
+
+Fixed::Fixed(const Fixed &other) : raw_value(other.raw_value)
+{
+    std::cout << "Copy constructor called" << std::endl;
+}
+
+Fixed &Fixed::operator=(const Fixed &other)
+{
+    std::cout << "Copy assignment operator called" << std::endl;
+    this->raw_value = other.getRawBits();
+    return *this;
+}
+
+Fixed::~Fixed(void)
+{
+    std::cout << "Destructor called" << std::endl;
+}
+
+int Fixed::getRawBits(void) const
+{
+    std::cout << "getRawBits member function called" << std::endl;
+    return this->raw_value;
+}
+
+void Fixed::setRawBits(int const raw)
+{
+    std::cout << "setRawBits member function called" << std::endl;
+    this->raw_value = raw;
+}
+
+int Fixed::toInt(void) const
+{
+    return this->raw_value >> this->fractional_bits;
+}
+
+float Fixed::toFloat(void) const
+{
+    float f = static_cast<float>(this->raw_value);
+    for (int i = 0; i < this->fractional_bits; i++)
+    {
+        f /= 2;
+    }
+    return f;
+}
+
+std::ostream &operator<<(std::ostream &os, const Fixed &fixed)
+{
+    os << fixed.toFloat();
+    return os;
+}
diff --git a/ex01/Fixed.hpp b/ex01/Fixed.hpp
new file mode 100644 (file)
index 0000000..cb2878e
--- /dev/null
@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   Fixed.hpp                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/02/12 17:05:34 by dkaiser           #+#    #+#             */
+/*   Updated: 2025/02/13 16:02:44 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef FIXED_H_
+#define FIXED_H_
+
+#include <ostream>
+
+class Fixed
+{
+  public:
+    Fixed(void);
+    Fixed(const int i);
+    Fixed(const float f);
+    Fixed(const Fixed &other);
+    Fixed &operator=(const Fixed &other);
+    ~Fixed(void);
+
+    int getRawBits(void) const;
+    void setRawBits(int const raw);
+    int toInt(void) const;
+    float toFloat(void) const;
+
+  private:
+    static const int fractional_bits = 8;
+    int raw_value;
+};
+
+std::ostream &operator<<(std::ostream &os, const Fixed &fixed);
+
+#endif
diff --git a/ex01/Makefile b/ex01/Makefile
new file mode 100644 (file)
index 0000000..be83d36
--- /dev/null
@@ -0,0 +1,50 @@
+################################################################################
+################################## VARIABLES ###################################
+################################################################################
+
+NAME    := ex01
+
+CC      =  c++
+CFLAGS  =  -Wall -Wextra -Werror -std=c++17
+HEADERS =  -I.
+
+SRC     := main.cpp Fixed.cpp
+
+OBJ_DIR := _obj
+OBJ     := $(addprefix $(OBJ_DIR)/, $(SRC:%.cpp=%.o))
+
+################################################################################
+#################################### RULES #####################################
+################################################################################
+
+all: $(NAME)
+
+$(NAME): $(OBJ)
+       @$(CC) $(CFLAGS) $^ -o $@ $(HEADERS)
+       @echo "[$(NAME)] Created binary."
+
+$(OBJ_DIR)/%.o: %.cpp
+       @if [ ! -d "$(dir $@)" ]; then \
+               mkdir -p $(dir $@); \
+       fi
+       @$(CC) $(CFLAGS) -c $< -o $@ $(HEADERS)
+       @echo "[$(NAME)] Compiled $<."
+
+clean:
+       @if [ -d "$(OBJ_DIR)" ]; then \
+               rm -rf $(OBJ_DIR); \
+               echo "[$(NAME)] Removed object files."; \
+       fi
+
+fclean: clean
+       @if [ -f "$(NAME)" ]; then \
+               rm -f $(NAME); \
+               echo "[$(NAME)] Removed binary."; \
+       fi
+
+re:    fclean all
+
+.PHONY: all clean fclean re
+
+################################################################################
+################################################################################
diff --git a/ex01/main.cpp b/ex01/main.cpp
new file mode 100644 (file)
index 0000000..5c9fd7b
--- /dev/null
@@ -0,0 +1,32 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.cpp                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/02/13 14:53:26 by dkaiser           #+#    #+#             */
+/*   Updated: 2025/02/13 14:53:52 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#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;
+}