This commit is contained in:
Camille Chauvet 2023-05-17 16:41:46 +00:00
commit 67fbbf113b
9 changed files with 222 additions and 0 deletions

22
ex00/megaphone.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <iostream>
#include <string>
#include <cctype>
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "* LOUD AND UNBEARABLE FEEDBACK NOISE *" << std::endl;
return 0;
}
std::string message;
for (int i = 1; i < argc; i++) {
message += argv[i];
}
for (int i = 0; message[i] != '\0'; i++) {
message[i] = std::toupper(message[i]);
}
std::cout << message << std::endl;
return 0;
}

60
ex01/Contact.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <ios>
#include <ostream>
#include <sstream>
#include <string>
#include <iostream>
#include <iomanip>
#include "Contact.hpp"
#include "utils.hpp"
Contact::Contact(std::string first_name, std::string last_name, std::string nickname, std::string phone_number)
{
this->nickname = nickname;
this->last_name = last_name;
this->first_name = first_name;
this->phone_number = phone_number;
}
Contact::Contact()
{
this->first_name = "";
this->last_name = "";
this->phone_number = "";
this->phone_number = "";
}
Contact::~Contact()
{
}
Contact &Contact::operator=(const Contact &contact)
{
this->first_name = contact.first_name;
this->last_name = contact.last_name;
this->nickname = contact.nickname;
this->phone_number = contact.phone_number;
return (*this);
}
bool Contact::isValid() const
{
return (!(this->first_name == "" || this->last_name == "" || this->nickname == "" || this->phone_number == ""));
}
std::string Contact::to_string() const
{
std::stringstream ss;
ss << std::setw(10) << truncated(this->first_name) << "|"
<< std::setw(10) << truncated(this->last_name) << "|"
<< std::setw(10) << truncated(this->nickname) << "|"
<< std::setw(10) << truncated(this->phone_number);
return(ss.str());
}
std::ostream& operator<<(std::ostream& os, const Contact& contact)
{
os << contact.to_string() << std::endl;
return (os);
}

22
ex01/Contact.hpp Normal file
View File

@ -0,0 +1,22 @@
#include <string>
#include <iostream>
class Contact {
private:
std::string first_name;
std::string last_name;
std::string nickname;
std::string phone_number;
public:
Contact(std::string first_name, std::string last_name, std::string nickname, std::string phone_number);
Contact();
~Contact();
Contact &operator=(const Contact &contact);
bool isValid() const;
std::string to_string() const;
};
std::ostream& operator<<(std::ostream& os, const Contact& fixed);

36
ex01/PhoneBook.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "PhoneBook.hpp"
#include <cstddef>
#include <iostream>
#include <string>
PhoneBook::PhoneBook()
{
this->len = 0;
}
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;
for (i = 0; i < this->len; i++)
{
std::cout << this->contacts[i];
}
}

13
ex01/PhoneBook.hpp Normal file
View File

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

55
ex01/main.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <iostream>
#include <bits/stdc++.h>
#include <string>
#include "PhoneBook.hpp"
Contact read_contact()
{
std::string first_name;
std::string last_name;
std::string nickname;
std::string phone_number;
std::cout << "first_name: ";
std::getline(std::cin, first_name);
std::cout << "last_name: ";
std::getline(std::cin, last_name);
std::cout << "nickname: ";
std::getline(std::cin, nickname);
std::cout << "phone_number: ";
std::getline(std::cin, phone_number);
return Contact(first_name, last_name, nickname, phone_number);
}
int main()
{
std::string command;
Contact new_contact;
PhoneBook phone_book;
phone_book = PhoneBook();
while (true)
{
std::cout << "command: ";
std::getline(std::cin, command);
if (command == "ADD")
{
new_contact = read_contact();
if (new_contact.isValid())
phone_book.add_contact(new_contact);
else
std::cerr << "Contact Error !" << std::endl;
}
else if (command == "SEARCH")
{
phone_book.display_contacts();
}
else if (command == "EXIT")
{
return (0);
}
}
return (0);
}

11
ex01/utils.cpp Normal file
View 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);
}

3
ex01/utils.hpp Normal file
View File

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

BIN
subject.pdf Normal file

Binary file not shown.