feat(ex01): it works
This commit is contained in:
parent
baadd5bddc
commit
fce90b9a43
5 changed files with 163 additions and 0 deletions
1
ex01/.gitignore
vendored
Normal file
1
ex01/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
ex01
|
||||
70
ex01/Fixed.cpp
Normal file
70
ex01/Fixed.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#include "Fixed.hpp"
|
||||
#include <bitset>
|
||||
#include <sstream>
|
||||
|
||||
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<fracbits>(value & (1 << fracbits) - 1);
|
||||
return result.str();
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &stream, const Fixed &fixed) {
|
||||
stream << fixed.toFloat();
|
||||
return stream;
|
||||
}
|
||||
30
ex01/Fixed.hpp
Normal file
30
ex01/Fixed.hpp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#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;
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &stream, const Fixed &fixed);
|
||||
|
||||
private:
|
||||
int value;
|
||||
static const int fracbits;
|
||||
};
|
||||
|
||||
#endif
|
||||
39
ex01/Makefile
Normal file
39
ex01/Makefile
Normal file
|
|
@ -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
|
||||
23
ex01/main.cpp
Normal file
23
ex01/main.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#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;
|
||||
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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue