ex02: fix increment postfix operators
This commit is contained in:
parent
40f419d4e5
commit
fd15edfe1f
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.vscode/settings.json
|
||||||
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"files.associations": {
|
|
||||||
"iosfwd": "cpp"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,6 +1,3 @@
|
|||||||
NAME={project_name}
|
|
||||||
SRC={source_files}
|
|
||||||
include ../common.mk
|
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
# #
|
# #
|
||||||
# :::::::: #
|
# :::::::: #
|
||||||
@ -9,11 +6,12 @@ include ../common.mk
|
|||||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||||
# +#+ #
|
# +#+ #
|
||||||
# Created: 2025/03/24 15:14:58 by whaffman #+# #+# #
|
# Created: 2025/03/24 15:14:58 by whaffman #+# #+# #
|
||||||
# Updated: 2025/03/24 15:15:17 by whaffman ######## odam.nl #
|
# Updated: 2025/03/25 10:13:11 by whaffman ######## odam.nl #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
NAME= Fixed01
|
NAME = Fixed01
|
||||||
SRC= Fixed.cpp main.cpp
|
SRC = Fixed.cpp main.cpp
|
||||||
|
|
||||||
-include ../common.mk
|
-include ../common.mk
|
||||||
|
|
||||||
|
|||||||
@ -6,12 +6,13 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */
|
/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */
|
||||||
/* Updated: 2025/03/25 10:05:11 by whaffman ######## odam.nl */
|
/* Updated: 2025/03/25 11:39:39 by whaffman ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Fixed.hpp"
|
#include "Fixed.hpp"
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
Fixed::Fixed()
|
Fixed::Fixed()
|
||||||
{
|
{
|
||||||
@ -25,6 +26,18 @@ Fixed::Fixed(const Fixed &fixed)
|
|||||||
*this = fixed;
|
*this = fixed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Fixed::Fixed(const float value)
|
||||||
|
{
|
||||||
|
std::cout << "Float constructor called" << std::endl;
|
||||||
|
fixedPointValue = (int)(value * (1 << fractionalBits));
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed::Fixed(const int value)
|
||||||
|
{
|
||||||
|
std::cout << "Int constructor called" << std::endl;
|
||||||
|
fixedPointValue = value * (1 << fractionalBits);
|
||||||
|
}
|
||||||
|
|
||||||
Fixed::~Fixed()
|
Fixed::~Fixed()
|
||||||
{
|
{
|
||||||
std::cout << "Destructor called" << std::endl;
|
std::cout << "Destructor called" << std::endl;
|
||||||
@ -32,13 +45,14 @@ Fixed::~Fixed()
|
|||||||
Fixed &Fixed::operator=(const Fixed &fixed)
|
Fixed &Fixed::operator=(const Fixed &fixed)
|
||||||
{
|
{
|
||||||
std::cout << "Copy assignment operator called" << std::endl;
|
std::cout << "Copy assignment operator called" << std::endl;
|
||||||
fixedPointValue = fixed.getRawBits();
|
fixedPointValue = fixed.fixedPointValue;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &out, const Fixed &fixed)
|
std::ostream &operator<<(std::ostream &out, const Fixed &fixed)
|
||||||
{
|
{
|
||||||
out << fixed.toFloat();
|
out << fixed.toFloat();
|
||||||
|
return (out);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Fixed::getRawBits(void) const
|
int Fixed::getRawBits(void) const
|
||||||
@ -60,5 +74,5 @@ float Fixed::toFloat(void) const
|
|||||||
|
|
||||||
int Fixed::toInt(void) const
|
int Fixed::toInt(void) const
|
||||||
{
|
{
|
||||||
return (int)fixedPointValue >> fractionalBits;
|
return (int) std::roundf( (float)fixedPointValue / (1 << fractionalBits));
|
||||||
}
|
}
|
||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
/* +#+ */
|
/* +#+ */
|
||||||
/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */
|
/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */
|
||||||
/* Updated: 2025/03/25 09:59:15 by whaffman ######## odam.nl */
|
/* Updated: 2025/03/25 11:40:20 by whaffman ######## odam.nl */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ int main(void)
|
|||||||
Fixed const b(10);
|
Fixed const b(10);
|
||||||
Fixed const c(42.42f);
|
Fixed const c(42.42f);
|
||||||
Fixed const d(b);
|
Fixed const d(b);
|
||||||
a = Fixed(1234.4321f);
|
a = Fixed(1234.6321f);
|
||||||
|
|
||||||
std::cout << "a is " << a << std::endl;
|
std::cout << "a is " << a << std::endl;
|
||||||
std::cout << "b is " << b << std::endl;
|
std::cout << "b is " << b << std::endl;
|
||||||
|
|||||||
@ -6,11 +6,11 @@
|
|||||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||||
# +#+ #
|
# +#+ #
|
||||||
# Created: 2025/03/24 15:14:58 by whaffman #+# #+# #
|
# Created: 2025/03/24 15:14:58 by whaffman #+# #+# #
|
||||||
# Updated: 2025/03/24 15:15:48 by whaffman ######## odam.nl #
|
# Updated: 2025/03/25 11:42:39 by whaffman ######## odam.nl #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
NAME=
|
NAME=Fixed02
|
||||||
SRC=
|
SRC=main.cpp Fixed.cpp
|
||||||
|
|
||||||
-include ../common.mk
|
-include ../common.mk
|
||||||
|
|||||||
57
ex02/inc/Fixed.hpp
Normal file
57
ex02/inc/Fixed.hpp
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* Fixed.hpp :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */
|
||||||
|
/* Updated: 2025/03/25 15:23:47 by whaffman ######## odam.nl */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
class Fixed {
|
||||||
|
public:
|
||||||
|
Fixed();
|
||||||
|
Fixed(const int value);
|
||||||
|
Fixed(const float value);
|
||||||
|
Fixed(const Fixed &fixed);
|
||||||
|
~Fixed();
|
||||||
|
|
||||||
|
Fixed &operator=(const Fixed &fixed);
|
||||||
|
Fixed operator+(const Fixed &fixed) const;
|
||||||
|
Fixed operator-(const Fixed &fixed) const;
|
||||||
|
Fixed operator*(const Fixed &fixed) const;
|
||||||
|
Fixed operator/(const Fixed &fixed) const;
|
||||||
|
|
||||||
|
Fixed &operator++();
|
||||||
|
Fixed operator++(int);
|
||||||
|
Fixed &operator--();
|
||||||
|
Fixed operator--(int);
|
||||||
|
|
||||||
|
bool operator>(const Fixed &fixed) const;
|
||||||
|
bool operator<(const Fixed &fixed) const;
|
||||||
|
bool operator>=(const Fixed &fixed) const;
|
||||||
|
bool operator<=(const Fixed &fixed) const;
|
||||||
|
bool operator==(const Fixed &fixed) const;
|
||||||
|
bool operator!=(const Fixed &fixed) const;
|
||||||
|
|
||||||
|
static Fixed &min(Fixed &a, Fixed &b);
|
||||||
|
static const Fixed &min(const Fixed &a, const Fixed &b);
|
||||||
|
static Fixed &max(Fixed &a, Fixed &b);
|
||||||
|
static const Fixed &max(const Fixed &a, const Fixed &b);
|
||||||
|
|
||||||
|
int getRawBits(void) const;
|
||||||
|
void setRawBits(int const raw);
|
||||||
|
float toFloat(void) const;
|
||||||
|
int toInt(void) const;
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
int fixedPointValue;
|
||||||
|
static const int fractionalBits = 8;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::ostream &operator<<(std::ostream &out, const Fixed &fixed);
|
||||||
|
|
||||||
|
|
||||||
179
ex02/src/Fixed.cpp
Normal file
179
ex02/src/Fixed.cpp
Normal file
@ -0,0 +1,179 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* Fixed.cpp :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */
|
||||||
|
/* Updated: 2025/03/25 15:28:16 by whaffman ######## odam.nl */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include "Fixed.hpp"
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
Fixed::Fixed()
|
||||||
|
{
|
||||||
|
fixedPointValue = 0;
|
||||||
|
std::cout << "Default constructor called" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed::Fixed(const Fixed &fixed)
|
||||||
|
{
|
||||||
|
std::cout << "Copy constructor called" << std::endl;
|
||||||
|
*this = fixed;
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed::Fixed(const float value)
|
||||||
|
{
|
||||||
|
std::cout << "Float constructor called" << std::endl;
|
||||||
|
fixedPointValue = (int)(value * (1 << fractionalBits));
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed::Fixed(const int value)
|
||||||
|
{
|
||||||
|
std::cout << "Int constructor called" << std::endl;
|
||||||
|
fixedPointValue = value * (1 << fractionalBits);
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed::~Fixed()
|
||||||
|
{
|
||||||
|
std::cout << "Destructor called" << std::endl;
|
||||||
|
}
|
||||||
|
Fixed &Fixed::operator=(const Fixed &fixed)
|
||||||
|
{
|
||||||
|
std::cout << "Copy assignment operator called" << std::endl;
|
||||||
|
fixedPointValue = fixed.fixedPointValue;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream &operator<<(std::ostream &out, const Fixed &fixed)
|
||||||
|
{
|
||||||
|
out << fixed.toFloat();
|
||||||
|
return (out);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Fixed Fixed::operator+(const Fixed &fixed) const
|
||||||
|
{
|
||||||
|
return (Fixed(toFloat() + fixed.toFloat()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed Fixed::operator-(const Fixed &fixed) const
|
||||||
|
{
|
||||||
|
return (Fixed(toFloat() - fixed.toFloat()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed Fixed::operator*(const Fixed &fixed) const
|
||||||
|
{
|
||||||
|
return (Fixed(toFloat() * fixed.toFloat()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed Fixed::operator/(const Fixed &fixed) const
|
||||||
|
{
|
||||||
|
return (Fixed(toFloat() / fixed.toFloat()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Fixed &Fixed::operator++()
|
||||||
|
{
|
||||||
|
fixedPointValue++;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed Fixed::operator++(int)
|
||||||
|
{
|
||||||
|
fixedPointValue++;
|
||||||
|
return Fixed(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed &Fixed::operator--()
|
||||||
|
{
|
||||||
|
fixedPointValue--;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed Fixed::operator--(int)
|
||||||
|
{
|
||||||
|
fixedPointValue--;
|
||||||
|
return Fixed(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Fixed::operator>(const Fixed &fixed) const
|
||||||
|
{
|
||||||
|
return (fixedPointValue > fixed.fixedPointValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Fixed::operator<(const Fixed &fixed) const
|
||||||
|
{
|
||||||
|
return (fixedPointValue < fixed.fixedPointValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Fixed::operator>=(const Fixed &fixed) const
|
||||||
|
{
|
||||||
|
return (fixedPointValue >= fixed.fixedPointValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Fixed::operator<=(const Fixed &fixed) const
|
||||||
|
{
|
||||||
|
return (fixedPointValue <= fixed.fixedPointValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Fixed::operator==(const Fixed &fixed) const
|
||||||
|
{
|
||||||
|
return (fixedPointValue == fixed.fixedPointValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Fixed::operator!=(const Fixed &fixed) const
|
||||||
|
{
|
||||||
|
return (fixedPointValue != fixed.fixedPointValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Fixed::getRawBits(void) const
|
||||||
|
{
|
||||||
|
std::cout << "getRawBits member function called" << std::endl;
|
||||||
|
return fixedPointValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Fixed::setRawBits(int const raw)
|
||||||
|
{
|
||||||
|
std::cout << "setRawBits member function called" << std::endl;
|
||||||
|
fixedPointValue = raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
float Fixed::toFloat(void) const
|
||||||
|
{
|
||||||
|
return (float)fixedPointValue / (1 << fractionalBits);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Fixed::toInt(void) const
|
||||||
|
{
|
||||||
|
return (int) std::roundf( (float)fixedPointValue / (1 << fractionalBits));
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixed &Fixed::min(Fixed &a, Fixed &b)
|
||||||
|
{
|
||||||
|
if (a < b)
|
||||||
|
return a;
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
const Fixed &Fixed::min(const Fixed &a, const Fixed &b)
|
||||||
|
{
|
||||||
|
if (a < b)
|
||||||
|
return a;
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
Fixed &Fixed::max(Fixed &a, Fixed &b)
|
||||||
|
{
|
||||||
|
if (a > b)
|
||||||
|
return a;
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
const Fixed &Fixed::max(const Fixed &a, const Fixed &b)
|
||||||
|
{
|
||||||
|
if (a > b)
|
||||||
|
return a;
|
||||||
|
return b;
|
||||||
|
}
|
||||||
45
ex02/src/main.cpp
Normal file
45
ex02/src/main.cpp
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* :::::::: */
|
||||||
|
/* main.cpp :+: :+: */
|
||||||
|
/* +:+ */
|
||||||
|
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||||
|
/* +#+ */
|
||||||
|
/* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */
|
||||||
|
/* Updated: 2025/03/25 15:26:00 by whaffman ######## odam.nl */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#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.6321f);
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
Fixed a;
|
||||||
|
Fixed const b(Fixed(5.05f) * Fixed(2));
|
||||||
|
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;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user