feat(ex02): it works

This commit is contained in:
Khaïs COLIN 2025-05-14 17:17:33 +02:00
parent fce90b9a43
commit 6f17e2e888
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
5 changed files with 251 additions and 0 deletions

1
ex02/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
ex02

137
ex02/Fixed.cpp Normal file
View file

@ -0,0 +1,137 @@
#include "Fixed.hpp"
#include <bitset>
#include <sstream>
const int Fixed::fracbits(8);
Fixed::Fixed() { value = 0; }
Fixed::Fixed(const int val) { value = val << fracbits; }
Fixed::Fixed(const float val) {
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) { this->value = other.value; }
Fixed &Fixed::operator=(const Fixed &other) {
this->setRawBits(other.getRawBits());
return *this;
}
Fixed::~Fixed() {}
int Fixed::getRawBits(void) const { 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<fracbits>(value & (1 << fracbits) - 1);
return result.str();
}
bool Fixed::operator>(const Fixed &other) const {
return this->value > other.value;
}
bool Fixed::operator<(const Fixed &other) const {
return this->value < other.value;
}
bool Fixed::operator>=(const Fixed &other) const {
return this->value >= other.value;
}
bool Fixed::operator<=(const Fixed &other) const {
return this->value <= other.value;
}
bool Fixed::operator==(const Fixed &other) const {
return this->value == other.value;
}
bool Fixed::operator!=(const Fixed &other) const {
return this->value != other.value;
}
const Fixed Fixed::operator+(const Fixed &other) const {
Fixed result = *this;
result.value += other.value;
return result;
}
const Fixed Fixed::operator-(const Fixed &other) const {
Fixed result = *this;
result.value -= other.value;
return result;
}
const Fixed Fixed::operator*(const Fixed &other) const {
Fixed result = *this;
result.value *= other.value;
result.value >>= fracbits;
return result;
}
const Fixed Fixed::operator/(const Fixed &other) const {
Fixed result = *this;
result.value /= other.value;
result.value <<= fracbits;
return result;
}
Fixed Fixed::operator++() {
this->value++;
return *this;
}
Fixed Fixed::operator--() {
this->value--;
return *this;
}
Fixed Fixed::operator++(int) {
Fixed result = *this;
operator++();
return result;
}
Fixed Fixed::operator--(int) {
Fixed result = *this;
operator--();
return result;
}
Fixed &Fixed::min(Fixed &left, Fixed &right) {
if (left < right)
return left;
return right;
}
const Fixed &Fixed::min(const Fixed &left, const Fixed &right) {
if (left < right)
return left;
return right;
}
Fixed &Fixed::max(Fixed &left, Fixed &right) {
if (left > right)
return left;
return right;
}
const Fixed &Fixed::max(const Fixed &left, const Fixed &right) {
if (left > right)
return left;
return right;
}
std::ostream &operator<<(std::ostream &stream, const Fixed &fixed) {
stream << fixed.toFloat();
return stream;
}

51
ex02/Fixed.hpp Normal file
View file

@ -0,0 +1,51 @@
#ifndef FIXED_HPP
#define FIXED_HPP
#include <iostream>
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;
bool operator>(const Fixed &other) const;
bool operator<(const Fixed &other) const;
bool operator>=(const Fixed &other) const;
bool operator<=(const Fixed &other) const;
bool operator==(const Fixed &other) const;
bool operator!=(const Fixed &other) const;
const Fixed operator+(const Fixed &other) const;
const Fixed operator-(const Fixed &other) const;
const Fixed operator*(const Fixed &other) const;
const Fixed operator/(const Fixed &other) const;
Fixed operator++();
Fixed operator--();
Fixed operator++(int);
Fixed operator--(int);
static Fixed &min(Fixed &left, Fixed &right);
static const Fixed &min(const Fixed &left, const Fixed &right);
static Fixed &max(Fixed &left, Fixed &right);
static const Fixed &max(const Fixed &left, const Fixed &right);
friend std::ostream &operator<<(std::ostream &stream, const Fixed &fixed);
private:
int value;
static const int fracbits;
};
#endif

39
ex02/Makefile Normal file
View file

@ -0,0 +1,39 @@
NAME = ex02
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

23
ex02/main.cpp Normal file
View file

@ -0,0 +1,23 @@
#include "Fixed.hpp"
#include <iostream>
int main(void) {
Fixed a;
Fixed fivedotohfive = Fixed(5.05f);
Fixed two = Fixed(2);
std::cout << fivedotohfive.toBin() << std::endl;
std::cout << two.toBin() << std::endl;
Fixed const b(fivedotohfive * two);
std::cout << a << std::endl;
std::cout << ++a << std::endl;
std::cout << a << std::endl;
std::cout << a++ << std::endl;
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << Fixed::max(a, b) << std::endl;
std::cout << b.toBin() << std::endl;
Fixed const c(fivedotohfive / two);
std::cout << c << std::endl;
std::cout << c.toBin() << std::endl;
return 0;
}