diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-04-13 09:18:30 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-04-13 09:18:30 +0200 |
| commit | 629d52b1262879a346e9ca17567a8f483c60ba0a (patch) | |
| tree | b6d32f764980b08cea6327e9b8a472d88331aae9 | |
| parent | 8fc4395b9a61416e9bfee500e064dce7be9c8d7c (diff) | |
| download | piscine_cpp-629d52b1262879a346e9ca17567a8f483c60ba0a.tar.gz piscine_cpp-629d52b1262879a346e9ca17567a8f483c60ba0a.tar.bz2 piscine_cpp-629d52b1262879a346e9ca17567a8f483c60ba0a.zip | |
Fixed cpp00/ex01
| -rw-r--r-- | cpp00/ex00/megaphone.cpp | 15 | ||||
| -rw-r--r-- | cpp00/ex01/Contact.cpp | 101 | ||||
| -rw-r--r-- | cpp00/ex01/Contact.hpp | 63 | ||||
| -rw-r--r-- | cpp00/ex01/Makefile | 22 | ||||
| -rw-r--r-- | cpp00/ex01/PhoneBook.cpp | 49 | ||||
| -rw-r--r-- | cpp00/ex01/PhoneBook.hpp | 25 | ||||
| -rw-r--r-- | cpp00/ex01/main.cpp | 73 | ||||
| -rw-r--r-- | cpp00/ex01/utils.cpp | 26 | ||||
| -rw-r--r-- | cpp00/ex01/utils.hpp | 23 | ||||
| -rw-r--r-- | cpp01/ex00/Pony.cpp | 12 |
10 files changed, 321 insertions, 88 deletions
diff --git a/cpp00/ex00/megaphone.cpp b/cpp00/ex00/megaphone.cpp index 13ee0fc..32b4d14 100644 --- a/cpp00/ex00/megaphone.cpp +++ b/cpp00/ex00/megaphone.cpp @@ -1,17 +1,7 @@ #include <iostream> #include <cctype> -// not sure -// char -// ft_toupper(char c) -// { -// if (c >= 'a' && c <= 'z') -// return (c ^ 0b00100000); -// return (c); -// } - -int -main(int argc, char **argv) +int main(int argc, char **argv) { if (argc == 1) { @@ -21,8 +11,7 @@ main(int argc, char **argv) for (int i = 1; i < argc; i++) { for (int j = 0; argv[i][j]; j++) - argv[i][j] = toupper(argv[i][j]); - std::cout << argv[i]; + std::cout << (char)toupper(argv[i][j]); } std::cout << std::endl; return 0; diff --git a/cpp00/ex01/Contact.cpp b/cpp00/ex01/Contact.cpp index 6e8764c..12f06db 100644 --- a/cpp00/ex01/Contact.cpp +++ b/cpp00/ex01/Contact.cpp @@ -1,16 +1,99 @@ -#include <string> +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Contact.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 09:05:59 by charles #+# #+# */ +/* Updated: 2020/04/13 09:17:57 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "Contact.hpp" -Contact::Contact(std::string name) +Contact Contact::prompt() +{ + Contact c; + + Contact::promptString("first name: ", c.m_firstName); + Contact::promptString("last name: ", c.m_lastName); + Contact::promptString("nickname: ", c.m_nickname); + Contact::promptString("login: ", c.m_login); + + std::cout << "address:" << std::endl; + Contact::promptInt("| house number: ", c.m_address.m_houseNum); + Contact::promptInt("| post code: ", c.m_address.m_postCode); + Contact::promptString("| street name: ", c.m_address.m_street); + Contact::promptString("| city: ", c.m_address.m_city); + + Contact::promptString("email: ", c.m_email); + Contact::promptString("phone: ", c.m_phone); + + std::cout << "birthday:" << std::endl; + Contact::promptInt("| day: ", c.m_birthday.m_day); + Contact::promptInt("| month: ", c.m_birthday.m_month); + Contact::promptInt("| year: ", c.m_birthday.m_year); + + Contact::promptString("favorite meal: ", c.m_favMeal); + Contact::promptString("underware color: ", c.m_underwareColor); + Contact::promptString("darkest secret: ", c.m_darkestSecret); + return c; +} + +void Contact::promptString(std::string promptString, std::string &s) +{ + std::cout << promptString << std::flush; + getline(std::cin, s); +} + +void Contact::promptInt(std::string promptString, int &i) +{ + std::cout << promptString << std::flush; + i = getInt(); + while (i == -1) + { + std::cout << "Error: Not a valid number, please try again" << std::endl; + i = getInt(); + } +} + +std::string Contact::trimedName(std::string name) { - this->name = name; + if (name.length() > 10) + return name.substr(0, 9) + "."; + return name; } -std::string -Contact::phone_str() +void Contact::preview() const { - std::string s; - for (int i = 0; i < 10; i++) - s.push_back(phone[i] - '0'); - return s; + std::cout << std::setw(10) << trimedName(m_firstName) << "|" + << std::setw(10) << trimedName(m_lastName) << "|" + << std::setw(10) << trimedName(m_nickname); +} + +void Contact::put() const +{ + std::cout << m_firstName << std::endl; + std::cout << m_lastName << std::endl; + std::cout << m_nickname << std::endl; + std::cout << m_login << std::endl; + + std::cout << "Street " << m_address.m_street + << " Nb " << m_address.m_houseNum + << " " << m_address.m_postCode + << " " << m_address.m_city + << std::endl; + + std::cout << m_email << std::endl; + std::cout << m_phone << std::endl; + + std::cout << m_birthday.m_day + << "/" << m_birthday.m_month + << "/" << m_birthday.m_year + << std::endl; + + std::cout << m_favMeal << std::endl; + std::cout << m_underwareColor << std::endl; + std::cout << m_darkestSecret << std::endl; } diff --git a/cpp00/ex01/Contact.hpp b/cpp00/ex01/Contact.hpp index 3d04889..d9fbee6 100644 --- a/cpp00/ex01/Contact.hpp +++ b/cpp00/ex01/Contact.hpp @@ -1,25 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Contact.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 07:23:11 by charles #+# #+# */ +/* Updated: 2020/04/13 08:57:01 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef CONTACT_HPP # define CONTACT_HPP # include <string> +# include <iomanip> +# include <iostream> +# include "utils.hpp" class Contact { public: - Contact(std::string name); - // std::string phone_str(); + static Contact prompt(); + void preview() const; + void put() const; + +private: + static void promptString(std::string promptString, std::string &s); + static void promptInt(std::string promptString, int &i); + static std::string trimedName(std::string name); + + struct Address + { + int m_houseNum; + int m_postCode; + std::string m_street; + std::string m_city; + }; + struct Date + { + int m_day; + int m_month; + int m_year; + }; - std::string first_name; - std::string last_name; - std::string nickname; - std::string login; - std::string postal_address; - std::string email; - std::string phone; - std::string birthday; - std::string fav_meal; - std::string underware_color; - std::string darkest_secret; + std::string m_firstName; + std::string m_lastName; + std::string m_nickname; + std::string m_login; + Address m_address; + std::string m_email; + std::string m_phone; + Date m_birthday; + std::string m_favMeal; + std::string m_underwareColor; + std::string m_darkestSecret; }; -#endif // CONTACT_HPP +#endif diff --git a/cpp00/ex01/Makefile b/cpp00/ex01/Makefile index a69e4f5..a21d79f 100644 --- a/cpp00/ex01/Makefile +++ b/cpp00/ex01/Makefile @@ -1,20 +1,32 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/04/13 08:32:50 by charles #+# #+# # +# Updated: 2020/04/13 08:42:09 by charles ### ########.fr # +# # +# **************************************************************************** # + RM = rm -f -CC = clang++ -CCFLAGS = -Wall -Wextra -Werror +CXX = clang++ +CXXFLAGS = -Wall -Wextra -Werror NAME = crapyphonebook -SRC = main.cpp PhoneBook.cpp Contact.cpp +SRC = main.cpp PhoneBook.cpp Contact.cpp utils.cpp OBJ = $(SRC:.cpp=.o) all: $(NAME) $(NAME): $(OBJ) - $(CC) $(CCFLAGS) -o $@ $^ + $(CXX) $(CXXFLAGS) -o $@ $^ %.o: %.c - $(CC) $(CCFLAGS) -o $@ $< + $(CXX) $(CXXFLAGS) -o $@ $< clean: $(RM) $(OBJ) diff --git a/cpp00/ex01/PhoneBook.cpp b/cpp00/ex01/PhoneBook.cpp index 44c7ea9..a47307f 100644 --- a/cpp00/ex01/PhoneBook.cpp +++ b/cpp00/ex01/PhoneBook.cpp @@ -1,25 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PhoneBook.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 07:23:13 by charles #+# #+# */ +/* Updated: 2020/04/13 08:43:05 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "PhoneBook.hpp" -PhoneBook::PhoneBook() +PhoneBook::PhoneBook(): m_size(0) +{ +} + +void PhoneBook::add(Contact contact) +{ + m_contacts[m_size] = contact; + m_size++; +} + +Contact const &PhoneBook::get(int index) const { - contacts_len = 0; + return m_contacts[index]; } -bool -PhoneBook::add(Contact contact) +size_t PhoneBook::getSize() const { - if (contacts_len >= CONTACTS_SIZE) - return false; - contacts[contacts_len] = contact; - contacts_len++; - return true; + return m_size; } -Contact* -PhoneBook::search(std::string needle) +std::ostream &operator<<(std::ostream &out, PhoneBook const &p) { - for (int i = 0; i < contacts_len; i++) - if (needle == contacts[i].name) - return contacts + i; - return NULL; + for (size_t i = 0; i < p.getSize(); i++) + { + out << i << "|"; + p.get(i).preview(); + std::cout << std::endl; + } + return out; } diff --git a/cpp00/ex01/PhoneBook.hpp b/cpp00/ex01/PhoneBook.hpp index c4f8a23..8510710 100644 --- a/cpp00/ex01/PhoneBook.hpp +++ b/cpp00/ex01/PhoneBook.hpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PhoneBook.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 09:06:23 by charles #+# #+# */ +/* Updated: 2020/04/13 09:06:39 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef PHONE_BOOK_HPP # define PHONE_BOOK_HPP @@ -10,11 +22,16 @@ class PhoneBook { public: PhoneBook(); - bool add(Contact contact); + void add(Contact contact); Contact* search(std::string needle); + size_t getSize() const; + Contact const &get(int index) const; + private: - Contact contacts[CONTACTS_SIZE]; - int contacts_len; + Contact m_contacts[CONTACTS_SIZE]; + int m_size; }; -#endif // PHONE_BOOK_HPP +std::ostream &operator<<(std::ostream &out, PhoneBook const &p); + +#endif diff --git a/cpp00/ex01/main.cpp b/cpp00/ex01/main.cpp index 051f0e8..5734d38 100644 --- a/cpp00/ex01/main.cpp +++ b/cpp00/ex01/main.cpp @@ -1,35 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 07:23:15 by charles #+# #+# */ +/* Updated: 2020/04/13 09:18:04 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include <iostream> #include "PhoneBook.hpp" #include "Contact.hpp" +#include "utils.hpp" -int -main() +int main() { - std::string user_input; - size_t found_index; - PhoneBook phone_book = PhoneBook(); - - while (true) - { - std::cin >> user_input; - if (user_input == "EXIT") - break; - else if ((found_index = user_input.find("ADD")) == 0) - { - Contact tmp = Contact(user_input.substr(found_index)); - phone_book.add(tmp); - } - else if ((found_index = user_input.find("SEARCH")) == 0) - { - std::string searched = user_input.substr(found_index); - Contact *result = phone_book.search(searched); - if (result == NULL) - std::cout << "Notfound: " << searched << std::endl; - else - std::cout << "Found: " << result->name - << " phone: " << result->phone_str() << std::endl; + std::string input; + PhoneBook phoneBook; + int tmp; - } - } - return 0; + while (true) + { + std::cout << "> " << std::flush; + if (!std::getline(std::cin, input)) + break; + std::cout.flush(); + if (input == "EXIT") + break; + else if (input == "ADD") + { + if (phoneBook.getSize() >= 8) + std::cout << "Error: Phonebook is full" << std::endl; + else + phoneBook.add(Contact::prompt()); + std::cout << std::flush; + } + else if (input == "SEARCH") + { + std::cout << phoneBook << std::flush; + tmp = getInt(); + if (tmp < 0 || size_t(tmp) >= phoneBook.getSize()) + std::cout << "Error: Not valid index: " << tmp << std::endl; + else + phoneBook.get(tmp).put(); + std::cout << std::flush; + } + } + return 0; } diff --git a/cpp00/ex01/utils.cpp b/cpp00/ex01/utils.cpp new file mode 100644 index 0000000..27c7daa --- /dev/null +++ b/cpp00/ex01/utils.cpp @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 07:31:30 by charles #+# #+# */ +/* Updated: 2020/04/13 08:43:18 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "utils.hpp" + +int getInt() +{ + std::string s; + int tmp; + + std::cin >> s; + for (size_t i = 0; i < s.length(); i++) + if (!isdigit(s[i])) + return (-1); + std::istringstream(s) >> tmp; + return tmp; +} diff --git a/cpp00/ex01/utils.hpp b/cpp00/ex01/utils.hpp new file mode 100644 index 0000000..716befd --- /dev/null +++ b/cpp00/ex01/utils.hpp @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 07:31:38 by charles #+# #+# */ +/* Updated: 2020/04/13 09:07:18 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef UTILS_HPP +# define UTILS_HPP + +# include <iostream> +# include <sstream> +# include <string> +# include <cctype> + +int getInt(); + +#endif diff --git a/cpp01/ex00/Pony.cpp b/cpp01/ex00/Pony.cpp index f521d4b..61f3b47 100644 --- a/cpp01/ex00/Pony.cpp +++ b/cpp01/ex00/Pony.cpp @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Pony.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/13 09:06:06 by charles #+# #+# */ +/* Updated: 2020/04/13 09:06:11 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include <iostream> #include "Pony.hpp" |
