sfdpionj9ogdfpjbjghdkgf
This commit is contained in:
parent
908691c5da
commit
2e0f67ca59
@ -1,27 +1,24 @@
|
|||||||
CXX = c++
|
CXX = c++
|
||||||
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g
|
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
|
||||||
SRCDIR = src
|
SRCDIR = src
|
||||||
INCDIR = include
|
|
||||||
OBJDIR = obj
|
OBJDIR = obj
|
||||||
BINDIR = bin
|
NAME = contacts
|
||||||
EXECUTABLE = contacts
|
|
||||||
|
|
||||||
SRCS = $(wildcard $(SRCDIR)/*.cpp)
|
SRCS = $(wildcard $(SRCDIR)/*.cpp)
|
||||||
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
|
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
|
||||||
INCS = -I$(INCDIR)
|
|
||||||
|
|
||||||
all: $(BINDIR)/$(EXECUTABLE)
|
all: $(NAME)
|
||||||
|
|
||||||
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
|
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
|
||||||
$(CXX) $(CXXFLAGS) $(INCS) -c $< -o $@
|
$(CXX) $(CPPFLAGS) -c $< -o $@
|
||||||
|
|
||||||
$(BINDIR)/$(EXECUTABLE): $(OBJS)
|
$(NAME): $(OBJS)
|
||||||
$(CXX) $(CXXFLAGS) $^ -o $@
|
$(CXX) $(CPPFLAGS) $^ -o $@
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(OBJDIR)/*.o
|
rm -rf $(OBJDIR)/*.o
|
||||||
|
|
||||||
fclean: clean
|
fclean: clean
|
||||||
rm -fr $(BINDIR)/$(EXECUTABLE)
|
rm -fr $(NAME)
|
||||||
|
|
||||||
re: fclean $(BINDIR)/$(EXECUTABLE)
|
re: fclean all
|
||||||
|
@ -4,8 +4,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include "../include/Contact.hpp"
|
#include "./Contact.hpp"
|
||||||
#include "../include/utils.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)
|
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);
|
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::string Contact::to_string_partial() const
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
21
ex01/src/Contact.hpp
Normal file
21
ex01/src/Contact.hpp
Normal 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;
|
||||||
|
};
|
@ -1,4 +1,4 @@
|
|||||||
#include "../include/PhoneBook.hpp"
|
#include "./PhoneBook.hpp"
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -8,33 +8,19 @@
|
|||||||
|
|
||||||
PhoneBook::PhoneBook()
|
PhoneBook::PhoneBook()
|
||||||
{
|
{
|
||||||
|
Contact contact;
|
||||||
this->len = 0;
|
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++)
|
this->contacts[this->number % 8] = new_contact;
|
||||||
delete this->contacts[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
void PhoneBook::add_contact(Contact *new_contact)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
std::cout << std::endl << this->len << std::endl;
|
|
||||||
if (this->len < 8)
|
if (this->len < 8)
|
||||||
{
|
|
||||||
this->contacts[this->len] = new_contact;
|
|
||||||
this->len++;
|
this->len++;
|
||||||
}
|
this->number++;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -56,7 +42,7 @@ void PhoneBook::display_contacts()
|
|||||||
std::cout
|
std::cout
|
||||||
<< std::setw(10) << i
|
<< std::setw(10) << i
|
||||||
<< "|"
|
<< "|"
|
||||||
<< this->contacts[i]->to_string_partial()
|
<< this->contacts[i].to_string_partial()
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,5 +51,5 @@ Contact* PhoneBook::search(int index)
|
|||||||
{
|
{
|
||||||
if ((size_t) index > this->len || index < 0)
|
if ((size_t) index > this->len || index < 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
return (this->contacts[index]);
|
return (&this->contacts[index]);
|
||||||
}
|
}
|
||||||
|
17
ex01/src/PhoneBook.hpp
Normal file
17
ex01/src/PhoneBook.hpp
Normal 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);
|
||||||
|
};
|
@ -6,7 +6,7 @@
|
|||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "../include/PhoneBook.hpp"
|
#include "./PhoneBook.hpp"
|
||||||
|
|
||||||
int get_input(std::string &in, std::string out)
|
int get_input(std::string &in, std::string out)
|
||||||
{
|
{
|
||||||
@ -19,7 +19,7 @@ int get_input(std::string &in, std::string out)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Contact* read_contact()
|
int read_contact(Contact& contact)
|
||||||
{
|
{
|
||||||
std::string first_name;
|
std::string first_name;
|
||||||
std::string last_name;
|
std::string last_name;
|
||||||
@ -32,9 +32,10 @@ Contact* read_contact()
|
|||||||
|| get_input(nickname, "nickname: ")
|
|| get_input(nickname, "nickname: ")
|
||||||
|| get_input(phone_number, "phone_number: ")
|
|| get_input(phone_number, "phone_number: ")
|
||||||
|| get_input(darkest_secret, "darkest_secret: "))
|
|| get_input(darkest_secret, "darkest_secret: "))
|
||||||
return (NULL);
|
return (1);
|
||||||
|
Contact new_contact = Contact(first_name, last_name, nickname, phone_number, darkest_secret);
|
||||||
return (new Contact(first_name, last_name, nickname, phone_number, darkest_secret));
|
contact = new_contact;
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Contact* search(PhoneBook &phone_book)
|
Contact* search(PhoneBook &phone_book)
|
||||||
@ -57,7 +58,8 @@ Contact* search(PhoneBook &phone_book)
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::string command;
|
std::string command;
|
||||||
Contact* contact;
|
Contact contact;
|
||||||
|
Contact *result;
|
||||||
PhoneBook phone_book;
|
PhoneBook phone_book;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
@ -67,9 +69,9 @@ int main()
|
|||||||
return (0);
|
return (0);
|
||||||
if (command == "ADD")
|
if (command == "ADD")
|
||||||
{
|
{
|
||||||
contact = read_contact();
|
if (read_contact(contact))
|
||||||
if (contact == NULL)
|
|
||||||
return (0);
|
return (0);
|
||||||
|
std::cout << std::endl << "dasd" << std::endl;
|
||||||
phone_book.add_contact(contact);
|
phone_book.add_contact(contact);
|
||||||
}
|
}
|
||||||
else if (command == "SEARCH")
|
else if (command == "SEARCH")
|
||||||
@ -77,10 +79,10 @@ int main()
|
|||||||
phone_book.display_contacts();
|
phone_book.display_contacts();
|
||||||
if (phone_book.len > 0)
|
if (phone_book.len > 0)
|
||||||
{
|
{
|
||||||
contact = search(phone_book);
|
result = search(phone_book);
|
||||||
if (contact == NULL)
|
if (result == NULL)
|
||||||
return (0);
|
return (0);
|
||||||
std::cout << contact->to_string_complete() << std::endl;
|
std::cout << result->to_string_complete() << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (command == "EXIT")
|
else if (command == "EXIT")
|
||||||
|
3
ex01/src/utils.hpp
Normal file
3
ex01/src/utils.hpp
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
|
std::string truncated(const std::string str);
|
Loading…
Reference in New Issue
Block a user