Compare commits

...

14 Commits

Author SHA1 Message Date
35cbcfd02d g la chiassss 2023-07-31 18:03:40 +02:00
cebe9f6ea7 ex00: remove useless func and rename announe to announce 2023-07-31 17:50:17 +02:00
cc5928a671 ex00: remove useless func and rename announe to announce 2023-07-31 17:50:07 +02:00
5522ebf3ba ex05: remove const to respect the subject 2023-07-31 17:48:23 +02:00
a7e4a9cd75 sed: fix: remove \n at the end 2023-07-31 17:46:09 +02:00
7a10311d56 fix 2023-07-31 15:28:10 +02:00
42a06c46cd ex04: remove print and write to a file 2023-07-31 14:45:04 +02:00
efa91c0cb5 fix: ex03 2023-07-19 23:26:04 +02:00
eb2b035b75 add: ex05 2023-07-18 03:28:41 +02:00
636365d65c d 2023-07-18 03:20:06 +02:00
5d687d7c28 add: ex04 2023-07-18 03:20:06 +02:00
3ef324e5e1 recration of ex03 2023-07-18 03:20:06 +02:00
98ece4839e fix: ex02 2023-07-18 03:20:00 +02:00
76d822b831 add: ex02 2023-07-18 03:18:33 +02:00
27 changed files with 219 additions and 208 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

@ -1,15 +1,17 @@
#include "Zombie.hpp" #include "Zombie.hpp"
#include <cstddef>
int main() int main()
{ {
Zombie* jean = zombieHorde(2, "jean"); Zombie* jean = zombieHorde(2, "jean");
delete[] jean; delete[] jean;
Zombie* bob = zombieHorde(-1, "bob"); Zombie* bob = zombieHorde(4, "bob");
if (bob == NULL) for (size_t i = 0; i < 4; i++)
return 1; bob[i].announce();
bob->announe();
delete[] bob; delete[] bob;
Zombie* pierre = zombieHorde(-1, "pierre");
if (pierre == NULL)
return 1;
return 0; return 0;
} }

View File

@ -7,9 +7,6 @@ Zombie* zombieHorde(int N, std::string name)
return NULL; return NULL;
Zombie* zombies = new Zombie[N]; Zombie* zombies = new Zombie[N];
for (int i = 0; i < N; i++) for (int i = 0; i < N; i++)
{
zombies[i].setName(name); zombies[i].setName(name);
zombies[i].announe();
}
return zombies; return zombies;
} }

View File

@ -2,7 +2,7 @@ CXX = c++
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
SRCDIR = src SRCDIR = src
OBJDIR = obj OBJDIR = obj
NAME = ex00 NAME = ex02
SRCS = $(wildcard $(SRCDIR)/*.cpp) SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS)) OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))

View File

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

View File

View File

@ -1,17 +1,21 @@
#include "HumanA.hpp" #include "HumanA.hpp"
#include "Weapon.hpp"
#include <string>
#include <iostream>
HumanA::HumanA(std::string name, Weapon &weapon) : weapon(weapon) #include <iostream>
#include <string>
HumanA::HumanA(const std::string& name, const Weapon& weapon):
_weapon(weapon)
{ {
this->name = name; this->_name = name;
std::cout << "HumanA()" << std::endl;
} }
HumanA::~HumanA() HumanA::~HumanA()
{}
void HumanA::attack()
{ {
std::cout << this->name << " attacks with their " << this->weapon.getType() << std::endl; std::cout << "~HumanA()" << std::endl;
}
void HumanA::attack()
{
std::cout << this->_name << " attacks with their " << this->_weapon.getType() << std::endl;
} }

View File

@ -1,19 +1,18 @@
#ifndef HUMANA_HPP #pragma once
# define HUMANA_HPP
# include "Weapon.hpp" #include "Weapon.hpp"
# include <string> #include <string>
class HumanA class HumanA
{ {
private: public:
Weapon &weapon; HumanA(const std::string& name, const Weapon& weapon);
std::string name;
public:
HumanA(std::string name, Weapon &weapon);
~HumanA(); ~HumanA();
void attack(); void attack();
};
#endif private:
const Weapon& _weapon;
std::string _name;
};

View File

@ -1,29 +1,33 @@
#include "HumanB.hpp" #include "HumanB.hpp"
#include "Weapon.hpp"
#include <cstddef>
#include <string>
#include <iostream>
HumanB::HumanB(std::string name) #include <iostream>
#include <string>
HumanB::HumanB(const std::string& name)
{ {
this->weapon = NULL; std::cout << "HumanB()" << std::endl;
this->name = name; this->_name = name;
this->_weapon = NULL;
} }
HumanB::~HumanB() HumanB::~HumanB()
{}
void HumanB::attack()
{ {
if (this->weapon == NULL) std::cout << "~HumanB()" << std::endl;
{
std::cerr << this->name << " can't attack without weapon" << std::endl;
return;
}
std::cout << this->name << " attacks with their " << this->weapon->getType() << std::endl;
} }
void HumanB::setWeapon(Weapon& new_weapon) void HumanB::attack()
{ {
this->weapon = &new_weapon; 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)
{
this->_weapon = &weapon;
}

View File

@ -1,19 +1,19 @@
#ifndef HUMANB_HPP #pragma once
# define HUMANB_HPP
# include "Weapon.hpp" #include "Weapon.hpp"
# include <string> #include <string>
class HumanB class HumanB
{ {
private: public:
Weapon* weapon; HumanB(const std::string& name);
std::string name;
public:
HumanB(std::string name);
~HumanB(); ~HumanB();
void setWeapon(Weapon& new_weapon); void attack();
void attack(); void setWeapon(const Weapon& weapon);
private:
const Weapon* _weapon;
std::string _name;
}; };
#endif

View File

@ -1,24 +1,42 @@
#include "Weapon.hpp" #include "Weapon.hpp"
#include <string>
Weapon::Weapon(std::string type) #include <iostream>
{ #include <string>
this->type = type;
}
Weapon::Weapon() Weapon::Weapon()
{ {
std::cout << "Weapon()" << std::endl;
}
Weapon::Weapon(const Weapon& src)
{
std::cout << "Weapon()" << std::endl;
this->_type = src._type;
}
Weapon::Weapon(const std::string& type)
{
std::cout << "Weapon()" << std::endl;
this->_type = type;
} }
Weapon::~Weapon() Weapon::~Weapon()
{}
std::string Weapon::getType()
{ {
return (this->type); std::cout << "~Weapon()" << std::endl;
} }
void Weapon::setType(std::string new_type) Weapon& Weapon::operator=(const Weapon &src)
{ {
this->type = new_type; this->_type = src._type;
return *this;
}
void Weapon::setType(const std::string& newType)
{
this->_type = newType;
}
const std::string& Weapon::getType() const
{
return this->_type;
} }

View File

@ -1,18 +1,20 @@
#ifndef WEAPON_HPP #pragma once
# define WEAPON_HPP
# include <string> #include <string>
class Weapon class Weapon
{ {
private: public:
std::string type;
public:
Weapon(std::string type);
Weapon(); Weapon();
Weapon(const Weapon& src);
Weapon(const std::string& type);
~Weapon(); ~Weapon();
std::string getType(); Weapon& operator=(const Weapon& src);
void setType(std::string new_type);
const std::string& getType() const;
void setType(const std::string& newType);
private:
std::string _type;
}; };
#endif

View File

@ -1,7 +1,5 @@
#include "Weapon.hpp"
#include "HumanA.hpp" #include "HumanA.hpp"
#include "HumanB.hpp" #include "HumanB.hpp"
int main() int main()
{ {
{ {
@ -19,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;
} }

25
ex04/Makefile Normal file
View File

@ -0,0 +1,25 @@
CXX = c++
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
SRCDIR = src
OBJDIR = obj
NAME = sed
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

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;
} }

View File

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

View File

@ -1,6 +1,5 @@
#include "Harl.hpp" #include "Harl.hpp"
#include <iostream> #include <iostream>
#include <string>
void Harl::complain(std::string level) void Harl::complain(std::string level)
{ {
@ -26,22 +25,22 @@ void Harl::complain(std::string level)
} }
} }
void Harl::debug() const 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; 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() const 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; 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() const 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; 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() const void Harl::error()
{ {
std::cout << "This is unacceptable ! I want to speak to the manager now." << std::endl; std::cout << "This is unacceptable ! I want to speak to the manager now." << std::endl;
} }

View File

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

View File

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

View File

@ -1,47 +1,51 @@
#include "Harl.hpp" #include "Harl.hpp"
#include <iostream> #include <iostream>
#include <string>
void Harl::complain(std::string level) 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] = { static const std::string strings[4] = {
"DEBUG", "DEBUG",
"INFO", "INFO",
"WARNING", "WARNING",
"ERROR" "ERROR"
}; };
for (size_t i = 0; i < 4; i++)
{ size_t i = 0;
for (; i < 4; i++)
if (strings[i] == level) if (strings[i] == level)
{ break;
(this->*methods[i])(); switch(i) {
return; 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() const 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; 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() const 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; 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() const 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; 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() const void Harl::error()
{ {
std::cout << "This is unacceptable ! I want to speak to the manager now." << std::endl; std::cout << "This is unacceptable ! I want to speak to the manager now." << std::endl;
} }

View File

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

View File

@ -1,28 +1,10 @@
#include "Harl.hpp" #include "Harl.hpp"
#include <iostream>
int main(int ac, char **av) int main(int argc, char **argv)
{ {
if (argc <= 1)
return 1;
Harl harl; Harl harl;
static const std::string levels[] = { harl.complain(argv[1]);
"DEBUG", return 0;
"INFO",
"WARNING",
"GOLEM",
"ERROR",
"PASLU",
"FLEMME",
"PANIC"
};
for (int i = 0; i < 7; i++)
{
if (ac > 1 && levels[i] != av[1])
harl.complain(levels[i]);
else
{
std::cerr << "ERROR" << std::endl;
return (1);
}
}
return (0);
} }