sfdpionj9ogdfpjbjghdkgf

This commit is contained in:
Camille Chauvet 2023-06-15 12:28:47 +00:00
parent 908691c5da
commit 2e0f67ca59
7 changed files with 98 additions and 48 deletions

View File

@ -1,27 +1,24 @@
CXX = c++
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
SRCDIR = src
INCDIR = include
OBJDIR = obj
BINDIR = bin
EXECUTABLE = contacts
NAME = contacts
SRCS = $(wildcard $(SRCDIR)/*.cpp)
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
INCS = -I$(INCDIR)
all: $(BINDIR)/$(EXECUTABLE)
all: $(NAME)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) $(INCS) -c $< -o $@
$(CXX) $(CPPFLAGS) -c $< -o $@
$(BINDIR)/$(EXECUTABLE): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
$(NAME): $(OBJS)
$(CXX) $(CPPFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)/*.o
fclean: clean
rm -fr $(BINDIR)/$(EXECUTABLE)
rm -fr $(NAME)
re: fclean $(BINDIR)/$(EXECUTABLE)
re: fclean all

View File

@ -4,8 +4,8 @@
#include <string>
#include <iostream>
#include <iomanip>
#include "../include/Contact.hpp"
#include "../include/utils.hpp"
#include "./Contact.hpp"
#include "./utils.hpp"
Contact::Contact(std::string first_name, std::string last_name, std::string nickname, std::string phone_number, std::string darkest_secret)
{
@ -16,6 +16,30 @@ Contact::Contact(std::string first_name, std::string last_name, std::string nick
this->darkest_secret = std::string(darkest_secret);
}
Contact::Contact()
{
this->darkest_secret = "";
this->phone_number = "";
this->nickname = "";
this->last_name = "";
this->first_name = "";
}
Contact& Contact::operator=(Contact& src)
{
this->darkest_secret = src.darkest_secret;
this->phone_number = src.phone_number;
this->nickname = src.nickname;
this->last_name = src.last_name;
this->first_name = src.first_name;
return (*this);
}
Contact::~Contact()
{
std::cout << "je suis finit: " << this << std::endl;
}
std::string Contact::to_string_partial() const
{
std::stringstream ss;

21
ex01/src/Contact.hpp Normal file
View File

@ -0,0 +1,21 @@
#include <string>
#include <iostream>
class Contact {
private:
std::string first_name;
std::string last_name;
std::string nickname;
std::string phone_number;
std::string darkest_secret;
public:
Contact();
Contact(std::string first_name, std::string last_name, std::string nickname, std::string phone_number, std::string darkest_secret);
~Contact();
Contact &operator=(Contact &src);
std::string to_string_partial() const;
std::string to_string_complete() const;
};

View File

@ -1,4 +1,4 @@
#include "../include/PhoneBook.hpp"
#include "./PhoneBook.hpp"
#include <cstddef>
#include <iomanip>
#include <iostream>
@ -8,33 +8,19 @@
PhoneBook::PhoneBook()
{
Contact contact;
this->len = 0;
this->number = 0;
for (size_t i = 0; i < 8; i++)
this->contacts[i] = contact;
}
PhoneBook::~PhoneBook()
void PhoneBook::add_contact(Contact &new_contact)
{
for (size_t i = 0; i < this->len; i++)
delete this->contacts[i];
}
void PhoneBook::add_contact(Contact *new_contact)
{
size_t i;
std::cout << std::endl << this->len << std::endl;
this->contacts[this->number % 8] = new_contact;
if (this->len < 8)
{
this->contacts[this->len] = new_contact;
this->len++;
}
else
{
delete this->contacts[0];
for (i = this->len - 1; i > 0; i--) {
this->contacts[i] = this->contacts[i - 1];
}
this->contacts[0] = new_contact;
}
this->number++;
}
@ -56,7 +42,7 @@ void PhoneBook::display_contacts()
std::cout
<< std::setw(10) << i
<< "|"
<< this->contacts[i]->to_string_partial()
<< this->contacts[i].to_string_partial()
<< std::endl;
}
}
@ -65,5 +51,5 @@ Contact* PhoneBook::search(int index)
{
if ((size_t) index > this->len || index < 0)
return (NULL);
return (this->contacts[index]);
return (&this->contacts[index]);
}

17
ex01/src/PhoneBook.hpp Normal file
View File

@ -0,0 +1,17 @@
#include "Contact.hpp"
#include <cstddef>
class PhoneBook {
private:
Contact contacts[8];
size_t number;
public:
size_t len;
PhoneBook();
void add_contact(Contact &new_contact);
void display_contacts();
Contact* search(int index);
};

View File

@ -6,7 +6,7 @@
#include <ostream>
#include <sstream>
#include <string>
#include "../include/PhoneBook.hpp"
#include "./PhoneBook.hpp"
int get_input(std::string &in, std::string out)
{
@ -19,7 +19,7 @@ int get_input(std::string &in, std::string out)
return (0);
}
Contact* read_contact()
int read_contact(Contact& contact)
{
std::string first_name;
std::string last_name;
@ -32,9 +32,10 @@ Contact* read_contact()
|| get_input(nickname, "nickname: ")
|| get_input(phone_number, "phone_number: ")
|| get_input(darkest_secret, "darkest_secret: "))
return (NULL);
return (new Contact(first_name, last_name, nickname, phone_number, darkest_secret));
return (1);
Contact new_contact = Contact(first_name, last_name, nickname, phone_number, darkest_secret);
contact = new_contact;
return (0);
}
Contact* search(PhoneBook &phone_book)
@ -57,7 +58,8 @@ Contact* search(PhoneBook &phone_book)
int main()
{
std::string command;
Contact* contact;
Contact contact;
Contact *result;
PhoneBook phone_book;
while (true)
@ -67,9 +69,9 @@ int main()
return (0);
if (command == "ADD")
{
contact = read_contact();
if (contact == NULL)
if (read_contact(contact))
return (0);
std::cout << std::endl << "dasd" << std::endl;
phone_book.add_contact(contact);
}
else if (command == "SEARCH")
@ -77,10 +79,10 @@ int main()
phone_book.display_contacts();
if (phone_book.len > 0)
{
contact = search(phone_book);
if (contact == NULL)
result = search(phone_book);
if (result == NULL)
return (0);
std::cout << contact->to_string_complete() << std::endl;
std::cout << result->to_string_complete() << std::endl;
}
}
else if (command == "EXIT")

3
ex01/src/utils.hpp Normal file
View File

@ -0,0 +1,3 @@
#include <string>
std::string truncated(const std::string str);