diff --git a/ex01/.gitignore b/ex01/.gitignore new file mode 100644 index 0000000..b678b9e --- /dev/null +++ b/ex01/.gitignore @@ -0,0 +1 @@ +ex01 diff --git a/ex01/Fixed.cpp b/ex01/Fixed.cpp new file mode 100644 index 0000000..eb610b8 --- /dev/null +++ b/ex01/Fixed.cpp @@ -0,0 +1,70 @@ +#include "Fixed.hpp" +#include +#include + +const int Fixed::fracbits(8); + +Fixed::Fixed() { + std::cout << "Default constructor called" << std::endl; + value = 0; +} + +Fixed::Fixed(const int val) { + std::cout << "Int constructor called with " << val << std::endl; + value = val << fracbits; +} + +Fixed::Fixed(const float val) { + std::cout << "Float constructor called with " << val << std::endl; + const static int factor = 1 << fracbits; + float temp = (val * factor); + if (temp >= 0) + temp += 0.5f; + else + temp -= 0.5f; + value = (int)(temp); +} + +Fixed::Fixed(const Fixed &other) { + std::cout << "Copy constructor called" << std::endl; + this->value = other.value; +} + +Fixed &Fixed::operator=(const Fixed &other) { + std::cout << "Copy assignment operator called" << std::endl; + this->setRawBits(other.getRawBits()); + return *this; +} + +Fixed::~Fixed() { std::cout << "Destructor called" << std::endl; } + +int Fixed::getRawBits(void) const { + std::cout << "getRawBits member function called" << std::endl; + return value; +} + +void Fixed::setRawBits(int const raw) { value = raw; } + +float Fixed::toFloat(void) const { + const static int factor = 1 << fracbits; + float result = (float)value / factor; + return (result); +} + +int Fixed::toInt(void) const { + int result = (value >> fracbits); + return (result); +} + +std::string Fixed::toBin(void) const { + std::stringstream result; + result << std::bitset<32 - fracbits>(value >> fracbits); + result << "."; + result << std::bitset(value & (1 << fracbits) - 1); + return result.str(); +} + +std::ostream &operator<<(std::ostream &stream, const Fixed &fixed) { + stream << fixed.toFloat(); + return stream; +} diff --git a/ex01/Fixed.hpp b/ex01/Fixed.hpp new file mode 100644 index 0000000..87128cc --- /dev/null +++ b/ex01/Fixed.hpp @@ -0,0 +1,30 @@ +#ifndef FIXED_HPP +#define FIXED_HPP + +#include + +class Fixed { +public: + Fixed(); + Fixed(const int val); + Fixed(const float val); + Fixed(const Fixed &other); + Fixed &operator=(const Fixed &other); + ~Fixed(); + + int getRawBits(void) const; + void setRawBits(int const raw); + + float toFloat(void) const; + int toInt(void) const; + + std::string toBin(void) const; + + friend std::ostream &operator<<(std::ostream &stream, const Fixed &fixed); + +private: + int value; + static const int fracbits; +}; + +#endif diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..3989714 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,39 @@ +NAME = ex01 +ifeq ($(CPPFLAGS),) + CPPFLAGS = -Wall -Wextra -Werror -std=c++98 -g +endif +ifeq ($(CXX),) + CXX = c++ +endif +# g++ is the default on 42 computers +ifeq ($(CXX),g++) + CXX = c++ +endif +srcs = \ + Fixed.cpp \ + +main_objs = main.o $(srcs:.cpp=.o) +all_objs = $(main_objs) +deps = $(all_objs:.o=.d) + +all: $(NAME) + +-include $(deps) + +$(NAME): $(main_objs) + $(CXX) $(CPPFLAGS) -o $@ $^ + +%.o: %.cpp + $(CXX) -c $(CPPFLAGS) -o $*.o $*.cpp + $(CXX) -MM $(CPPFLAGS) -MT $*.o $*.cpp > $*.d + +clean: + find . -name '*.o' -print -delete + find . -name '*.d' -print -delete + +fclean: clean + rm -f $(NAME) + +re: + +make fclean + +make all diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..3b2e08b --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,23 @@ +#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; + std::cout << "a is " << a.toBin() << " as binary" << std::endl; + std::cout << "b is " << b.toBin() << " as binary" << std::endl; + std::cout << "c is " << c.toBin() << " as binary" << std::endl; + std::cout << "d is " << d.toBin() << " as binary" << std::endl; + return 0; +}