Compare commits
11 Commits
dce65dcb84
...
master
Author | SHA1 | Date | |
---|---|---|---|
bd77483ed4 | |||
16ab359cf2 | |||
120dfcfd83 | |||
585571872e | |||
bfda0a7548 | |||
03fe0a8720 | |||
44eec651e0 | |||
2a1dc87ca0 | |||
2e0f67ca59 | |||
908691c5da | |||
7bc9220a7a |
25
ex00/Makefile
Normal file
25
ex00/Makefile
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
CXX = c++
|
||||||
|
CPPFLAGS = -std=c++98 -Wall -Wextra -Werror -g
|
||||||
|
SRCDIR = src
|
||||||
|
OBJDIR = obj
|
||||||
|
NAME = ex02
|
||||||
|
|
||||||
|
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
|
@ -1,27 +1,25 @@
|
|||||||
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 = ex02
|
||||||
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 $@
|
mkdir -p obj
|
||||||
|
$(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,26 +16,43 @@ 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::string Contact::to_string_partial() const
|
std::string Contact::to_string_partial() const
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
std::string short_first_name;
|
std::string short_first_name;
|
||||||
std::string short_last_name;
|
std::string short_last_name;
|
||||||
std::string short_nickname;
|
std::string short_nickname;
|
||||||
std::string short_phone_number;
|
|
||||||
std::string short_darkest_secret;
|
|
||||||
|
|
||||||
short_first_name = truncated(this->first_name);
|
short_first_name = truncated(this->first_name);
|
||||||
short_last_name = truncated(this->last_name);
|
short_last_name = truncated(this->last_name);
|
||||||
short_nickname = truncated(this->nickname);
|
short_nickname = truncated(this->nickname);
|
||||||
short_phone_number = truncated(this->phone_number);
|
|
||||||
short_darkest_secret = truncated(this->darkest_secret);
|
|
||||||
|
|
||||||
ss << std::setw(10) << short_first_name << "|"
|
ss << std::setw(10) << short_first_name << "|"
|
||||||
<< std::setw(10) << short_last_name << "|"
|
<< std::setw(10) << short_last_name << "|"
|
||||||
<< std::setw(10) << short_nickname << "|"
|
<< std::setw(10) << short_nickname;
|
||||||
<< std::setw(10) << short_phone_number << "|"
|
|
||||||
<< std::setw(10) << short_darkest_secret;
|
|
||||||
|
|
||||||
return(ss.str());
|
return(ss.str());
|
||||||
}
|
}
|
||||||
@ -44,11 +61,11 @@ std::string Contact::to_string_complete() const
|
|||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
ss << std::setw(10) << this->first_name << "|"
|
ss << "first name: " << this->first_name << std::endl
|
||||||
<< std::setw(10) << this->last_name << "|"
|
<< "last name: " << this->last_name << std::endl
|
||||||
<< std::setw(10) << this->nickname << "|"
|
<< "nickname: " << this->nickname << std::endl
|
||||||
<< std::setw(10) << this->phone_number << "|"
|
<< "phone number: " << this->phone_number << std::endl
|
||||||
<< std::setw(10) << this->darkest_secret << "|";
|
<< "darkest secret: " << this->darkest_secret;
|
||||||
|
|
||||||
return(ss.str());
|
return(ss.str());
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@ -10,7 +11,11 @@ class Contact {
|
|||||||
std::string darkest_secret;
|
std::string darkest_secret;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Contact();
|
||||||
Contact(std::string first_name, std::string last_name, std::string nickname, std::string phone_number, std::string darkest_secret);
|
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_partial() const;
|
||||||
std::string to_string_complete() const;
|
std::string to_string_complete() const;
|
@ -1,37 +1,26 @@
|
|||||||
#include "../include/PhoneBook.hpp"
|
#include "./PhoneBook.hpp"
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iterator>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (this->len < 8)
|
if (this->len < 8)
|
||||||
{
|
|
||||||
this->contacts[this->len] = new_contact;
|
|
||||||
this->len++;
|
this->len++;
|
||||||
}
|
this->number++;
|
||||||
else
|
|
||||||
{
|
|
||||||
for (i = this->len; i != 0; i--) {
|
|
||||||
this->contacts[i] = this->contacts[i - 1];
|
|
||||||
}
|
|
||||||
this->contacts[0] = new_contact;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -39,28 +28,27 @@ void PhoneBook::display_contacts()
|
|||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
std::cout
|
if (this->len)
|
||||||
<< " index|"
|
std::cout
|
||||||
<< "first name|"
|
<< " index|"
|
||||||
<< "last name |"
|
<< "first name|"
|
||||||
<< "nickname |"
|
<< "last name |"
|
||||||
<< "phone num.|"
|
<< "nickname "
|
||||||
<< "darkest s."
|
<< std::endl;
|
||||||
<< std::endl;
|
|
||||||
|
|
||||||
for (i = 0; i < this->len; i++)
|
for (i = 0; i < this->len; i++)
|
||||||
{
|
{
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Contact* PhoneBook::search(int index)
|
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]);
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
#include "Contact.hpp"
|
#include "Contact.hpp"
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
class PhoneBook {
|
class PhoneBook {
|
||||||
private:
|
private:
|
||||||
Contact *contacts[8];
|
Contact contacts[8];
|
||||||
|
size_t number;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
PhoneBook();
|
PhoneBook();
|
||||||
~PhoneBook();
|
|
||||||
|
|
||||||
void add_contact(Contact *new_contact);
|
void add_contact(Contact &new_contact);
|
||||||
void display_contacts();
|
void display_contacts();
|
||||||
Contact* search(int index);
|
Contact* search(int index);
|
||||||
};
|
};
|
@ -3,9 +3,10 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
#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)
|
||||||
{
|
{
|
||||||
@ -18,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;
|
||||||
@ -31,25 +32,47 @@ 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)
|
int isdigit(std::string& str)
|
||||||
{
|
{
|
||||||
int index;
|
for (size_t i = 0; i < str.size(); i++)
|
||||||
|
{
|
||||||
|
if (!std::isdigit(str[i]))
|
||||||
|
return(0);
|
||||||
|
std::cout << i << std::endl;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Contact* search(PhoneBook &phone_book)
|
||||||
|
{
|
||||||
|
long index;
|
||||||
|
char *error;
|
||||||
std::string index_str;
|
std::string index_str;
|
||||||
Contact* contact;
|
Contact* contact;
|
||||||
|
|
||||||
contact = NULL;
|
contact = NULL;
|
||||||
while (contact == NULL)
|
while (contact == NULL)
|
||||||
{
|
{
|
||||||
|
index_str = "";
|
||||||
if (get_input(index_str, "index: "))
|
if (get_input(index_str, "index: "))
|
||||||
return (NULL);
|
return (NULL);
|
||||||
index = std::atoi(index_str.c_str());
|
index = std::strtol(index_str.c_str(), &error, 10);
|
||||||
contact = phone_book.search(index);
|
if (errno == ERANGE || error[0] != '\0')
|
||||||
|
{
|
||||||
|
std::cerr << "Invalid input" << std::endl;
|
||||||
|
errno = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
contact = phone_book.search(index);
|
||||||
|
if (contact == NULL)
|
||||||
|
std::cerr << "Contact not found" << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return (contact);
|
return (contact);
|
||||||
}
|
}
|
||||||
@ -57,10 +80,10 @@ 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;
|
||||||
|
|
||||||
phone_book = PhoneBook();
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
std::cout << "command: ";
|
std::cout << "command: ";
|
||||||
@ -68,8 +91,7 @@ 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);
|
||||||
phone_book.add_contact(contact);
|
phone_book.add_contact(contact);
|
||||||
}
|
}
|
||||||
@ -78,10 +100,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")
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
std::string truncated(const std::string str);
|
std::string truncated(const std::string str);
|
BIN
subject.pdf
BIN
subject.pdf
Binary file not shown.
Reference in New Issue
Block a user