From 888b211d4ae7279b20986c613d76dbe553fdd4c2 Mon Sep 17 00:00:00 2001 From: narnaud Date: Tue, 19 Jul 2022 09:04:18 +0200 Subject: [PATCH] cpp03 bonus and rework --- CPP01/ex01/main.cpp | 4 +-- CPP01/ex01/zombieHorde.cpp | 8 ++++-- CPP01/ex02/Makefile | 16 +++++++++++ CPP01/ex02/main.cpp | 4 ++- CPP01/ex03/HumanA.hpp | 10 ++----- CPP01/ex03/HumanB.cpp | 9 ++---- CPP01/ex03/HumanB.hpp | 10 ++----- CPP01/ex03/Weapon.cpp | 12 ++++---- CPP01/ex03/Weapon.hpp | 7 ++--- CPP01/ex04/Makefile | 16 +++++++++++ CPP01/ex04/main.cpp | 30 ++++++++++++-------- CPP01/ex05/Harl.cpp | 11 ++------ CPP01/ex06/Harl.cpp | 56 ++++++++++++++++++++++++++++++++++++++ CPP01/ex06/Harl.hpp | 32 ++++++++++++++++++++++ CPP01/ex06/Makefile | 20 ++++++++++++++ CPP01/ex06/main.cpp | 20 ++++++++++++++ 16 files changed, 208 insertions(+), 57 deletions(-) create mode 100644 CPP01/ex02/Makefile create mode 100644 CPP01/ex04/Makefile create mode 100644 CPP01/ex06/Harl.cpp create mode 100644 CPP01/ex06/Harl.hpp create mode 100644 CPP01/ex06/Makefile create mode 100644 CPP01/ex06/main.cpp diff --git a/CPP01/ex01/main.cpp b/CPP01/ex01/main.cpp index c9af313..4bc10b3 100644 --- a/CPP01/ex01/main.cpp +++ b/CPP01/ex01/main.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/14 07:59:29 by narnaud #+# #+# */ -/* Updated: 2022/06/16 12:44:26 by narnaud ### ########.fr */ +/* Updated: 2022/07/18 08:19:08 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ int main(void) { - Zombie *zombs = zombieHorde(ZB_AMOUNT, "Zombie"); + Zombie *zombs = zombieHorde(ZB_AMOUNT, "Z"); for (int i = 0; i < ZB_AMOUNT; i++) zombs[i].announce(); diff --git a/CPP01/ex01/zombieHorde.cpp b/CPP01/ex01/zombieHorde.cpp index c2fd183..85faf50 100644 --- a/CPP01/ex01/zombieHorde.cpp +++ b/CPP01/ex01/zombieHorde.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/14 08:17:02 by narnaud #+# #+# */ -/* Updated: 2022/06/16 12:47:20 by narnaud ### ########.fr */ +/* Updated: 2022/07/18 08:20:03 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,11 @@ Zombie *zombieHorde(int N, string name) int n = 0; while (n < N) { - ret[n].setName(n); + std::stringstream ss; + string s; + ss << name << n; + ss >> s; + ret[n].setName(s); n++; } return (ret); diff --git a/CPP01/ex02/Makefile b/CPP01/ex02/Makefile new file mode 100644 index 0000000..1b3c76f --- /dev/null +++ b/CPP01/ex02/Makefile @@ -0,0 +1,16 @@ +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +brain: main.o + c++ main.o -o brain + +all: brain + +clean: + rm -rf main.o + +fclean: clean + rm -rf brain + +re: fclean all + +.PHONY: brain all clean fclean re diff --git a/CPP01/ex02/main.cpp b/CPP01/ex02/main.cpp index 9626a81..d56b7fa 100644 --- a/CPP01/ex02/main.cpp +++ b/CPP01/ex02/main.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/14 11:01:07 by narnaud #+# #+# */ -/* Updated: 2022/06/14 11:08:21 by narnaud ### ########.fr */ +/* Updated: 2022/07/18 08:31:12 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,9 +23,11 @@ int main(void) cout << &str << endl; cout << stringPTR << endl; + //cout << &stringPTR << endl; cout << &stringREF << endl; cout << str << endl; cout << *stringPTR << endl; + //cout << stringPTR << endl; cout << stringREF << endl; } diff --git a/CPP01/ex03/HumanA.hpp b/CPP01/ex03/HumanA.hpp index ce7ad11..dca0290 100644 --- a/CPP01/ex03/HumanA.hpp +++ b/CPP01/ex03/HumanA.hpp @@ -6,14 +6,12 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/14 12:52:11 by narnaud #+# #+# */ -/* Updated: 2022/06/16 10:33:40 by narnaud ### ########.fr */ +/* Updated: 2022/07/18 10:06:45 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef HUMANA_HPP -# define HUMANA_HPP - -# include "Weapon.hpp" +#pragma once +#include "Weapon.hpp" class HumanA { @@ -24,5 +22,3 @@ class HumanA string _name; Weapon &_weapon; }; - -#endif diff --git a/CPP01/ex03/HumanB.cpp b/CPP01/ex03/HumanB.cpp index 6555eba..e530d1c 100644 --- a/CPP01/ex03/HumanB.cpp +++ b/CPP01/ex03/HumanB.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/16 09:13:28 by narnaud #+# #+# */ -/* Updated: 2022/06/16 10:12:56 by narnaud ### ########.fr */ +/* Updated: 2022/07/18 10:05:32 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,9 +17,7 @@ HumanB::HumanB(string name):_name(name), _weapon(NULL){} HumanB::HumanB(string name, Weapon &weap):_name(name), _weapon(&weap){} - -void HumanB::attack(void) const -{ +void HumanB::attack(void) const { std::cout << _name << " attacks with their "; if (_weapon) cout << _weapon->getType() << endl; @@ -27,7 +25,6 @@ void HumanB::attack(void) const cout << "fists\n"; } -void HumanB::setWeapon(Weapon &weap) -{ +void HumanB::setWeapon(Weapon &weap) { _weapon = &weap; } diff --git a/CPP01/ex03/HumanB.hpp b/CPP01/ex03/HumanB.hpp index b4bef57..d93c6ad 100644 --- a/CPP01/ex03/HumanB.hpp +++ b/CPP01/ex03/HumanB.hpp @@ -6,14 +6,12 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/14 12:54:11 by narnaud #+# #+# */ -/* Updated: 2022/06/16 10:33:28 by narnaud ### ########.fr */ +/* Updated: 2022/07/18 10:07:11 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef HUMANB_HPP -# define HUMANB_HPP - -# include "Weapon.hpp" +#pragma once +#include "Weapon.hpp" class HumanB { @@ -26,5 +24,3 @@ class HumanB string _name; Weapon *_weapon; }; - -#endif diff --git a/CPP01/ex03/Weapon.cpp b/CPP01/ex03/Weapon.cpp index e52f882..ba1d073 100644 --- a/CPP01/ex03/Weapon.cpp +++ b/CPP01/ex03/Weapon.cpp @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/14 12:51:23 by narnaud #+# #+# */ -/* Updated: 2022/06/16 10:08:02 by narnaud ### ########.fr */ +/* Updated: 2022/07/18 10:05:10 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,11 @@ Weapon::Weapon(string type):_type(type){} -const string &Weapon::getType(void) const -{ - return (this->_type); +const string &Weapon::getType(void) const { + return (_type); } -void Weapon::setType(string type) -{ - this->_type = type; +void Weapon::setType(string type) { + _type = type; } diff --git a/CPP01/ex03/Weapon.hpp b/CPP01/ex03/Weapon.hpp index 52a2064..80dedfb 100644 --- a/CPP01/ex03/Weapon.hpp +++ b/CPP01/ex03/Weapon.hpp @@ -6,12 +6,11 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/14 12:50:31 by narnaud #+# #+# */ -/* Updated: 2022/06/16 10:04:50 by narnaud ### ########.fr */ +/* Updated: 2022/07/18 10:07:39 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef WEAPON_HPP -# define WEAPON_HPP +#pragma once # include using std::string; @@ -29,5 +28,3 @@ class Weapon private: string _type; }; - -#endif diff --git a/CPP01/ex04/Makefile b/CPP01/ex04/Makefile new file mode 100644 index 0000000..87472aa --- /dev/null +++ b/CPP01/ex04/Makefile @@ -0,0 +1,16 @@ +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +replaceby: main.o + c++ main.o -o replaceby + +all: replaceby + +clean: + rm -rf main.o + +fclean: clean + rm -rf replaceby + +re: fclean all + +.PHONY: replaceby all clean fclean re diff --git a/CPP01/ex04/main.cpp b/CPP01/ex04/main.cpp index b86caf8..4dff08e 100644 --- a/CPP01/ex04/main.cpp +++ b/CPP01/ex04/main.cpp @@ -1,14 +1,14 @@ -/* - * my_sed(argc,argv) - * filename = argv[1]; - * s1 = argv[2]; - * s2 = argv[3]; - * input::ifstream ifs; - * input = open(filename, std::ifstream::in); - * - * - * - * */ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/07/18 10:27:42 by narnaud #+# #+# */ +/* Updated: 2022/07/18 12:46:16 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ #include @@ -31,10 +31,16 @@ int main(int argc, char **argv) if (argc != 4) return (EXIT_FAILURE); ifstream input(argv[1]); + if (!input.is_open()) { + std::cout << "Invalid file." << std::endl; + return (EXIT_FAILURE); + } oldStr = argv[2]; newStr = argv[3]; - while (getline(input, buffer)) + while (getline(input, buffer)) { text += buffer; + text += '\n'; + } input.close(); fileLen = text.length(); oldLen = oldStr.length(); diff --git a/CPP01/ex05/Harl.cpp b/CPP01/ex05/Harl.cpp index 151aa22..7861201 100644 --- a/CPP01/ex05/Harl.cpp +++ b/CPP01/ex05/Harl.cpp @@ -6,20 +6,15 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/17 11:14:29 by narnaud #+# #+# */ -/* Updated: 2022/06/20 09:30:23 by narnaud ### ########.fr */ +/* Updated: 2022/07/18 13:00:53 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "Harl.hpp" -Harl::Harl (void) { - std::cout << "Harl default constructor called " << std::endl; -} - -Harl::~Harl (void) { +Harl::Harl (void) {} - std::cout << "Harl default destructor called" << std::endl; -} +Harl::~Harl (void) {} void Harl::_debug(void) { diff --git a/CPP01/ex06/Harl.cpp b/CPP01/ex06/Harl.cpp new file mode 100644 index 0000000..42a996e --- /dev/null +++ b/CPP01/ex06/Harl.cpp @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Harl.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/17 11:14:29 by narnaud #+# #+# */ +/* Updated: 2022/07/18 13:00:18 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Harl.hpp" + +Harl::Harl (void) {} + +Harl::~Harl (void) {} + +void Harl::_debug(void) +{ + std::cout << "Debug : ..." << std::endl; +} + +void Harl::_info(void) +{ + std::cout << "Info: ..." << std::endl; +} + +void Harl::_warning(void) +{ + std::cout << "Warning: ..." << std::endl; +} + +void Harl::_error(void) +{ + std::cout << "Error: ..." << std::endl; +} + +void Harl::_complain(string level) { + + string ids = "DIWE"; + int i = 0; + while (ids[i] && ids[i] != level[0]) + i++; + switch (i) { + case 0: + this->_debug(); + case 1: + this->_info(); + case 2: + this->_warning(); + case 3: + this->_error(); + } +} + diff --git a/CPP01/ex06/Harl.hpp b/CPP01/ex06/Harl.hpp new file mode 100644 index 0000000..96baed2 --- /dev/null +++ b/CPP01/ex06/Harl.hpp @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Harl.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/17 11:13:50 by narnaud #+# #+# */ +/* Updated: 2022/06/20 09:30:38 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +using std::string; + +class Harl { + + public: + + Harl (void); + ~Harl (void); + void _complain(string level); + + private: + void _debug(void); + void _info(void); + void _warning(void); + void _error(void); + +}; diff --git a/CPP01/ex06/Makefile b/CPP01/ex06/Makefile new file mode 100644 index 0000000..bbc9a22 --- /dev/null +++ b/CPP01/ex06/Makefile @@ -0,0 +1,20 @@ +NAME = harlFilter +SRCS = main.cpp Harl.cpp +OBJS = ${SRCS:.cpp=.o} + +CXXFLAGS = -std=c++98 -Werror -Wextra -Wall + +$(NAME): $(OBJS) + c++ $(OBJS) -o $(NAME) + +all: $(NAME) + +clean: + rm -rf $(OBJS) + +fclean: clean + rm -rf $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/CPP01/ex06/main.cpp b/CPP01/ex06/main.cpp new file mode 100644 index 0000000..6c91b5a --- /dev/null +++ b/CPP01/ex06/main.cpp @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/17 11:37:33 by narnaud #+# #+# */ +/* Updated: 2022/07/18 12:59:24 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Harl.hpp" + +int main(int argc, char **argv) +{ + Harl bot; + if (argc == 2) + bot._complain(argv[1]); +}