init: ex06 ex05

This commit is contained in:
Camille Chauvet 2023-05-26 16:50:46 +00:00
parent 84b0db8c9c
commit dcfb2af057
8 changed files with 223 additions and 0 deletions

25
ex05/Makefile Normal file
View File

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

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

@ -0,0 +1,47 @@
#include "Harl.hpp"
#include <iostream>
#include <string>
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() const
{
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
{
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
{
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
{
std::cout << "This is unacceptable ! I want to speak to the manager now." << std::endl;
}

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

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

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++
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g -O0
SRCDIR = src
OBJDIR = obj
BINDIR = bin
EXECUTABLE = harl
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(BINDIR)/$(EXECUTABLE)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
$(BINDIR)/$(EXECUTABLE): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(BINDIR)/$(EXECUTABLE)
re: fclean all

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

@ -0,0 +1,47 @@
#include "Harl.hpp"
#include <iostream>
#include <string>
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() const
{
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
{
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
{
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
{
std::cout << "This is unacceptable ! I want to speak to the manager now." << std::endl;
}

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

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

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

@ -0,0 +1,28 @@
#include "Harl.hpp"
#include <iostream>
int main(int ac, char **av)
{
Harl harl;
static const std::string levels[] = {
"DEBUG",
"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);
}