cpp0 ex0 et ex1

This commit is contained in:
kinou-p 2022-04-21 19:02:55 +02:00
commit 064ec4a751
10 changed files with 443 additions and 0 deletions

34
cpp00/ex00/Makefile Normal file
View File

@ -0,0 +1,34 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

BIN
cpp00/ex00/megaphone Normal file

Binary file not shown.

36
cpp00/ex00/megaphone.cpp Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* megaphone.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/15 01:47:56 by apommier #+# #+# */
/* Updated: 2022/04/20 05:18:29 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <cctype>
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);
}

57
cpp00/ex01/Contact.cpp Normal file
View File

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Contact.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

43
cpp00/ex01/Contact.hpp Normal file
View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Contact.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <cstring>
# include <cctype>
# include <cstdlib>
# include <string>
# include <iostream>
# include <iomanip>
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

36
cpp00/ex01/Makefile Normal file
View File

@ -0,0 +1,36 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

91
cpp00/ex01/PhoneBook.cpp Normal file
View File

@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PhoneBook.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
}
}
}
}

33
cpp00/ex01/PhoneBook.hpp Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PhoneBook.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

79
cpp00/ex01/main.cpp Normal file
View File

@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

34
cpp00/ex02/Makefile Normal file
View File

@ -0,0 +1,34 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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