Browse Source

cpp03 bonus and rework

master^2
narnaud 3 years ago
parent
commit
888b211d4a
  1. 4
      CPP01/ex01/main.cpp
  2. 8
      CPP01/ex01/zombieHorde.cpp
  3. 16
      CPP01/ex02/Makefile
  4. 4
      CPP01/ex02/main.cpp
  5. 10
      CPP01/ex03/HumanA.hpp
  6. 9
      CPP01/ex03/HumanB.cpp
  7. 10
      CPP01/ex03/HumanB.hpp
  8. 12
      CPP01/ex03/Weapon.cpp
  9. 7
      CPP01/ex03/Weapon.hpp
  10. 16
      CPP01/ex04/Makefile
  11. 30
      CPP01/ex04/main.cpp
  12. 11
      CPP01/ex05/Harl.cpp
  13. 56
      CPP01/ex06/Harl.cpp
  14. 32
      CPP01/ex06/Harl.hpp
  15. 20
      CPP01/ex06/Makefile
  16. 20
      CPP01/ex06/main.cpp

4
CPP01/ex01/main.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();

8
CPP01/ex01/zombieHorde.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

16
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

4
CPP01/ex02/main.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}

10
CPP01/ex03/HumanA.hpp

@ -6,14 +6,12 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

9
CPP01/ex03/HumanB.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}

10
CPP01/ex03/HumanB.hpp

@ -6,14 +6,12 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

12
CPP01/ex03/Weapon.cpp

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}

7
CPP01/ex03/Weapon.hpp

@ -6,12 +6,11 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <iostream>
using std::string;
@ -29,5 +28,3 @@ class Weapon
private:
string _type;
};
#endif

16
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

30
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 <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 10:27:42 by narnaud #+# #+# */
/* Updated: 2022/07/18 12:46:16 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
@ -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();

11
CPP01/ex05/Harl.cpp

@ -6,20 +6,15 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{

56
CPP01/ex06/Harl.cpp

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Harl.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
}
}

32
CPP01/ex06/Harl.hpp

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Harl.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/17 11:13:50 by narnaud #+# #+# */
/* Updated: 2022/06/20 09:30:38 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <iostream>
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);
};

20
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

20
CPP01/ex06/main.cpp

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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]);
}
Loading…
Cancel
Save