Compare commits

...

9 Commits

23 changed files with 247 additions and 94 deletions

View File

@ -13,23 +13,12 @@ Zombie::Zombie(const std::string& name)
this->_name = name; this->_name = name;
} }
Zombie::Zombie(const Zombie& src)
{
*this = src;
}
Zombie::~Zombie() Zombie::~Zombie()
{ {
std::cout << "~Zombie()" << std::endl; std::cout << "~Zombie()" << std::endl;
} }
Zombie& Zombie::operator=(const Zombie& src) void Zombie::announce()
{
this->_name = src._name;
return *this;
}
void Zombie::announe()
{ {
std::cout << this->_name << ": BraiiiiiiinnnzzzZ..." << std::endl; std::cout << this->_name << ": BraiiiiiiinnnzzzZ..." << std::endl;
} }

View File

@ -8,14 +8,11 @@ class Zombie
std::string _name; std::string _name;
public: public:
Zombie(const Zombie& src);
Zombie();
Zombie(const std::string& name); Zombie(const std::string& name);
Zombie();
~Zombie(); ~Zombie();
Zombie& operator=(const Zombie& src); void announce(void);
void announe(void);
}; };
void randomChump(std::string name); void randomChump(std::string name);

View File

@ -3,10 +3,10 @@
int main() int main()
{ {
Zombie* jean = newZombie("jean"); Zombie* jean = newZombie("jean");
jean->announe(); jean->announce();
delete jean; delete jean;
Zombie no_name; Zombie no_name;
no_name.announe(); no_name.announce();
return 0; return 0;
} }

View File

@ -4,5 +4,5 @@
void randomChump(std::string name) void randomChump(std::string name)
{ {
Zombie zombie(name); Zombie zombie(name);
zombie.announe(); zombie.announce();
} }

View File

@ -13,23 +13,12 @@ Zombie::Zombie(const std::string& name)
this->_name = name; this->_name = name;
} }
Zombie::Zombie(const Zombie& src)
{
*this = src;
}
Zombie::~Zombie() Zombie::~Zombie()
{ {
std::cout << "~Zombie()" << std::endl; std::cout << "~Zombie()" << std::endl;
} }
Zombie& Zombie::operator=(const Zombie& src) void Zombie::announce()
{
this->_name = src._name;
return *this;
}
void Zombie::announe()
{ {
std::cout << this->_name << ": BraiiiiiiinnnzzzZ..." << std::endl; std::cout << this->_name << ": BraiiiiiiinnnzzzZ..." << std::endl;
} }

View File

@ -8,14 +8,11 @@ class Zombie
std::string _name; std::string _name;
public: public:
Zombie(const Zombie& src);
Zombie(); Zombie();
Zombie(const std::string& name); Zombie(const std::string& name);
~Zombie(); ~Zombie();
Zombie& operator=(const Zombie& src); void announce(void);
void announe(void);
void setName(const std::string& name); void setName(const std::string& name);
}; };

View File

@ -8,7 +8,7 @@ int main()
Zombie* bob = zombieHorde(4, "bob"); Zombie* bob = zombieHorde(4, "bob");
for (size_t i = 0; i < 4; i++) for (size_t i = 0; i < 4; i++)
bob[i].announe(); bob[i].announce();
delete[] bob; delete[] bob;
Zombie* pierre = zombieHorde(-1, "pierre"); Zombie* pierre = zombieHorde(-1, "pierre");
if (pierre == NULL) if (pierre == NULL)

View File

@ -3,34 +3,18 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
HumanA::HumanA(const std::string& name, const Weapon& weapon) HumanA::HumanA(const std::string& name, const Weapon& weapon):
_weapon(weapon)
{ {
this->_name = name; this->_name = name;
this->_weapon = weapon;
}
HumanA::HumanA()
{
std::cout << "HumanA()" << std::endl; std::cout << "HumanA()" << std::endl;
} }
HumanA::HumanA(const HumanA& src)
{
*this = src;
}
HumanA::~HumanA() HumanA::~HumanA()
{ {
std::cout << "~HumanA()" << std::endl; std::cout << "~HumanA()" << std::endl;
} }
HumanA& HumanA::operator=(const HumanA& src)
{
this->_weapon = src._weapon;
this->_name = src._name;
return *this;
}
void HumanA::attack() void HumanA::attack()
{ {
std::cout << this->_name << " attacks with their " << this->_weapon.getType() << std::endl; std::cout << this->_name << " attacks with their " << this->_weapon.getType() << std::endl;

View File

@ -6,17 +6,13 @@
class HumanA class HumanA
{ {
public: public:
HumanA();
HumanA(const std::string& name, const Weapon& weapon); HumanA(const std::string& name, const Weapon& weapon);
HumanA(const HumanA& src);
~HumanA(); ~HumanA();
HumanA& operator=(const HumanA& src);
void attack(); void attack();
private: private:
Weapon _weapon; const Weapon& _weapon;
std::string _name; std::string _name;
}; };

View File

@ -4,18 +4,10 @@
#include <string> #include <string>
HumanB::HumanB(const std::string& name) HumanB::HumanB(const std::string& name)
{
this->_name = name;
}
HumanB::HumanB()
{ {
std::cout << "HumanB()" << std::endl; std::cout << "HumanB()" << std::endl;
} this->_name = name;
this->_weapon = NULL;
HumanB::HumanB(const HumanB& src)
{
*this = src;
} }
HumanB::~HumanB() HumanB::~HumanB()
@ -23,21 +15,19 @@ HumanB::~HumanB()
std::cout << "~HumanB()" << std::endl; std::cout << "~HumanB()" << std::endl;
} }
HumanB& HumanB::operator=(const HumanB& src)
{
this->_weapon = src._weapon;
this->_name = src._name;
return *this;
}
void HumanB::attack() void HumanB::attack()
{ {
std::cout << this->_name << " attacks with their " << this->_weapon.getType() << std::endl; std::cout << this->_name << " attacks with their ";
if (this->_weapon == NULL)
std::cout << "hands";
else
std::cout << this->_weapon->getType();
std::cout << std::endl;
} }
void HumanB::setWeapon(const Weapon &weapon) void HumanB::setWeapon(const Weapon &weapon)
{ {
this->_weapon = weapon; this->_weapon = &weapon;
} }

View File

@ -6,18 +6,14 @@
class HumanB class HumanB
{ {
public: public:
HumanB();
HumanB(const std::string& name); HumanB(const std::string& name);
HumanB(const HumanB& src);
~HumanB(); ~HumanB();
HumanB& operator=(const HumanB& src);
void attack(); void attack();
void setWeapon(const Weapon& weapon); void setWeapon(const Weapon& weapon);
private: private:
Weapon _weapon; const Weapon* _weapon;
std::string _name; std::string _name;
}; };

View File

@ -10,17 +10,19 @@ Weapon::Weapon()
Weapon::Weapon(const Weapon& src) Weapon::Weapon(const Weapon& src)
{ {
std::cout << "Weapon()" << std::endl;
this->_type = src._type; this->_type = src._type;
} }
Weapon::Weapon(const std::string& type) Weapon::Weapon(const std::string& type)
{ {
std::cout << "Weapon()" << std::endl;
this->_type = type; this->_type = type;
} }
Weapon::~Weapon() Weapon::~Weapon()
{ {
std::cout << "Weapon()" << std::endl; std::cout << "~Weapon()" << std::endl;
} }
Weapon& Weapon::operator=(const Weapon &src) Weapon& Weapon::operator=(const Weapon &src)
@ -34,7 +36,7 @@ void Weapon::setType(const std::string& newType)
this->_type = newType; this->_type = newType;
} }
const std::string& Weapon::getType() const std::string& Weapon::getType() const
{ {
return this->_type; return this->_type;
} }

View File

@ -12,7 +12,7 @@ class Weapon
Weapon& operator=(const Weapon& src); Weapon& operator=(const Weapon& src);
const std::string& getType(); const std::string& getType() const;
void setType(const std::string& newType); void setType(const std::string& newType);
private: private:

View File

@ -17,5 +17,7 @@ int main()
club.setType("some other type of club"); club.setType("some other type of club");
jim.attack(); jim.attack();
} }
HumanB jim("Jim");
jim.attack();
return 0; return 0;
} }

View File

@ -8,17 +8,13 @@
static int get_text(const char *path, std::string &text) static int get_text(const char *path, std::string &text)
{ {
std::ifstream file(path); std::ifstream file(path);
std::string line;
if (!file.is_open()) if (!file.is_open())
{ {
std::cerr << "sed: file error" << std::endl; std::cerr << "sed: file error" << std::endl;
return (1); return (1);
} }
while (std::getline(file, line)) std::getline(file, text, '\0');
text += line + "\n";
if (text.size() != 0)
text.erase(text.size() - 1);
file.close(); file.close();
return (0); return (0);
} }
@ -60,7 +56,12 @@ int main(int ac, char **av)
} }
if (get_text(av[1], text)) if (get_text(av[1], text))
return (2); return (2);
std::cout << text << std::endl;
replaced = replace_text(text.c_str(), av[2], av[3]); replaced = replace_text(text.c_str(), av[2], av[3]);
std::cout << replaced << std::endl; std::ofstream file((std::string(av[1]) + std::string(".replace")).c_str());
if (!file.is_open())
{
std::cerr << "sed: file error" << std::endl;
return (1);
}
file << replaced;
} }

25
ex05/Makefile Normal file
View File

@ -0,0 +1,25 @@
CXX = c++
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
SRCDIR = src
OBJDIR = obj
NAME = ex05
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(NAME)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
mkdir -p obj
$(CXX) $(CPPFLAGS) -c $< -o $@
$(NAME): $(OBJS)
$(CXX) $(CPPFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(NAME)
re: fclean all

46
ex05/src/Harl.cpp Normal file
View File

@ -0,0 +1,46 @@
#include "Harl.hpp"
#include <iostream>
void Harl::complain(std::string level)
{
static const HarlMethod methods[4] = {
&Harl::debug,
&Harl::info,
&Harl::warning,
&Harl::error,
};
static const std::string strings[4] = {
"DEBUG",
"INFO",
"WARNING",
"ERROR"
};
for (size_t i = 0; i < 4; i++)
{
if (strings[i] == level)
{
(this->*methods[i])();
return;
}
}
}
void Harl::debug()
{
std::cout << "I love having extra bacon for my 7XL-double-cheese-triple-pickle-special-ketchup burger. I really do !" << std::endl;
}
void Harl::info()
{
std::cout << "I cannot believe adding extra bacon costs more money. You didnt put enough bacon in my burger ! If you did, I wouldnt be asking for more !" << std::endl;
}
void Harl::warning()
{
std::cout << "I think I deserve to have some extra bacon for free. Ive been coming for years whereas you started working here since last month." << std::endl;
}
void Harl::error()
{
std::cout << "This is unacceptable ! I want to speak to the manager now." << std::endl;
}

17
ex05/src/Harl.hpp Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <string>
class Harl
{
private:
void debug();
void info();
void warning();
void error();
public:
void complain(std::string level);
};
typedef void (Harl::*HarlMethod)();

19
ex05/src/main.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "Harl.hpp"
int main()
{
Harl harl;
static const std::string levels[] = {
"DEBUG",
"INFO",
"WARNING",
"GOLEM",
"ERROR",
"PASLU",
"FLEMME",
"PANIC"
};
for (int i = 0; i < 6; i++)
harl.complain(levels[i]);
return 0;
}

25
ex06/Makefile Normal file
View File

@ -0,0 +1,25 @@
CXX = c++
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
SRCDIR = src
OBJDIR = obj
NAME = ex06
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(NAME)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
mkdir -p obj
$(CXX) $(CPPFLAGS) -c $< -o $@
$(NAME): $(OBJS)
$(CXX) $(CPPFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(NAME)
re: fclean all

51
ex06/src/Harl.cpp Normal file
View File

@ -0,0 +1,51 @@
#include "Harl.hpp"
#include <iostream>
void Harl::complain(std::string level)
{
static const std::string strings[4] = {
"DEBUG",
"INFO",
"WARNING",
"ERROR"
};
size_t i = 0;
for (; i < 4; i++)
if (strings[i] == level)
break;
switch(i) {
case 0:
debug();
case 1:
info();
case 2:
warning();
case 3:
error();
break;
default:
std::cout << "Not a level" << std::endl;
break;
}
}
void Harl::debug()
{
std::cout << "I love having extra bacon for my 7XL-double-cheese-triple-pickle-special-ketchup burger. I really do !" << std::endl;
}
void Harl::info()
{
std::cout << "I cannot believe adding extra bacon costs more money. You didnt put enough bacon in my burger ! If you did, I wouldnt be asking for more !" << std::endl;
}
void Harl::warning()
{
std::cout << "I think I deserve to have some extra bacon for free. Ive been coming for years whereas you started working here since last month." << std::endl;
}
void Harl::error()
{
std::cout << "This is unacceptable ! I want to speak to the manager now." << std::endl;
}

17
ex06/src/Harl.hpp Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <string>
class Harl
{
private:
void debug();
void info();
void warning();
void error();
public:
void complain(std::string level);
};
typedef void (Harl::*HarlMethod)();

10
ex06/src/main.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "Harl.hpp"
int main(int argc, char **argv)
{
if (argc <= 1)
return 1;
Harl harl;
harl.complain(argv[1]);
return 0;
}