Skip to content

Commit

Permalink
Added 'make run' to 02
Browse files Browse the repository at this point in the history
  • Loading branch information
LithiumOx committed Apr 25, 2024
1 parent 7e0d36a commit 286c7f2
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 21 deletions.
28 changes: 28 additions & 0 deletions .zed/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Static tasks configuration.
//
// Example:
[
{
"label": "Make",
"command": "make",
//"args": [],
// Env overrides for the command, will be appended to the terminal's environment from the settings.
"env": { "foo": "bar" },
// Current working directory to spawn the command into, defaults to current project root.
//"cwd": "/path/to/working/directory",
// Whether to use a new terminal tab or reuse the existing one to spawn the process, defaults to `false`.
"use_new_terminal": false,
// Whether to allow multiple instances of the same task to be run, or rather wait for the existing ones to finish, defaults to `false`.
"allow_concurrent_runs": false,
// What to do with the terminal pane and tab, after the command was started:
// * `always` — always show the terminal pane, add and focus the corresponding task's tab in it (default)
// * `never` — avoid changing current terminal pane focus, but still add/reuse the task's tab there
"reveal": "never"
},
{
"label": "Run",
"command": "make run",
"use_new_terminal": false,
"reveal": "never"
}
]
7 changes: 7 additions & 0 deletions 00/ex00/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ re: fclean all
format:
@clang-format -i $(SRC)

run: all
@printf "\n🤖 00/ex00 $(NAME) output:\n\n"
./$(NAME) "shhhhh... I think the students are asleep..."
./$(NAME) Damnit " ! " "Sorry students, I thought this thing was off."
./$(NAME) "This is a test"
./$(NAME)

.PHONY: all clean fclean re
5 changes: 5 additions & 0 deletions 00/ex01/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ re: fclean all
format:
@clang-format -i $(SRC) $(HEADERS)

run: all
@printf "\n🤖 00/ex01 $(NAME) output:\n\n"
@printf "\n\033[0;32m Cannot run phonebook automatically... \033[0m\n"
@printf "\033[0;32m Trust me phonebook is running just fine 🙂 \033[0m\n"

.PHONY: all clean fclean re
1 change: 1 addition & 0 deletions 01/ex03/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>

#include "HumanA.hpp"
#include "HumanB.hpp"

Expand Down
12 changes: 6 additions & 6 deletions 02/ex00/Fixed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

#include <iostream>

Fixed::Fixed() : _value(0) { std::cout << "Default constructor called" << std::endl; }
Fixed::Fixed() : _value(0) { std::cout << BLUE << "Default constructor called" << RESET << std::endl; }

Fixed::Fixed(const Fixed &fixed) {
std::cout << "Copy constructor called" << std::endl;
std::cout << BLUE << "Copy constructor called" << std::endl;
*this = fixed;
}

Fixed::~Fixed() { std::cout << "Destructor called" << std::endl; }
Fixed::~Fixed() { std::cout << RED << "Destructor called" << RESET << std::endl; }

Fixed &Fixed::operator=(const Fixed &fixed) {
std::cout << "Assignation operator called" << std::endl;
std::cout << YELLOW << "Assignation operator called" << RESET << std::endl;
_value = fixed.getRawBits();
return *this;
}

int Fixed::getRawBits() const {
std::cout << "getRawBits member function called" << std::endl;
std::cout << TEAL << "getRawBits member function called" << RESET << std::endl;
return _value;
}

void Fixed::setRawBits(int const raw) {
std::cout << "setRawBits member function called" << std::endl;
std::cout << TEAL << "setRawBits member function called" << RESET << std::endl;
_value = raw;
}
12 changes: 8 additions & 4 deletions 02/ex00/Fixed.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#ifndef __FIXED_H__
#define __FIXED_H__
#pragma once

#define BLUE "\033[34m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define RED "\033[31m"
#define TEAL "\033[36m"
#define RESET "\033[0m"

class Fixed {
public:
Expand All @@ -14,5 +20,3 @@ class Fixed {
int _value;
static const int _fractionalBits = 8;
};

#endif
4 changes: 4 additions & 0 deletions 02/ex00/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ re: fclean all
format:
@clang-format -i $(SRC) $(HEADERS)

run: all
@printf "\n🤖 02/ex00 $(NAME) output:\n\n"
@./$(NAME)

.PHONY: all clean fclean re
16 changes: 8 additions & 8 deletions 02/ex01/Fixed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@
#include <cmath>
#include <iostream>

Fixed::Fixed() : _value(0) { std::cout << "Default constructor called" << std::endl; }
Fixed::Fixed() : _value(0) { std::cout << BLUE << "Default constructor called" << RESET << std::endl; }

Fixed::Fixed(const Fixed &fixed) {
std::cout << "Copy constructor called" << std::endl;
std::cout << BLUE << "Copy constructor called" << RESET << std::endl;
*this = fixed;
}

Fixed::Fixed(const int value) {
std::cout << "Int constructor called" << std::endl;
std::cout << BLUE << "Int constructor called" << RESET << std::endl;
_value = value << _fractionalBits;
}

Fixed::Fixed(const float value) {
std::cout << "Float constructor called" << std::endl;
std::cout << BLUE << "Float constructor called" << RESET << std::endl;
_value = static_cast<int>(std::roundf(value * (1 << _fractionalBits)));
}

Fixed::~Fixed() { std::cout << "Destructor called" << std::endl; }
Fixed::~Fixed() { std::cout << RED << "Destructor called" << RESET << std::endl; }

Fixed &Fixed::operator=(const Fixed &fixed) {
std::cout << "Assignation operator called" << std::endl;
std::cout << YELLOW << "Assignation operator called" << RESET << std::endl;
_value = fixed.getRawBits();
return *this;
}

int Fixed::getRawBits() const {
std::cout << "getRawBits member function called" << std::endl;
std::cout << TEAL << "getRawBits member function called" << RESET << std::endl;
return _value;
}

void Fixed::setRawBits(int const raw) {
std::cout << "setRawBits member function called" << std::endl;
std::cout << TEAL << "setRawBits member function called" << RESET << std::endl;
_value = raw;
}

Expand Down
7 changes: 7 additions & 0 deletions 02/ex01/Fixed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

#include <ostream>

#define BLUE "\033[34m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define RED "\033[31m"
#define TEAL "\033[36m"
#define RESET "\033[0m"

class Fixed {
public:
Fixed();
Expand Down
3 changes: 3 additions & 0 deletions 02/ex01/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ re: fclean all
format:
@clang-format -i $(SRC) $(HEADERS)

run: all
@printf "\n🤖 02/ex01 $(NAME) output:\n\n"
@./$(NAME)
.PHONY: all clean fclean re
2 changes: 2 additions & 0 deletions 02/ex01/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ int main(void) {
Fixed const c(42.42f);
Fixed const d(b);
a = Fixed(1234.4321f);
std::cout << "\nValues start off at:\n" << std::endl;
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 << "\nValues after conversion:\n" << 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;
Expand Down
9 changes: 6 additions & 3 deletions 02/ex02/Fixed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
#include <cmath>
#include <iostream>

Fixed::Fixed() : _value(0) {}
Fixed::Fixed() : _value(0) { std::cout << BLUE << "Default constructor called" << RESET << std::endl; }

Fixed::Fixed(const Fixed &fixed) { *this = fixed; }
Fixed::Fixed(const Fixed &fixed) {
*this = fixed;
std::cout << BLUE << "Copy constructor called" << RESET << std::endl;
}

Fixed::Fixed(const int value) { _value = value << _fractionalBits; }

Fixed::Fixed(const float value) { _value = static_cast<int>(std::roundf(value * (1 << _fractionalBits))); }

Fixed::~Fixed() { std::cout << "Destructor called" << std::endl; }
Fixed::~Fixed() { std::cout << RED << "Destructor called" << RESET << std::endl; }

Fixed &Fixed::operator=(const Fixed &fixed) {
_value = fixed.getRawBits();
Expand Down
7 changes: 7 additions & 0 deletions 02/ex02/Fixed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

#include <ostream>

#define BLUE "\033[34m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define RED "\033[31m"
#define TEAL "\033[36m"
#define RESET "\033[0m"

class Fixed {
public:
Fixed();
Expand Down
4 changes: 4 additions & 0 deletions 02/ex02/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ re: fclean all
format:
@clang-format -i $(SRC) $(HEADERS)

run: all
@printf "\n🤖 02/ex02 $(NAME) output:\n\n"
@./$(NAME)

.PHONY: all clean fclean re
11 changes: 11 additions & 0 deletions 02/ex02/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ int main(void) {
Fixed const c(42.42f);
Fixed const d(b);
a = Fixed(1234.4321f);
std::cout << "Initial values\n" << std::endl;
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 << "\nChecking toInt function\n" << 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 << "\nChecking increment and decrement operators\n" << std::endl;
std::cout << "a starts at 10" << std::endl;
a = Fixed(10);
std::cout << "a is incremented by 1" << std::endl;
Expand All @@ -30,6 +33,7 @@ int main(void) {
a = --a;
std::cout << "a is " << a << std::endl;

std::cout << "\nChecking arithmetic operators\n" << std::endl;
a = a / Fixed(2);
std::cout << "a is " << a << std::endl;
a = a * Fixed(2);
Expand All @@ -39,12 +43,19 @@ int main(void) {
a = a - Fixed(2);
std::cout << "a is " << a << std::endl;

std::cout << "\nChecking comparison operators\n" << std::endl;
if (a > Fixed(2)) std::cout << "a is greater than 2" << std::endl;
else { std::cout << "a is less than 2" << std::endl;}
if (a < Fixed(2)) std::cout << "a is less than 2" << std::endl;
else { std::cout << "a is greater than 2" << std::endl;}
if (a >= Fixed(2)) std::cout << "a is greater or equal to 2" << std::endl;
else { std::cout << "a is less than 2" << std::endl;}
if (a <= Fixed(2)) std::cout << "a is less or equal to 2" << std::endl;
else { std::cout << "a is greater than 2" << std::endl;}
if (a == Fixed(2)) std::cout << "a is equal to 2" << std::endl;
else { std::cout << "a is not equal to 2" << std::endl;}
if (a != Fixed(2)) std::cout << "a is not equal to 2" << std::endl;
else { std::cout << "a is equal to 2" << std::endl;}

return 0;
}

0 comments on commit 286c7f2

Please sign in to comment.