commit 064ec4a751c08e5172d71696d6f0b60fab6aa283 Author: kinou-p Date: Thu Apr 21 19:02:55 2022 +0200 cpp0 ex0 et ex1 diff --git a/cpp00/ex00/Makefile b/cpp00/ex00/Makefile new file mode 100644 index 0000000..eca33fe --- /dev/null +++ b/cpp00/ex00/Makefile @@ -0,0 +1,34 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: apommier +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/04/15 01:47:51 by apommier #+# #+# # +# Updated: 2022/04/15 05:00:14 by apommier ### ########.fr # +# # +# **************************************************************************** # + +NAME = megaphone +SRCS = megaphone.cpp + +OBJS = ${SRCS:.cpp=.o} +CC = c++ +CFLAGS = -Wall -Wextra -Werror +RM = rm -rf + +${NAME}: ${OBJS} + ${CC} ${LIB} ${OBJS} -o ${NAME} + +all: ${NAME} + +clean: + @${RM} ${OBJS} + +fclean: clean + @${RM} ${NAME} + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/cpp00/ex00/megaphone b/cpp00/ex00/megaphone new file mode 100644 index 0000000..ba0d0f2 Binary files /dev/null and b/cpp00/ex00/megaphone differ diff --git a/cpp00/ex00/megaphone.cpp b/cpp00/ex00/megaphone.cpp new file mode 100644 index 0000000..63a107c --- /dev/null +++ b/cpp00/ex00/megaphone.cpp @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* megaphone.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/15 01:47:56 by apommier #+# #+# */ +/* Updated: 2022/04/20 05:18:29 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +int main(int ac, char **av) +{ + int i = 0; + int j = 0; + + if (ac != 1) + { + while (av[++i]) + { + j = -1; + while (av[i][++j]) + av[i][j] = toupper(av[i][j]); + } + i = 0; + while (av[++i]) + std::cout << av[i] << std::endl; + } + else + std::cout << "* LOUD AND UNBEARABLE FEEDBACK NOISE *" << std::endl; + return (0); +} \ No newline at end of file diff --git a/cpp00/ex01/Contact.cpp b/cpp00/ex01/Contact.cpp new file mode 100644 index 0000000..f7f7113 --- /dev/null +++ b/cpp00/ex01/Contact.cpp @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Contact.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/15 10:37:12 by apommier #+# #+# */ +/* Updated: 2022/04/21 18:32:43 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Contact.hpp" + +Contact::Contact(void) { + return ; +} + +Contact::~Contact(void) { + return ; +} + +void Contact::FillContact(std::string *info) { + this->_first_name = info[0]; + this->_last_name = info[1]; + this->_nickname = info[2]; + this->_phone_number = info[3]; + this->_darkest_secret = info[4]; +} + +std::string Contact::GetInfo(int index) +{ + std::string str; + + str = this->GetRealInfo(index); + if (str.size() > 10) + { + str.resize(10); + str[9] = '.'; + } + return (str); +} + +std::string Contact::GetRealInfo(int index) +{ + if (index == 0) + return (this->_first_name); + if (index == 1) + return (this->_last_name); + if (index == 2) + return (this->_nickname); + if (index == 3) + return (this->_phone_number); + if (index == 4) + return (this->_darkest_secret); + return (0); +} \ No newline at end of file diff --git a/cpp00/ex01/Contact.hpp b/cpp00/ex01/Contact.hpp new file mode 100644 index 0000000..419df3e --- /dev/null +++ b/cpp00/ex01/Contact.hpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Contact.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/15 10:37:15 by apommier #+# #+# */ +/* Updated: 2022/04/21 18:00:26 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CONTACT_HPP +# define CONTACT_HPP + +# include +# include +# include +# include +# include +# include + +class Contact { + + public: + + Contact(void); + ~Contact(void); + void FillContact(std::string *info); + std::string GetInfo(int index); + std::string GetRealInfo(int index); + + private: + + + std::string _first_name; + std::string _last_name; + std::string _nickname; + std::string _phone_number; + std::string _darkest_secret; +}; + +#endif \ No newline at end of file diff --git a/cpp00/ex01/Makefile b/cpp00/ex01/Makefile new file mode 100644 index 0000000..f454103 --- /dev/null +++ b/cpp00/ex01/Makefile @@ -0,0 +1,36 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: apommier +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/04/15 01:47:51 by apommier #+# #+# # +# Updated: 2022/04/20 05:40:02 by apommier ### ########.fr # +# # +# **************************************************************************** # + +NAME = a.out +SRCS = main.cpp\ + Contact.cpp\ + PhoneBook.cpp + +OBJS = ${SRCS:.cpp=.o} +CC = c++ +CFLAGS = -Wall -Wextra -Werror -g +RM = rm -rf + +${NAME}: ${OBJS} + ${CC} ${LIB} ${OBJS} -o ${NAME} + +all: ${NAME} + +clean: + @${RM} ${OBJS} + +fclean: clean + @${RM} ${NAME} + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/cpp00/ex01/PhoneBook.cpp b/cpp00/ex01/PhoneBook.cpp new file mode 100644 index 0000000..b714057 --- /dev/null +++ b/cpp00/ex01/PhoneBook.cpp @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PhoneBook.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/15 10:37:20 by apommier #+# #+# */ +/* Updated: 2022/04/21 18:32:32 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "PhoneBook.hpp" + +PhoneBook::PhoneBook(void) +{ + int index = 0; + + this->_NbrContact = 0; + while (index < 8) + _Contact[index++] = 0; + return ; +} + +PhoneBook::~PhoneBook(void) +{ + return ; +} + +void PhoneBook::StoreContact(Contact *NewContact) +{ + _Contact[this->_NbrContact] = NewContact; + if (this->_NbrContact == 8) + this->_NbrContact = 0; + else + this->_NbrContact++; +} + +void PhoneBook::PrintContact(int index) +{ + if (!this->_Contact[index]) + { + std::cout << "There is no contact at this index" << std::endl; + return ; + } + std::cout << std::setw(10) << "First name: " << this->_Contact[index]->GetRealInfo(0) << std::endl; + std::cout << std::setw(10) << "Last name: " << this->_Contact[index]->GetRealInfo(1) << std::endl; + std::cout << std::setw(10) << "Nickname: " << this->_Contact[index]->GetRealInfo(2) << std::endl; + std::cout << std::setw(10) << "Phone number: " << this->_Contact[index]->GetRealInfo(3) << std::endl; + std::cout << std::setw(10) << "Darkest secret: " << this->_Contact[index]->GetRealInfo(4) << std::endl; +} + +void PhoneBook::PrintIndex() +{ + int index = 0; + std::string nbr; + + + while (index < 8 && this->_Contact[index]) + { + std::cout << std::setw(10) << index + 1 << "|"; + std::cout << std::setw(10) << this->_Contact[index]->GetInfo(0) << "|"; + std::cout << std::setw(10) << this->_Contact[index]->GetInfo(1) << "|"; + std::cout << std::setw(10) << this->_Contact[index]->GetInfo(2) << std::endl; + index++; + } + if (!index) + std::cout << "There is no contact\n"; + else + { + while (1) + { + std::cout << "Enter index of contact: "; + std::cin >> nbr; + if (nbr.size() != 1 || !isdigit(nbr[0])) + std::cout << "Bad entry" << std::endl; + else + { + index = nbr[0] - 48; + if (!index || index > 8) + std::cout << "Bad entry" << std::endl; + else + { + this->PrintContact(index - 1); + break; + } + } + } + } +} + diff --git a/cpp00/ex01/PhoneBook.hpp b/cpp00/ex01/PhoneBook.hpp new file mode 100644 index 0000000..8b7379d --- /dev/null +++ b/cpp00/ex01/PhoneBook.hpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PhoneBook.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/15 10:37:24 by apommier #+# #+# */ +/* Updated: 2022/04/21 17:57:29 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PHONEBOOK_HPP +# define PHONEBOOK_HPP + +# include "Contact.hpp" + +class PhoneBook { + + public: + + PhoneBook(void); + ~PhoneBook(void); + void StoreContact(Contact *NewContact); + void PrintContact(int index); + void PrintIndex(); + private: + + int _NbrContact; + class Contact *_Contact[8]; +}; + +#endif \ No newline at end of file diff --git a/cpp00/ex01/main.cpp b/cpp00/ex01/main.cpp new file mode 100644 index 0000000..d4fe0b6 --- /dev/null +++ b/cpp00/ex01/main.cpp @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/17 15:55:40 by apommier #+# #+# */ +/* Updated: 2022/04/21 18:58:19 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Contact.hpp" +#include "PhoneBook.hpp" + +void PrintMessage(int index) +{ + if (index == 0) + std::cout << "Enter your fisrt name: "; + if (index == 1) + std::cout << "Enter your last name: "; + if (index == 2) + std::cout << "Enter your nickname: "; + if (index == 3) + std::cout << "Enter your phone number: "; + if (index == 4) + std::cout << "Enter your darkest secret: "; +} + +void search_contact(PhoneBook *Book, Contact *Contact) +{ + +} + +void add_contact(PhoneBook *Book, Contact *Contact) +{ + int index = 0; + std::string line; + std::string info[5]; + + while (index < 5) + { + PrintMessage(index); + std::getline(std::cin, line); + info[index] = line; + if (!line.empty()) + index++; + } + Contact->FillContact(info); + Book->StoreContact(Contact); +} + +int main(int ac, char **av) +{ + PhoneBook book; + Contact contact; + std::string line; + int NbrContact = 0; + + if (ac != 1) + { + std::cout << "too much arguments\n"; + return (0); + } + //line = "nothing"; + std::cout << "Enter a command : ADD | SEARCH | EXIT" << std::endl; + while (line != "EXIT") + { + std::getline(std::cin, line); + if (line == "ADD") + add_contact(&book, &contact); + else if (line == "SEARCH") + book.PrintIndex(); + else + std::cout << "Invalid command" << std::endl; + } + //std::cout << "exit\n"; + return (0); +} \ No newline at end of file diff --git a/cpp00/ex02/Makefile b/cpp00/ex02/Makefile new file mode 100644 index 0000000..eca33fe --- /dev/null +++ b/cpp00/ex02/Makefile @@ -0,0 +1,34 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: apommier +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/04/15 01:47:51 by apommier #+# #+# # +# Updated: 2022/04/15 05:00:14 by apommier ### ########.fr # +# # +# **************************************************************************** # + +NAME = megaphone +SRCS = megaphone.cpp + +OBJS = ${SRCS:.cpp=.o} +CC = c++ +CFLAGS = -Wall -Wextra -Werror +RM = rm -rf + +${NAME}: ${OBJS} + ${CC} ${LIB} ${OBJS} -o ${NAME} + +all: ${NAME} + +clean: + @${RM} ${OBJS} + +fclean: clean + @${RM} ${NAME} + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file