fix
This commit is contained in:
parent
49f93acdee
commit
11eb368caf
27
ex01/Makefile
Normal file
27
ex01/Makefile
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
CXX = c++
|
||||||
|
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror -g
|
||||||
|
SRCDIR = src
|
||||||
|
INCDIR = include
|
||||||
|
OBJDIR = obj
|
||||||
|
BINDIR = bin
|
||||||
|
EXECUTABLE = contacts
|
||||||
|
|
||||||
|
SRCS = $(wildcard $(SRCDIR)/*.cpp)
|
||||||
|
OBJS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
|
||||||
|
INCS = -I$(INCDIR)
|
||||||
|
|
||||||
|
all: $(BINDIR)/$(EXECUTABLE)
|
||||||
|
|
||||||
|
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
|
||||||
|
$(CXX) $(CXXFLAGS) $(INCS) -c $< -o $@
|
||||||
|
|
||||||
|
$(BINDIR)/$(EXECUTABLE): $(OBJS)
|
||||||
|
$(CXX) $(CXXFLAGS) $^ -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(OBJDIR)/*.o
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
rm -fr $(BINDIR)/$(EXECUTABLE)
|
||||||
|
|
||||||
|
re: fclean $(BINDIR)/$(EXECUTABLE)
|
17
ex01/include/Contact.hpp
Normal file
17
ex01/include/Contact.hpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#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(std::string first_name, std::string last_name, std::string nickname, std::string phone_number, std::string darkest_secret);
|
||||||
|
|
||||||
|
std::string to_string_partial() const;
|
||||||
|
std::string to_string_complete() const;
|
||||||
|
};
|
17
ex01/include/PhoneBook.hpp
Normal file
17
ex01/include/PhoneBook.hpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include "Contact.hpp"
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
class PhoneBook {
|
||||||
|
private:
|
||||||
|
Contact *contacts[8];
|
||||||
|
|
||||||
|
public:
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
PhoneBook();
|
||||||
|
~PhoneBook();
|
||||||
|
|
||||||
|
void add_contact(Contact *new_contact);
|
||||||
|
void display_contacts();
|
||||||
|
Contact* search(int index);
|
||||||
|
};
|
3
ex01/include/utils.hpp
Normal file
3
ex01/include/utils.hpp
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
|
std::string truncated(const std::string str);
|
54
ex01/src/Contact.cpp
Normal file
54
ex01/src/Contact.cpp
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include <ios>
|
||||||
|
#include <ostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include "../include/Contact.hpp"
|
||||||
|
#include "../include/utils.hpp"
|
||||||
|
|
||||||
|
Contact::Contact(std::string first_name, std::string last_name, std::string nickname, std::string phone_number, std::string darkest_secret)
|
||||||
|
{
|
||||||
|
this->first_name = std::string(first_name);
|
||||||
|
this->last_name = std::string(last_name);
|
||||||
|
this->nickname = std::string(nickname);
|
||||||
|
this->phone_number = std::string(phone_number);
|
||||||
|
this->darkest_secret = std::string(darkest_secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
return(ss.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
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 << "|";
|
||||||
|
|
||||||
|
return(ss.str());
|
||||||
|
}
|
66
ex01/src/PhoneBook.cpp
Normal file
66
ex01/src/PhoneBook.cpp
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
#include "../include/PhoneBook.hpp"
|
||||||
|
#include <cstddef>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
PhoneBook::PhoneBook()
|
||||||
|
{
|
||||||
|
this->len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PhoneBook::~PhoneBook()
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < this->len; i++)
|
||||||
|
delete this->contacts[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhoneBook::add_contact(Contact *new_contact)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PhoneBook::display_contacts()
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
std::cout
|
||||||
|
<< " index|"
|
||||||
|
<< "first name|"
|
||||||
|
<< "last name |"
|
||||||
|
<< "nickname |"
|
||||||
|
<< "phone num.|"
|
||||||
|
<< "darkest s."
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
|
for (i = 0; i < this->len; i++)
|
||||||
|
{
|
||||||
|
std::cout
|
||||||
|
<< std::setw(10) << i
|
||||||
|
<< "|"
|
||||||
|
<< this->contacts[i]->to_string_partial()
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Contact* PhoneBook::search(int index)
|
||||||
|
{
|
||||||
|
if ((size_t) index > this->len || index < 0)
|
||||||
|
return (NULL);
|
||||||
|
return (this->contacts[index]);
|
||||||
|
}
|
96
ex01/src/main.cpp
Normal file
96
ex01/src/main.cpp
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
#include <cstddef>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include "../include/PhoneBook.hpp"
|
||||||
|
|
||||||
|
int get_input(std::string &in, std::string out)
|
||||||
|
{
|
||||||
|
while (in.size() == 0)
|
||||||
|
{
|
||||||
|
std::cout << out;
|
||||||
|
if (!std::getline(std::cin, in))
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Contact* read_contact()
|
||||||
|
{
|
||||||
|
std::string first_name;
|
||||||
|
std::string last_name;
|
||||||
|
std::string nickname;
|
||||||
|
std::string phone_number;
|
||||||
|
std::string darkest_secret;
|
||||||
|
|
||||||
|
if (get_input(first_name, "first_name: ")
|
||||||
|
|| get_input(last_name, "last_name: ")
|
||||||
|
|| 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));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Contact* search(PhoneBook phone_book)
|
||||||
|
{
|
||||||
|
int index;
|
||||||
|
std::string index_str;
|
||||||
|
Contact* contact;
|
||||||
|
|
||||||
|
contact = NULL;
|
||||||
|
while (contact == NULL)
|
||||||
|
{
|
||||||
|
if (get_input(index_str, "index: "))
|
||||||
|
return (NULL);
|
||||||
|
index = std::atoi(index_str.c_str());
|
||||||
|
contact = phone_book.search(index);
|
||||||
|
}
|
||||||
|
return (contact);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::string command;
|
||||||
|
Contact* contact;
|
||||||
|
PhoneBook phone_book;
|
||||||
|
|
||||||
|
phone_book = PhoneBook();
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
std::cout << "command: ";
|
||||||
|
if (!std::getline(std::cin, command))
|
||||||
|
return (0);
|
||||||
|
if (command == "ADD")
|
||||||
|
{
|
||||||
|
contact = read_contact();
|
||||||
|
if (contact == NULL)
|
||||||
|
return (0);
|
||||||
|
phone_book.add_contact(contact);
|
||||||
|
}
|
||||||
|
else if (command == "SEARCH")
|
||||||
|
{
|
||||||
|
phone_book.display_contacts();
|
||||||
|
if (phone_book.len > 0)
|
||||||
|
{
|
||||||
|
contact = search(phone_book);
|
||||||
|
if (contact == NULL)
|
||||||
|
return (0);
|
||||||
|
std::cout << contact->to_string_complete() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (command == "EXIT")
|
||||||
|
{
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cerr << "Command not found" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
11
ex01/src/utils.cpp
Normal file
11
ex01/src/utils.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
|
std::string truncated(const std::string str)
|
||||||
|
{
|
||||||
|
std::string out;
|
||||||
|
|
||||||
|
out = str.substr(0, 10);
|
||||||
|
if (str.length() > 10)
|
||||||
|
(out)[9] = '.';
|
||||||
|
return (out);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user