From eb2b035b75d2d96684ad805366df6fad6b55aa3e Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 18 Jul 2023 03:28:41 +0200 Subject: [PATCH] add: ex05 --- ex05/Makefile | 25 +++++++++++++++++++++++++ ex05/src/Harl.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ ex05/src/Harl.hpp | 17 +++++++++++++++++ ex05/src/main.cpp | 19 +++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 ex05/Makefile create mode 100644 ex05/src/Harl.cpp create mode 100644 ex05/src/Harl.hpp create mode 100644 ex05/src/main.cpp diff --git a/ex05/Makefile b/ex05/Makefile new file mode 100644 index 0000000..c40c84e --- /dev/null +++ b/ex05/Makefile @@ -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 diff --git a/ex05/src/Harl.cpp b/ex05/src/Harl.cpp new file mode 100644 index 0000000..88c28ab --- /dev/null +++ b/ex05/src/Harl.cpp @@ -0,0 +1,46 @@ +#include "Harl.hpp" +#include + +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 didn’t put enough bacon in my burger ! If you did, I wouldn’t be asking for more !" << std::endl; +} + +void Harl::warning() const +{ + std::cout << "I think I deserve to have some extra bacon for free. I’ve 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; +} diff --git a/ex05/src/Harl.hpp b/ex05/src/Harl.hpp new file mode 100644 index 0000000..ca9f23a --- /dev/null +++ b/ex05/src/Harl.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +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; diff --git a/ex05/src/main.cpp b/ex05/src/main.cpp new file mode 100644 index 0000000..ac7f980 --- /dev/null +++ b/ex05/src/main.cpp @@ -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; +}