Compare commits

...

11 Commits

11 changed files with 133 additions and 84 deletions

25
ex00/Makefile Normal file
View 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

View File

@ -1,27 +1,25 @@
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 = ex02
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 $@
mkdir -p obj
$(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,26 +16,43 @@ 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::string Contact::to_string_partial() const
{
std::stringstream ss;
std::string short_first_name;
std::string short_last_name;
std::string short_nickname;
std::string short_phone_number;
std::string short_darkest_secret;
short_first_name = truncated(this->first_name);
short_last_name = truncated(this->last_name);
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 << "|"
<< std::setw(10) << short_last_name << "|"
<< std::setw(10) << short_nickname << "|"
<< std::setw(10) << short_phone_number << "|"
<< std::setw(10) << short_darkest_secret;
<< std::setw(10) << short_nickname;
return(ss.str());
}
@ -44,11 +61,11 @@ std::string Contact::to_string_complete() const
{
std::stringstream ss;
ss << std::setw(10) << this->first_name << "|"
<< std::setw(10) << this->last_name << "|"
<< std::setw(10) << this->nickname << "|"
<< std::setw(10) << this->phone_number << "|"
<< std::setw(10) << this->darkest_secret << "|";
ss << "first name: " << this->first_name << std::endl
<< "last name: " << this->last_name << std::endl
<< "nickname: " << this->nickname << std::endl
<< "phone number: " << this->phone_number << std::endl
<< "darkest secret: " << this->darkest_secret;
return(ss.str());
}

View File

@ -1,3 +1,4 @@
#pragma once
#include <string>
#include <iostream>
@ -10,7 +11,11 @@ class Contact {
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,37 +1,26 @@
#include "../include/PhoneBook.hpp"
#include "./PhoneBook.hpp"
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
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;
this->contacts[this->number % 8] = new_contact;
if (this->len < 8)
{
this->contacts[this->len] = new_contact;
this->len++;
}
else
{
for (i = this->len; i != 0; i--) {
this->contacts[i] = this->contacts[i - 1];
}
this->contacts[0] = new_contact;
}
this->number++;
}
@ -39,28 +28,27 @@ void PhoneBook::display_contacts()
{
size_t i;
std::cout
<< " index|"
<< "first name|"
<< "last name |"
<< "nickname |"
<< "phone num.|"
<< "darkest s."
<< std::endl;
if (this->len)
std::cout
<< " index|"
<< "first name|"
<< "last name |"
<< "nickname "
<< std::endl;
for (i = 0; i < this->len; i++)
{
std::cout
<< std::setw(10) << i
<< "|"
<< this->contacts[i]->to_string_partial()
<< this->contacts[i].to_string_partial()
<< std::endl;
}
}
Contact* PhoneBook::search(int index)
{
if ((size_t) index > this->len || index < 0)
if ((size_t) index >= this->len || index < 0)
return (NULL);
return (this->contacts[index]);
return (&this->contacts[index]);
}

View File

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

View File

@ -3,9 +3,10 @@
#include <cstdlib>
#include <iostream>
#include <bits/stdc++.h>
#include <ostream>
#include <sstream>
#include <string>
#include "../include/PhoneBook.hpp"
#include "./PhoneBook.hpp"
int get_input(std::string &in, std::string out)
{
@ -18,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;
@ -31,25 +32,47 @@ 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)
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;
Contact* contact;
contact = NULL;
while (contact == NULL)
{
index_str = "";
if (get_input(index_str, "index: "))
return (NULL);
index = std::atoi(index_str.c_str());
contact = phone_book.search(index);
index = std::strtol(index_str.c_str(), &error, 10);
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);
}
@ -57,10 +80,10 @@ Contact* search(PhoneBook phone_book)
int main()
{
std::string command;
Contact* contact;
Contact contact;
Contact *result;
PhoneBook phone_book;
phone_book = PhoneBook();
while (true)
{
std::cout << "command: ";
@ -68,8 +91,7 @@ int main()
return (0);
if (command == "ADD")
{
contact = read_contact();
if (contact == NULL)
if (read_contact(contact))
return (0);
phone_book.add_contact(contact);
}
@ -78,10 +100,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")

View File

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

7
ex01/t
View File

@ -1,7 +0,0 @@
ADD
fffffffffff
fff
f
f
f
SEARCH

Binary file not shown.