diff --git a/.vscode/settings.json b/.vscode/settings.json index 9061c9a7..a18f3ab9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -89,6 +89,11 @@ "xlocmes": "cpp", "complex": "cpp", "codecvt": "cpp", - "unordered_set": "cpp" - } + "unordered_set": "cpp", + "ranges": "cpp", + "cfenv": "cpp", + "typeindex": "cpp", + "__nullptr": "cpp" + }, + "cmake.sourceDirectory": "/home/thierno/Documents/m1_ubuntu/cpp/CPP_Exercises/tp03/3-hrsoft" } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000..05054c5c --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++ build active file", + "command": "/usr/bin/g++", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/tp01/ex1/main.cpp b/tp01/ex1/main.cpp index 8ccb0dc3..ccb9aaa4 100644 --- a/tp01/ex1/main.cpp +++ b/tp01/ex1/main.cpp @@ -1,13 +1,12 @@ #include #include +#include -int main() -{ +int main() { std::cout << "Entre ton nom: "; - - char name[20] = ""; - std::cin >> std::setw(20) >> name; - std::cout << "Bonjour Palluche La Falluche !" << std::endl; + std::string name; + std::cin >> name; + std::cout << name << std::endl; return 0; } diff --git a/tp01/ex2/main.cpp b/tp01/ex2/main.cpp index 6ea9fdbd..12c849cf 100644 --- a/tp01/ex2/main.cpp +++ b/tp01/ex2/main.cpp @@ -1,25 +1,53 @@ #include +#include -void ajoute_double(std::vector v) -{ - for (auto n : v) +void ajoute_double(std::vector& v) { + auto size = v.size(); + for (auto i = 0; i < size; i++) { - v.emplace_back(n * 2); + v.push_back(v[i] * 2); } } -int main() -{ - auto entiers = std::vector{1, 3, 3, 7}; - +void affiche(const std::vector& v) { std::cout << "Le tableau contient les valeurs :"; - for (int i = 0; i < entiers.size(); i++) + for (auto entier : v) { - std::cout << " " << entiers[i]; + std::cout << " " << entier; } std::cout << std::endl; +} +int main() { + auto entiers = std::vector {}; + auto value = 0; + while (1) + { + std::cin >> value; + if (std::cin.fail()) + { + std::cout << "Ceci n'est pas un entier !" << std::endl; + return 1; + } + if (value == 0) + { + break; + } + if (value < 0) + { + if (!entiers.empty()) + entiers.pop_back(); + } + else + { + entiers.emplace_back(value); + } + } + affiche(entiers); + if (!entiers.empty()) + std::cout << "first: " << entiers.front() << " last: " << entiers.back() << std::endl; ajoute_double(entiers); + affiche(entiers); return 0; } \ No newline at end of file diff --git a/tp01/ex3/main.cpp b/tp01/ex3/main.cpp index 51b85563..75be3fe2 100644 --- a/tp01/ex3/main.cpp +++ b/tp01/ex3/main.cpp @@ -1,8 +1,7 @@ #include "utils.hpp" -int main() -{ +int main() { print_hello(); - // print_bye(); + print_bye(); return 0; } diff --git a/tp01/ex3/utils.hpp b/tp01/ex3/utils.hpp index a8a61fa0..f0c0bffc 100644 --- a/tp01/ex3/utils.hpp +++ b/tp01/ex3/utils.hpp @@ -1,8 +1,8 @@ #include +#pragma once void print_hello(); -// void print_bye() -// { -// std::cout << "Bye" << std::endl; -// } +inline void print_bye() { + std::cout << "Bye" << std::endl; +} diff --git a/tp02/Card.cpp b/tp02/Card.cpp new file mode 100644 index 00000000..20face38 --- /dev/null +++ b/tp02/Card.cpp @@ -0,0 +1,61 @@ +#include "Card.hpp" + +#include + +Card::Card(CardValeur value, CardColor color) + : _value { value } + , _color { color } { +} + +Card::Card(unsigned int value, unsigned int color) + : Card{static_cast(value), static_cast(color)} { + +} + +void Card::print() const { + std::cout << Card::string_from_value(_value) << " de " << Card::string_from_color(_color) << std::endl; +} + +std::string Card::string_from_color(CardColor color) { + switch (color) + { + case CardColor::Coeur: + return "Coeur"; + case CardColor::Carreau: + return "Carreau"; + case CardColor::Trefle: + return "Trefle"; + case CardColor::Pique: + return "Pique"; + default: + return "?"; + } +} + +std::string Card::string_from_value(CardValeur value) { + switch (value) + { + case CardValeur::As: + return "AS"; + case CardValeur::Roi: + return "Roi"; + case CardValeur::Dame: + return "Dame"; + case CardValeur::Valet: + return "Valet"; + default: + return std::to_string(static_cast(value)); + } +} + +std::ostream& operator<<(std::ostream& stream, Card card) { + return stream << Card::string_from_value(card._value) << " de " << Card::string_from_color(card._color); +} + +bool Card::operator==(Card other) const { + return _value == other._value; +} + +bool Card::operator<(Card other) const { + return _value < other._value; +} \ No newline at end of file diff --git a/tp02/Card.hpp b/tp02/Card.hpp new file mode 100644 index 00000000..ba32347f --- /dev/null +++ b/tp02/Card.hpp @@ -0,0 +1,47 @@ +#include +#include +#pragma once + +enum class CardColor +{ + Coeur = 1, + Pique = 2, + Carreau = 3, + Trefle = 4 +}; + +enum class CardValeur +{ + Sept = 7, + Huit = 8, + Neuf = 9, + Dix = 10, + Valet = 11, + Dame = 12, + Roi = 13, + As = 14 +}; + +class Card +{ +public: + + Card(CardValeur value, CardColor color); + + Card(unsigned int value, unsigned int color); + + void print() const; + + friend std::ostream& operator<<(std::ostream& stream, Card card); + + bool operator==(Card other) const; + + bool operator<(Card other) const; + +private: + CardValeur _value; + CardColor _color; + static std::string string_from_color(CardColor color); + + static std::string string_from_value(CardValeur value); +}; \ No newline at end of file diff --git a/tp02/Player.cpp b/tp02/Player.cpp new file mode 100644 index 00000000..096062d4 --- /dev/null +++ b/tp02/Player.cpp @@ -0,0 +1,95 @@ +#include "Player.hpp" + +#include "Card.hpp" + +#include +#include +#include + +Player::Player(std::string name) + : _name { name } + , _score { 0u } {}; + +unsigned int Player::turn_number = 0u; + +void Player::deal_all_cards(Player& p1, Player& p2) { + std::vector all_cards; + for (unsigned int i = 7; i < 15; i++) + { + for (unsigned int j = 1; j < 5; j++) + { + all_cards.emplace_back(i, j); + } + } + + std::random_device rd; + std::shuffle(all_cards.begin(), all_cards.end(), std::default_random_engine(rd())); + auto milieu = all_cards.size() / 2; + p1._cards.assign(all_cards.begin(), all_cards.begin() + milieu); + p2._cards.assign(all_cards.begin() + milieu, all_cards.end()); +} + +Card Player::operator[](unsigned int index) const { + if (index < _cards.size()) + return _cards[index]; + std::cerr << "Problème d'index !!!" << std::endl; + exit(EXIT_FAILURE); +} + +unsigned int Player::get_score() const { + return _score; +} +std::string Player::get_name() const { + return _name; +} + +bool Player::play(Player& p1, Player& p2) { + std::cout << "Turn Number: " << turn_number + 1 << std::endl; + std::cout << "Carte de " << p1._name << ": " << p1._cards[turn_number] << std::endl; + std::cout << "Carte de " << p2._name << ": " << p2._cards[turn_number] << std::endl; + if (!(p1._cards[turn_number] == p2._cards[turn_number])) + { + if (p1._cards[turn_number] < p2._cards[turn_number]) + { + std::cout << p2._name << " a gagné 1 point." << std::endl; + p2._score++; + } + else + { + std::cout << p1._name << " a gagné 1 point." << std::endl; + p1._score++; + } + } + else { + turn_number++; + equality(p1, p2, 1); + } + turn_number++; + return turn_number >= 15; +} + +void Player::equality(Player& p1, Player& p2, int point_to_win) { + if (turn_number >= 15) { + return; + } + std::cout << "C'est une égalité, on rejoue !!" << std::endl; + std::cout << "Carte de " << p1._name << ": " << p1._cards[turn_number] << std::endl; + std::cout << "Carte de " << p2._name << ": " << p2._cards[turn_number] << std::endl; + if (!(p1._cards[turn_number] == p2._cards[turn_number])) + { + if (p1._cards[turn_number] < p2._cards[turn_number]) + { + std::cout << p2._name << " a gagné " << point_to_win + 1 << " points." << std::endl; + p2._score += point_to_win + 1; + } + else + { + std::cout << p1._name << " a gagné " << point_to_win + 1 << " points." << std::endl; + p1._score += point_to_win + 1; + } + } + else { + turn_number++; + equality(p1, p2, ++point_to_win); + } +} \ No newline at end of file diff --git a/tp02/Player.hpp b/tp02/Player.hpp new file mode 100644 index 00000000..0903831e --- /dev/null +++ b/tp02/Player.hpp @@ -0,0 +1,24 @@ +#include "Card.hpp" + +#include +#include +#include +#pragma once + +class Player +{ +public: + static unsigned int turn_number; + Player(std::string name); + static void deal_all_cards(Player& p1, Player& p2); + Card operator[](unsigned int index) const; + static bool play(Player& p1, Player& p2); + unsigned int get_score() const; + std::string get_name() const; + +private: + std::string _name; + std::vector _cards; + unsigned int _score; + static void equality(Player& p1, Player& p2, int point_to_win); +}; \ No newline at end of file diff --git a/tp02/main b/tp02/main new file mode 100755 index 00000000..3c004b9a Binary files /dev/null and b/tp02/main differ diff --git a/tp02/main.cpp b/tp02/main.cpp new file mode 100644 index 00000000..56064237 --- /dev/null +++ b/tp02/main.cpp @@ -0,0 +1,52 @@ +#include "Card.hpp" +#include "Player.hpp" + +#include +#include + +int main() { + // const Card c1 { CardValeur::Huit, CardColor::Pique }; + // c1.print(); + // std::cout << c1 << std::endl; + // const Card c2 { CardValeur::Huit, CardColor::Pique }; + // std::cout << (c2 == c1) << std::endl; // -> 1 + // const Card c3 { CardValeur::Dix, CardColor::Carreau }; + // std::cout << (c2 == c3) << std::endl; // -> 0 + + // std::cout << (c1 < c2) << std::endl; // -> 0 + // std::cout << (c1 < c3) << std::endl; // -> 1 + // std::cout << (c3 < c1) << std::endl; // -> 0 + + Player p1 { "Val" }; + Player p2 { "Rieu" }; + Player::deal_all_cards(p1, p2); + + // for (auto i = 0; i < 16; ++i) + // { + // p1[i].print(); + // std::cout << std::endl; + // p2[i].print(); + // std::cout << std::endl; + // } + + while (!Player::play(p1, p2)) + { + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + + if (p1.get_score() > p2.get_score()) + { + std::cout << p1.get_name() << " a gagné avec un score de " << p1.get_score() << std::endl; + } + else if (p1.get_score() < p2.get_score()) + { + std::cout << p2.get_name() << " a gagné avec un score de " << p2.get_score() << std::endl; + } + else + { + std::cout << "C'est une égalité avec un score de " << p1.get_score() << " pour chaque joueur" + << std::endl; + } + + return 0; +} diff --git a/tp03/3-hrsoft/HRSoft/Department.hpp b/tp03/3-hrsoft/HRSoft/Department.hpp index 14de8432..7fb836f7 100644 --- a/tp03/3-hrsoft/HRSoft/Department.hpp +++ b/tp03/3-hrsoft/HRSoft/Department.hpp @@ -2,6 +2,7 @@ #include "Employee.hpp" +#include #include #include @@ -9,11 +10,9 @@ class Department { public: Department(const std::string& name) - : _name { name } - {} + : _name { name } {} - Employee& add_employee(const std::string& name, unsigned int salary, Employee* manager) - { + Employee& add_employee(const std::string& name, unsigned int salary, Employee* manager) { auto& employee = _employees.emplace_back(name, salary); if (manager != nullptr) @@ -24,7 +23,38 @@ class Department return employee; } + void print_employees() const { + std::cout << "Employees of " << _name << " department:" << std::endl; + for (const auto& employee : _employees) + { + std::cout << " - " << employee << std::endl; + } + } + + const std::string& get_name() const { return _name; } + + void remove_employee(Employee& employee) { + for (auto& _employee : _employees) + { + _employee.remove_subordinate(employee); + } + _employees.remove(employee); + } + + const std::list& get_employees() const { return _employees; } + + Employee* find_employee(const std::string& name) { + for (auto& employee : _employees) + { + if (employee.get_name() == name) + { + return &employee; + } + } + return nullptr; + } + private: - std::string _name; + std::string _name; std::list _employees; }; diff --git a/tp03/3-hrsoft/HRSoft/Employee.hpp b/tp03/3-hrsoft/HRSoft/Employee.hpp index 4b017e2e..cb53d4ff 100644 --- a/tp03/3-hrsoft/HRSoft/Employee.hpp +++ b/tp03/3-hrsoft/HRSoft/Employee.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -8,28 +9,37 @@ class Employee { public: Employee(const std::string& name, unsigned int salary) - : _name { name }, _salary { salary } - {} + : _name { name } + , _salary { salary } {} - void add_subordinate(Employee& subordinate) - { - // TODO - Q3 - // ... - } + void add_subordinate(Employee& subordinate) { _subordinates.push_back(&subordinate); } friend std::ostream& operator<<(std::ostream&, const Employee&); + void print_subordinates() const { + std::cout << "Subordinates of " << _name << ":" << std::endl; + for (const auto& subordinate : _subordinates) + { + std::cout << " - " << *subordinate << std::endl; + } + } + + bool operator==(const Employee& other) const { return _name == other._name && _salary == other._salary; } + + void remove_subordinate(Employee& subordinate) { _subordinates.remove(&subordinate); } + + const std::string& get_name() const { return _name; } + + bool is_manager() const { return !_subordinates.empty(); } + private: - std::string _name; - unsigned int _salary = 0; + std::string _name; + unsigned int _salary = 0; std::list _subordinates; }; -inline std::ostream& operator<<(std::ostream& stream, const Employee& employee) -{ +inline std::ostream& operator<<(std::ostream& stream, const Employee& employee) { const auto is_manager = !employee._subordinates.empty(); - return stream << employee._name - << " (salary: " << employee._salary - << "| manager: " << (is_manager ? "yes" : "no") - << ")"; + return stream << employee._name << " (salary: " << employee._salary + << "| manager: " << (is_manager ? "yes" : "no") << ")"; } diff --git a/tp03/3-hrsoft/HRSoft/HRSoftSystem.hpp b/tp03/3-hrsoft/HRSoft/HRSoftSystem.hpp index 8c823138..73559893 100644 --- a/tp03/3-hrsoft/HRSoft/HRSoftSystem.hpp +++ b/tp03/3-hrsoft/HRSoft/HRSoftSystem.hpp @@ -1,6 +1,7 @@ #pragma once #include "Department.hpp" +#include "Employee.hpp" #include #include @@ -8,9 +9,76 @@ class HRSoftSystem { public: - Department& add_department(const std::string& name) - { - return _departments.emplace_back(name); + Department& add_department(const std::string& name) { return _departments.emplace_back(name); } + + void print_all_departments() const { + std::cout << "Departments:" << std::endl; + for (const auto& department : _departments) + { + std::cout << " - " << department.get_name() << std::endl; + } + } + + void print_all_employees() const { + std::cout << "Employees:" << std::endl; + for (const auto& department : _departments) + { + department.print_employees(); + } + } + + void remove_employee(Employee& employee) { + for (auto& department : _departments) + { + department.remove_employee(employee); + } + } + + void add_department(std::string& departement_name) { + for (auto& department : _departments) + { + if (department.get_name() == departement_name) + { + return; + } + } + _departments.emplace_back(departement_name); + } + + Department* find_department(const std::string& name) { + for (auto& department : _departments) + { + if (department.get_name() == name) + { + return &department; + } + } + return nullptr; + } + + Employee* find_employee(const std::string& name) { + for (auto& department : _departments) + { + if (auto* employee = department.find_employee(name)) + { + return employee; + } + } + return nullptr; + } + + void list_managers() const { + std::cout << "Managers:" << std::endl; + for (const auto& department : _departments) + { + for (const auto& employee : department.get_employees()) + { + if (!employee.is_manager()) + { + std::cout << " - " << employee << std::endl; + } + } + } } private: diff --git a/tp03/3-hrsoft/HRSoftMain b/tp03/3-hrsoft/HRSoftMain new file mode 100755 index 00000000..936f0453 Binary files /dev/null and b/tp03/3-hrsoft/HRSoftMain differ diff --git a/tp03/3-hrsoft/HRSoftMain.cpp b/tp03/3-hrsoft/HRSoftMain.cpp index 5e0c6589..7200201c 100644 --- a/tp03/3-hrsoft/HRSoftMain.cpp +++ b/tp03/3-hrsoft/HRSoftMain.cpp @@ -1,38 +1,34 @@ #include "HRSoft/HRSoftSystem.hpp" #include -#include #include +#include #include -std::stringstream get_next_line() -{ +std::stringstream get_next_line() { auto line = std::string {}; std::getline(std::cin, line); return std::stringstream { line }; } -std::string parse_string(std::istream& stream) -{ +std::string parse_string(std::istream& stream) { auto name = std::string {}; stream >> name; return name; } -unsigned int parse_value(std::istream& stream) -{ +unsigned int parse_value(std::istream& stream) { auto salary = 0u; stream >> salary; return salary; } -int main() -{ +int main() { std::cout << "Welcome in HRSoft!" << std::endl; - auto system = HRSoftSystem {}; + auto system = HRSoftSystem {}; auto command = ' '; - + while (command != 'q') { std::cout << "What do you want to do? (tap 'h' for help)" << std::endl; @@ -42,63 +38,59 @@ int main() switch (command) { - case 'd': - std::cout << "Not implemented yet" << std::endl; - // system.add_department(parse_string(next_line)); - break; - - case 'l': - std::cout << "Not implemented yet" << std::endl; - // system.print_all_departments(); - break; - - case 'e': - std::cout << "Not implemented yet" << std::endl; - // if (auto* dpt = system.find_department(parse_string(next_line))) - // { - // auto name = parse_string(next_line); - // auto salary = parse_value(next_line); - // auto* manager = system.find_employee(parse_string(next_line)); - // dpt->add_employee(name, salary, manager); - // } - break; - - case 'k': - std::cout << "Not implemented yet" << std::endl; - // system.print_all_employees(); - break; - - case 'f': - std::cout << "Not implemented yet" << std::endl; - // if (auto* employee = system.find_employee(parse_string(next_line))) - // { - // system.remove_employee(*employee); - // } - break; - - case 'n': - std::cout << "Not implemented yet" << std::endl; - // ... - break; - - case 'm': - std::cout << "Not implemented yet" << std::endl; - // ... - break; - - case 't': - std::cout << "Not implemented yet" << std::endl; - // ... - break; - - case 's': - std::cout << "Not implemented yet" << std::endl; - // ... - break; - - default: - std::cout << "Unknown command " << command << std::endl; - break; + case 'd': + system.add_department(parse_string(next_line)); + break; + + case 'l': + system.print_all_departments(); + break; + + case 'e': + if (auto* dpt = system.find_department(parse_string(next_line))) + { + auto name = parse_string(next_line); + auto salary = parse_value(next_line); + auto* manager = system.find_employee(parse_string(next_line)); + dpt->add_employee(name, salary, manager); + } + break; + + case 'k': + system.print_all_employees(); + break; + + case 'f': + if (auto* employee = system.find_employee(parse_string(next_line))) + { + system.remove_employee(*employee); + } + break; + + case 'n': + if (auto* departement = system.find_department(parse_string(next_line))) + { + departement->print_employees(); + } + break; + + case 'm': + system.list_managers(); + break; + + case 't': + std::cout << "Not implemented yet" << std::endl; + // ... + break; + + case 's': + std::cout << "Not implemented yet" << std::endl; + // ... + break; + + default: + std::cout << "Unknown command " << command << std::endl; + break; } } diff --git a/tp03/3-hrsoft/HRSoftTests b/tp03/3-hrsoft/HRSoftTests new file mode 100755 index 00000000..fc7464ec Binary files /dev/null and b/tp03/3-hrsoft/HRSoftTests differ diff --git a/tp03/3-hrsoft/HRSoftTests.cpp b/tp03/3-hrsoft/HRSoftTests.cpp index dc33cef9..e48f9511 100644 --- a/tp03/3-hrsoft/HRSoftTests.cpp +++ b/tp03/3-hrsoft/HRSoftTests.cpp @@ -1,31 +1,30 @@ #include "HRSoft/HRSoftSystem.hpp" -int main() -{ +int main() { auto system = HRSoftSystem {}; - auto& rd_dpt = system.add_department("R&D"); + auto& rd_dpt = system.add_department("R&D"); auto& market_dpt = system.add_department("Marketing"); auto& charline = rd_dpt.add_employee("Charline", 6000, nullptr); - auto& jacques = rd_dpt.add_employee("Jacques", 2500, &charline); - auto& paul = market_dpt.add_employee("Paul", 2500, &charline); + auto& jacques = rd_dpt.add_employee("Jacques", 2500, &charline); + auto& paul = market_dpt.add_employee("Paul", 2500, &charline); // TODO Q2. // Affiche différentes informations du système. - // rd_dpt.print_employees(); - // system.print_all_departments(); - // system.print_all_employees(); + rd_dpt.print_employees(); + system.print_all_departments(); + system.print_all_employees(); // TODO Q3. // Affiche tous les employés managés par Charline. - // charline.print_subordinates(); + charline.print_subordinates(); // TODO Q4. // Jacques est licencié... - // system.remove_employee(jacques); - // rd_dpt.print_employees(); - // charline.print_subordinates(); + system.remove_employee(jacques); + rd_dpt.print_employees(); + charline.print_subordinates(); return 0; } diff --git a/tp03/TP3.md b/tp03/TP3.md index b4c85858..34eda2ea 100644 --- a/tp03/TP3.md +++ b/tp03/TP3.md @@ -45,7 +45,8 @@ int main() ![](images/ex1-a.svg) -1. Pourquoi n'y a-t-il pas de relation entre `last_wheel` et `wheels[3]` contrairement à `first_wheel` et `wheels[0]` ? +1. Pourquoi n'y a-t-il pas de relation entre `last_wheel` et `wheels[3]` contrairement à `first_wheel` et `wheels[0]` ? + - Parce que `last_wheel` est une copie de `wheels.back()`, alors que `first_wheel` est une référence à `wheels.front()`. ### Cas B - Pointeurs-observants @@ -73,8 +74,11 @@ int main() ![](images/ex1-b.svg) 1. Dans le graphe d'ownership, comment distingue-t-on un pointeur d'une référence ? + - Non. 2. Comment est représenté un pointeur nul ? + - Par un pointeur qui pointe vers un rond rempli d'une croix. 3. En termes de code, quelles sont les deux différences principales entre un pointeur-observant et une référence ? + - Un pointeur-observant peut être nul, et peut être réaffecté. ### Cas C - Insertions dans un `std::vector` @@ -110,10 +114,45 @@ Lors d'une insertion, si le buffer mémoire réservé par `std::vector` n'a pas Chaque élément est déplacé de son ancienne adresse mémoire vers la nouvelle. 1. Essayez de représenter les transitions dans le graphe d'ownership après le dernier `push_back` si celui-ci déclenchait une réallocation mémoire. + - ![](images/CasC1.png) 2. Quel problème relève-t-on dans le graphe ? + - `first_product` est une référence sur un élément qui a été déplacé lors de la réallocation mémoire, et pointe donc vers une zone mémoire invalide. 3. Modifiez le code ci-dessus afin que `products` contienne des pointeurs ownants. Pensez à ajouter un destructeur à `Client` pour libérer la mémoire allouée dynamiquement. + - ```cpp + #include + #include + + struct Product + {}; + + struct Client + { + ~Client() + { + for (auto product : products) + { + delete product; + } + } + std::vector products; + }; + + int main() + { + auto client = Client {}; + + client.products.push_back(new Product{}); + client.products.push_back(new Product{}); + + auto first_product = client.products.front(); + + client.products.push_back(new Product{}); + return 0; + } + ``` 4. Redessinez le graphe d'ownership de la question 1, mais en prenant en compte vos changements dans le code. 5. Avez-vous toujours le problème relevé à la question 2 ? + - Oui on a toujours le problème en cas de réallocation du vector. ## Exercice 2 - La meilleure signature (15min) @@ -122,18 +161,15 @@ Chaque élément est déplacé de son ancienne adresse mémoire vers la nouvelle ```cpp #include -XX add(XX a, XX b) -{ +int add(const int& a, const int& b) { return a + b; } -XX add_to(XX a, XX b) -{ +void add_to(int& a, const int& b) { a += b; } -int main() -{ +int main() { int x = 10; int y = add(x, x); add_to(y, 22); @@ -146,7 +182,7 @@ int main() Vous pouvez vous aider des commentaires pour comprendre comment les fonctions utilisent leurs paramètres. ```cpp // Return the number of occurrences of 'a' found in string 's'. -int count_a_occurrences(std::string s); +int count_a_occurrences(const std::string& s); // Update function of a rendering program. // - dt (delta time) is read by the function to know the time elapsed since the last frame. @@ -160,11 +196,11 @@ void update_loop(const float& dt, std::string& errors_out); // -> res is false, since not all values are positive // -> negative_indices contains { 1, 3 } because values[1] = -2 and values[3] = -4 // -> negative_count is 2 -bool are_all_positives(std::vector values, int negative_indices_out[], size_t& negative_count_out); +bool are_all_positives(const std::vector& values, int negative_indices_out[], size_t& negative_count_out); // Concatenate 'str1' and 'str2' and return the result. // The input parameters are not modified by the function. -std::string concatenate(char* str1, char* str2); +std::string concatenate(const char* str1, const char* str2); ``` ## Exercice 3 - Gestion des resources (55min) @@ -243,6 +279,7 @@ Ce programme instancie les mêmes données que celles présentées dans les sch Pour quelle raison le programme utilise-t-il des `std::list` plutôt que des `std::vector` pour stocker les départements et les employés ? Si vous ne trouvez pas, remplacez les `list` par des `vector` et lancez le programme en mode Debug pour observer le comportement. En particulier, utilisez le debugger pour surveiller le contenu de `rd_dpt`. + - Car on ne veut pas que les éléments soient déplacés lors de l'ajout d'un nouvel élément vu que les pointeurs-observants pointent vers les éléments de ces listes. 2. Implémentez la fonction `print_employees` dans la classe `Department` (notez que `operator<<` est déjà fourni pour `Employee`). Faites de même pour `print_all_departments` et `print_all_employees` dans la classe `HRSoftSystem`. diff --git a/tp03/exo2 b/tp03/exo2 new file mode 100755 index 00000000..843eaf35 Binary files /dev/null and b/tp03/exo2 differ diff --git a/tp03/exo2.cpp b/tp03/exo2.cpp new file mode 100644 index 00000000..36e89d94 --- /dev/null +++ b/tp03/exo2.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +// Return the number of occurrences of 'a' found in string 's'. +int count_a_occurrences(const std::string& s); + +// Update function of a rendering program. +// - dt (delta time) is read by the function to know the time elapsed since the last frame. +// - errors is a string filled by the function to indicate what errors have occured. +void update_loop(const float& dt, std::string& errors_out); + +// Return whether all numbers in 'values' are positive. +// If there are negative values in it, fill the array 'negative_indices_out' with the indices +// of these values and set its size in 'negative_count_out'. +// ex: auto res = are_all_positive({ 1, -2, 3, -4 }, negative_indices, negative_count); +// -> res is false, since not all values are positive +// -> negative_indices contains { 1, 3 } because values[1] = -2 and values[3] = -4 +// -> negative_count is 2 +bool are_all_positives(const std::vector& values, int negative_indices_out[], + size_t& negative_count_out) { + negative_count_out = 0; + for (auto i = 0; i < values.size(); ++i) + { + if (values[i] < 0) + { + negative_indices_out[negative_count_out] = i; + negative_count_out++; + } + } + return negative_count_out == 0; +} + +// Concatenate 'str1' and 'str2' and return the result. +// The input parameters are not modified by the function. +std::string concatenate(const char* str1, const char* str2) { + return std::string(str1) + str2; +} + +int main(int argc, char const* argv[]) { + // std::vector values = { 1, -2, 3, -4 }; + // int negative_indices[10]; + // size_t negative_count = 0; + + // bool result = are_all_positives(values, negative_indices, negative_count); + + // std::cout << "Test are_all_positives: " << (result ? "Passed" : "Failed") << std::endl; + // std::cout << "Negative indices: "; + // for (size_t i = 0; i < negative_count; ++i) + // { + // std::cout << negative_indices[i] << " "; + // } + // std::cout << std::endl; + // std::cout << "Negative count: " << negative_count << std::endl; + + const char* str1 = "Hello, "; + const char* str2 = "World!"; + std::string result = concatenate(str1, str2); + std::cout << "Concatenate result: " << result << std::endl; + + return 0; +} diff --git a/tp03/images/CasC1.png b/tp03/images/CasC1.png new file mode 100644 index 00000000..a20ff827 Binary files /dev/null and b/tp03/images/CasC1.png differ diff --git a/tp03/images/CasC1.svg b/tp03/images/CasC1.svg new file mode 100644 index 00000000..a7cac36d --- /dev/null +++ b/tp03/images/CasC1.svg @@ -0,0 +1,4 @@ + + + +
main
main
client
client
first_product
first_product
products
products
products[0]
products[0]
products[1]
products[1]
products[2]
products[2]
??
??
Text is not SVG - cannot display
\ No newline at end of file diff --git a/tp04/.vscode/settings.json b/tp04/.vscode/settings.json index 503c5cdc..1e262943 100644 --- a/tp04/.vscode/settings.json +++ b/tp04/.vscode/settings.json @@ -87,7 +87,10 @@ "xtr1common": "cpp", "xtree": "cpp", "xutility": "cpp", - "span": "cpp" + "span": "cpp", + "codecvt": "cpp", + "ranges": "cpp", + "__nullptr": "cpp" }, "editor.formatOnSave": true, "git.requireGitUserConfig": false, diff --git a/tp04/backup/ex1-qcm/qcm.hpp b/tp04/backup/ex1-qcm/qcm.hpp new file mode 100644 index 00000000..379ccc12 --- /dev/null +++ b/tp04/backup/ex1-qcm/qcm.hpp @@ -0,0 +1,54 @@ +//////////////////////////////////////////////////////////////////////////////////////// +// +// Vous devez indiquer si chacune des propositions est vraie (true) ou fausse (false). +// +// Exemple : Le C++ est le meilleur langage de tous les temps. +#define ANSWER_EX1 true +// ^^^^ indiquez votre réponse ici +// +// Exemple : Le Java est le meilleur langage de tous les temps. +#define ANSWER_EX2 false +// ^^^^^ +// +//////////////////////////////////////////////////////////////////////////////////////// + +// Pour indiquer qu'une fonction-membre ne modifie pas l'état de l'objet, on écrit const +// devant le type de retour de la fonction. +#define ANSWER_1 false + +// Le type char* est le mieux adapté pour représenter des chaînes de caractères. +#define ANSWER_2 false + +// Dans une classe, il ne peut pas avoir plus d'un bloc `public` et un bloc `private`. +#define ANSWER_3 false + +// Le littéral 0u est de type unsigned int. +#define ANSWER_4 true + +// Une variable booléenne se définit avec le type `boolean`. +#define ANSWER_5 false + +// On peut utiliser `inline` pour définir des attributs statiques directement dans la classe. +#define ANSWER_6 true + +// Si une fonction est définie dans deux fichiers-objets différents, l'erreur se produit lors de la phase de +// build. +#define ANSWER_7 false + +// Le mot-clef `auto` sert à définir des variables sans préciser leur valeur initiale. +#define ANSWER_8 false + +// Le type `std::vector` sert à créer des tableaux dynamiques. +#define ANSWER_9 true + +// Il faut toujours penser à appeler `delete` sur les pointeurs observants. +#define ANSWER_10 false + +// Si une classe `MyClass` a un attribut `std::string& _name` alors, par défaut, +// la destruction d'une instance o de `MyClass` détruit la chaîne de caractère référencée par o._name. +#define ANSWER_11 true + +// Si une classe `MyBoxingClass` a un attribut `std::vector _data` alors, par défaut, +// la destruction d'une instance o de `MyBoxingClass` provoque la destruction de toutes les instances de +// `MyBoxedClass` contenues dans o._data. +#define ANSWER_12 true diff --git a/tp04/backup/ex2-01-phone-number/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-01-phone-number/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-01-phone-number/PhoneBook.hpp b/tp04/backup/ex2-01-phone-number/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-01-phone-number/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-01-phone-number/PhoneBookEntry.hpp b/tp04/backup/ex2-01-phone-number/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-01-phone-number/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-01-phone-number/PhoneNumber.hpp b/tp04/backup/ex2-01-phone-number/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-01-phone-number/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-02-is-valid/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-02-is-valid/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-02-is-valid/PhoneBook.hpp b/tp04/backup/ex2-02-is-valid/PhoneBook.hpp new file mode 100644 index 00000000..529a15ff --- /dev/null +++ b/tp04/backup/ex2-02-is-valid/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (const auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (const auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-02-is-valid/PhoneBookEntry.hpp b/tp04/backup/ex2-02-is-valid/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-02-is-valid/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-02-is-valid/PhoneNumber.hpp b/tp04/backup/ex2-02-is-valid/PhoneNumber.hpp new file mode 100644 index 00000000..32e34747 --- /dev/null +++ b/tp04/backup/ex2-02-is-valid/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (const auto nb : _nbrs) + { + if (nb < 0 || nb > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-03-const-is-valid/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-03-const-is-valid/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-03-const-is-valid/PhoneBook.hpp b/tp04/backup/ex2-03-const-is-valid/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-03-const-is-valid/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-03-const-is-valid/PhoneBookEntry.hpp b/tp04/backup/ex2-03-const-is-valid/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-03-const-is-valid/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-03-const-is-valid/PhoneNumber.hpp b/tp04/backup/ex2-03-const-is-valid/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-03-const-is-valid/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-04-index/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-04-index/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-04-index/PhoneBook.hpp b/tp04/backup/ex2-04-index/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-04-index/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-04-index/PhoneBookEntry.hpp b/tp04/backup/ex2-04-index/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-04-index/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-04-index/PhoneNumber.hpp b/tp04/backup/ex2-04-index/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-04-index/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-05-index-out/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-05-index-out/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-05-index-out/PhoneBook.hpp b/tp04/backup/ex2-05-index-out/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-05-index-out/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-05-index-out/PhoneBookEntry.hpp b/tp04/backup/ex2-05-index-out/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-05-index-out/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-05-index-out/PhoneNumber.hpp b/tp04/backup/ex2-05-index-out/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-05-index-out/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-06-const-index/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-06-const-index/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-06-const-index/PhoneBook.hpp b/tp04/backup/ex2-06-const-index/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-06-const-index/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-06-const-index/PhoneBookEntry.hpp b/tp04/backup/ex2-06-const-index/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-06-const-index/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-06-const-index/PhoneNumber.hpp b/tp04/backup/ex2-06-const-index/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-06-const-index/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-07-multi-inclusions/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-07-multi-inclusions/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-07-multi-inclusions/PhoneBook.hpp b/tp04/backup/ex2-07-multi-inclusions/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-07-multi-inclusions/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-07-multi-inclusions/PhoneBookEntry.hpp b/tp04/backup/ex2-07-multi-inclusions/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-07-multi-inclusions/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-07-multi-inclusions/PhoneNumber.hpp b/tp04/backup/ex2-07-multi-inclusions/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-07-multi-inclusions/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-11-print-out/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-11-print-out/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-11-print-out/PhoneBook.hpp b/tp04/backup/ex2-11-print-out/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-11-print-out/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-11-print-out/PhoneBookEntry.hpp b/tp04/backup/ex2-11-print-out/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-11-print-out/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-11-print-out/PhoneNumber.hpp b/tp04/backup/ex2-11-print-out/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-11-print-out/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-12-print-any-stream/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-12-print-any-stream/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-12-print-any-stream/PhoneBook.hpp b/tp04/backup/ex2-12-print-any-stream/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-12-print-any-stream/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-12-print-any-stream/PhoneBookEntry.hpp b/tp04/backup/ex2-12-print-any-stream/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-12-print-any-stream/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-12-print-any-stream/PhoneNumber.hpp b/tp04/backup/ex2-12-print-any-stream/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-12-print-any-stream/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-13-print-concat/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-13-print-concat/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-13-print-concat/PhoneBook.hpp b/tp04/backup/ex2-13-print-concat/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-13-print-concat/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-13-print-concat/PhoneBookEntry.hpp b/tp04/backup/ex2-13-print-concat/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-13-print-concat/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-13-print-concat/PhoneNumber.hpp b/tp04/backup/ex2-13-print-concat/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-13-print-concat/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-21-phone-book-entry/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-21-phone-book-entry/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-21-phone-book-entry/PhoneBook.hpp b/tp04/backup/ex2-21-phone-book-entry/PhoneBook.hpp new file mode 100644 index 00000000..529a15ff --- /dev/null +++ b/tp04/backup/ex2-21-phone-book-entry/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (const auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (const auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-21-phone-book-entry/PhoneBookEntry.hpp b/tp04/backup/ex2-21-phone-book-entry/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-21-phone-book-entry/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-21-phone-book-entry/PhoneNumber.hpp b/tp04/backup/ex2-21-phone-book-entry/PhoneNumber.hpp new file mode 100644 index 00000000..32e34747 --- /dev/null +++ b/tp04/backup/ex2-21-phone-book-entry/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (const auto nb : _nbrs) + { + if (nb < 0 || nb > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-22-accessors/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-22-accessors/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-22-accessors/PhoneBook.hpp b/tp04/backup/ex2-22-accessors/PhoneBook.hpp new file mode 100644 index 00000000..529a15ff --- /dev/null +++ b/tp04/backup/ex2-22-accessors/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (const auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (const auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-22-accessors/PhoneBookEntry.hpp b/tp04/backup/ex2-22-accessors/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-22-accessors/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-22-accessors/PhoneNumber.hpp b/tp04/backup/ex2-22-accessors/PhoneNumber.hpp new file mode 100644 index 00000000..32e34747 --- /dev/null +++ b/tp04/backup/ex2-22-accessors/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (const auto nb : _nbrs) + { + if (nb < 0 || nb > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-23-return-refs/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-23-return-refs/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-23-return-refs/PhoneBook.hpp b/tp04/backup/ex2-23-return-refs/PhoneBook.hpp new file mode 100644 index 00000000..529a15ff --- /dev/null +++ b/tp04/backup/ex2-23-return-refs/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (const auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (const auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-23-return-refs/PhoneBookEntry.hpp b/tp04/backup/ex2-23-return-refs/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-23-return-refs/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-23-return-refs/PhoneNumber.hpp b/tp04/backup/ex2-23-return-refs/PhoneNumber.hpp new file mode 100644 index 00000000..32e34747 --- /dev/null +++ b/tp04/backup/ex2-23-return-refs/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (const auto nb : _nbrs) + { + if (nb < 0 || nb > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-24-const-accessors/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-24-const-accessors/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-24-const-accessors/PhoneBook.hpp b/tp04/backup/ex2-24-const-accessors/PhoneBook.hpp new file mode 100644 index 00000000..529a15ff --- /dev/null +++ b/tp04/backup/ex2-24-const-accessors/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (const auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (const auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-24-const-accessors/PhoneBookEntry.hpp b/tp04/backup/ex2-24-const-accessors/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-24-const-accessors/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-24-const-accessors/PhoneNumber.hpp b/tp04/backup/ex2-24-const-accessors/PhoneNumber.hpp new file mode 100644 index 00000000..32e34747 --- /dev/null +++ b/tp04/backup/ex2-24-const-accessors/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (const auto nb : _nbrs) + { + if (nb < 0 || nb > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-25-equality/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-25-equality/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-25-equality/PhoneBook.hpp b/tp04/backup/ex2-25-equality/PhoneBook.hpp new file mode 100644 index 00000000..529a15ff --- /dev/null +++ b/tp04/backup/ex2-25-equality/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (const auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (const auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-25-equality/PhoneBookEntry.hpp b/tp04/backup/ex2-25-equality/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-25-equality/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-25-equality/PhoneNumber.hpp b/tp04/backup/ex2-25-equality/PhoneNumber.hpp new file mode 100644 index 00000000..32e34747 --- /dev/null +++ b/tp04/backup/ex2-25-equality/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (const auto nb : _nbrs) + { + if (nb < 0 || nb > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-26-const-equality/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-26-const-equality/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-26-const-equality/PhoneBook.hpp b/tp04/backup/ex2-26-const-equality/PhoneBook.hpp new file mode 100644 index 00000000..529a15ff --- /dev/null +++ b/tp04/backup/ex2-26-const-equality/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (const auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (const auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-26-const-equality/PhoneBookEntry.hpp b/tp04/backup/ex2-26-const-equality/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-26-const-equality/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-26-const-equality/PhoneNumber.hpp b/tp04/backup/ex2-26-const-equality/PhoneNumber.hpp new file mode 100644 index 00000000..32e34747 --- /dev/null +++ b/tp04/backup/ex2-26-const-equality/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (const auto nb : _nbrs) + { + if (nb < 0 || nb > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-31-phone-book/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-31-phone-book/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-31-phone-book/PhoneBook.hpp b/tp04/backup/ex2-31-phone-book/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-31-phone-book/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-31-phone-book/PhoneBookEntry.hpp b/tp04/backup/ex2-31-phone-book/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-31-phone-book/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-31-phone-book/PhoneNumber.hpp b/tp04/backup/ex2-31-phone-book/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-31-phone-book/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-32-add-entry/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-32-add-entry/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-32-add-entry/PhoneBook.hpp b/tp04/backup/ex2-32-add-entry/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-32-add-entry/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-32-add-entry/PhoneBookEntry.hpp b/tp04/backup/ex2-32-add-entry/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-32-add-entry/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-32-add-entry/PhoneNumber.hpp b/tp04/backup/ex2-32-add-entry/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-32-add-entry/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-33-get-entry/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-33-get-entry/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-33-get-entry/PhoneBook.hpp b/tp04/backup/ex2-33-get-entry/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-33-get-entry/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-33-get-entry/PhoneBookEntry.hpp b/tp04/backup/ex2-33-get-entry/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-33-get-entry/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-33-get-entry/PhoneNumber.hpp b/tp04/backup/ex2-33-get-entry/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-33-get-entry/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-35-unique-entry/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-35-unique-entry/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-35-unique-entry/PhoneBook.hpp b/tp04/backup/ex2-35-unique-entry/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-35-unique-entry/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-35-unique-entry/PhoneBookEntry.hpp b/tp04/backup/ex2-35-unique-entry/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-35-unique-entry/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-35-unique-entry/PhoneNumber.hpp b/tp04/backup/ex2-35-unique-entry/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-35-unique-entry/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex2-36-no-desinstanciation/PLACER_VOTRE_CODE_ICI.txt b/tp04/backup/ex2-36-no-desinstanciation/PLACER_VOTRE_CODE_ICI.txt new file mode 100644 index 00000000..e69de29b diff --git a/tp04/backup/ex2-36-no-desinstanciation/PhoneBook.hpp b/tp04/backup/ex2-36-no-desinstanciation/PhoneBook.hpp new file mode 100644 index 00000000..61744e31 --- /dev/null +++ b/tp04/backup/ex2-36-no-desinstanciation/PhoneBook.hpp @@ -0,0 +1,43 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(PhoneBookEntry phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + + return false; + } + for (auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-36-no-desinstanciation/PhoneBookEntry.hpp b/tp04/backup/ex2-36-no-desinstanciation/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/backup/ex2-36-no-desinstanciation/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/backup/ex2-36-no-desinstanciation/PhoneNumber.hpp b/tp04/backup/ex2-36-no-desinstanciation/PhoneNumber.hpp new file mode 100644 index 00000000..b800ddb4 --- /dev/null +++ b/tp04/backup/ex2-36-no-desinstanciation/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (int i = 0; i < 5; i++) + { + if (_nbrs[i] < 0 || _nbrs[i] > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/backup/ex3-01-types/01-types.hpp b/tp04/backup/ex3-01-types/01-types.hpp new file mode 100644 index 00000000..55d6b4a7 --- /dev/null +++ b/tp04/backup/ex3-01-types/01-types.hpp @@ -0,0 +1,47 @@ +/////////////////////////////////////////////////////////////////////////////////////// +// +// Choisissez les types les plus appropriés pour que les fonctions puissent faire leur travail de la manière +// la plus sécurisée et optimisée possible. +// Attention donc aux copies inutiles sur les types coûteux à copier et à si la fonction est censée modifier +// le paramètre ou pas. +// +// Exemple +// get_char retourne un caractère +// ANSWER_EX get_char(); +#define ANSWER_EX char +// ^^^^ -> indiquez votre réponse ici +// +/////////////////////////////////////////////////////////////////////////////////////// + +// Si vous devez ajouter des directives d'inclusion pour faire compiler le test, vous pouvez dupliquer et +// décommenter la ligne ci-dessous. +#include +#include + +// Question 1 +// stringify_integer convertit un entier en chaîne de caractères +// ANSWER_1A stringify_integer(ANSWER_1B integer); +#define ANSWER_1A std::string +#define ANSWER_1B int + +// Question 2 +// count_nb_elements permet de retourner le nombre d'éléments présents dans un tableau dynamique +// ANSWER_2A count_nb_elements(ANSWER_2B elements); +#define ANSWER_2A unsigned int +#define ANSWER_2B const std::vector& + +// Question 3 +// WrappedString permet de stocker une chaîne et d'encapsuler ses accès en lecture et en écriture +// Pour le constructeur, vous devez indiquer le type du paramètre qui permettra d'intialiser _content: +// WrappedString(ANSWER_3A initial_content) +#define ANSWER_3A const std::string& + +// La fonction get_readonly_content() doit donner un accès en lecture seule à _content +// ANSWER_3B get_readonly_content() ANSWER_3C { return _content; } +#define ANSWER_3B const std::string& +#define ANSWER_3C const + +// La fonction get_modifiable_content() doit donner un accès en lecture et écriture à _content +// ANSWER_3D get_modifiable_content() ANSWER_3E { return _content; } +#define ANSWER_3D std::string& +#define ANSWER_3E diff --git a/tp04/backup/ex3-01-types/02-vector.hpp b/tp04/backup/ex3-01-types/02-vector.hpp new file mode 100644 index 00000000..572939dc --- /dev/null +++ b/tp04/backup/ex3-01-types/02-vector.hpp @@ -0,0 +1,25 @@ +// Question 1 +// Quelle est la fonction qui permet d'obtenir le nombre d'éléments dans un vector ? +// auto nb_elements = values.ANSWER_1(); +#define ANSWER_1 size + +// Question 2 +// Comment écrit-on une boucle permettant d'itérer sur tous les éléments d'un vector ? +// ANSWER_2A (auto value ANSWER_2B values) +#define ANSWER_2A for +#define ANSWER_2B : + +// Question 3 +// Comment accède-t-on au dernier élément d'un vector ? +// auto last_value = values.ANSWER_3(); +#define ANSWER_3 back + +// Question 4 +// Comment ajoute-t-on un élément dans un vector ? +// values.ANSWER_4(new_value); +#define ANSWER_4 emplace_back + +// Question 5 +// Comment supprime-t-on tous les éléments d'un vector ? +// values.ANSWER_5(); +#define ANSWER_5 clear diff --git a/tp04/backup/ex3-02-vector/01-types.hpp b/tp04/backup/ex3-02-vector/01-types.hpp new file mode 100644 index 00000000..55d6b4a7 --- /dev/null +++ b/tp04/backup/ex3-02-vector/01-types.hpp @@ -0,0 +1,47 @@ +/////////////////////////////////////////////////////////////////////////////////////// +// +// Choisissez les types les plus appropriés pour que les fonctions puissent faire leur travail de la manière +// la plus sécurisée et optimisée possible. +// Attention donc aux copies inutiles sur les types coûteux à copier et à si la fonction est censée modifier +// le paramètre ou pas. +// +// Exemple +// get_char retourne un caractère +// ANSWER_EX get_char(); +#define ANSWER_EX char +// ^^^^ -> indiquez votre réponse ici +// +/////////////////////////////////////////////////////////////////////////////////////// + +// Si vous devez ajouter des directives d'inclusion pour faire compiler le test, vous pouvez dupliquer et +// décommenter la ligne ci-dessous. +#include +#include + +// Question 1 +// stringify_integer convertit un entier en chaîne de caractères +// ANSWER_1A stringify_integer(ANSWER_1B integer); +#define ANSWER_1A std::string +#define ANSWER_1B int + +// Question 2 +// count_nb_elements permet de retourner le nombre d'éléments présents dans un tableau dynamique +// ANSWER_2A count_nb_elements(ANSWER_2B elements); +#define ANSWER_2A unsigned int +#define ANSWER_2B const std::vector& + +// Question 3 +// WrappedString permet de stocker une chaîne et d'encapsuler ses accès en lecture et en écriture +// Pour le constructeur, vous devez indiquer le type du paramètre qui permettra d'intialiser _content: +// WrappedString(ANSWER_3A initial_content) +#define ANSWER_3A const std::string& + +// La fonction get_readonly_content() doit donner un accès en lecture seule à _content +// ANSWER_3B get_readonly_content() ANSWER_3C { return _content; } +#define ANSWER_3B const std::string& +#define ANSWER_3C const + +// La fonction get_modifiable_content() doit donner un accès en lecture et écriture à _content +// ANSWER_3D get_modifiable_content() ANSWER_3E { return _content; } +#define ANSWER_3D std::string& +#define ANSWER_3E diff --git a/tp04/backup/ex3-02-vector/02-vector.hpp b/tp04/backup/ex3-02-vector/02-vector.hpp new file mode 100644 index 00000000..572939dc --- /dev/null +++ b/tp04/backup/ex3-02-vector/02-vector.hpp @@ -0,0 +1,25 @@ +// Question 1 +// Quelle est la fonction qui permet d'obtenir le nombre d'éléments dans un vector ? +// auto nb_elements = values.ANSWER_1(); +#define ANSWER_1 size + +// Question 2 +// Comment écrit-on une boucle permettant d'itérer sur tous les éléments d'un vector ? +// ANSWER_2A (auto value ANSWER_2B values) +#define ANSWER_2A for +#define ANSWER_2B : + +// Question 3 +// Comment accède-t-on au dernier élément d'un vector ? +// auto last_value = values.ANSWER_3(); +#define ANSWER_3 back + +// Question 4 +// Comment ajoute-t-on un élément dans un vector ? +// values.ANSWER_4(new_value); +#define ANSWER_4 emplace_back + +// Question 5 +// Comment supprime-t-on tous les éléments d'un vector ? +// values.ANSWER_5(); +#define ANSWER_5 clear diff --git a/tp04/ex1/src/qcm.hpp b/tp04/ex1/src/qcm.hpp index 5d019e3e..379ccc12 100644 --- a/tp04/ex1/src/qcm.hpp +++ b/tp04/ex1/src/qcm.hpp @@ -14,41 +14,41 @@ // Pour indiquer qu'une fonction-membre ne modifie pas l'état de l'objet, on écrit const // devant le type de retour de la fonction. -#define ANSWER_1 +#define ANSWER_1 false // Le type char* est le mieux adapté pour représenter des chaînes de caractères. -#define ANSWER_2 +#define ANSWER_2 false // Dans une classe, il ne peut pas avoir plus d'un bloc `public` et un bloc `private`. -#define ANSWER_3 +#define ANSWER_3 false // Le littéral 0u est de type unsigned int. -#define ANSWER_4 +#define ANSWER_4 true // Une variable booléenne se définit avec le type `boolean`. -#define ANSWER_5 +#define ANSWER_5 false // On peut utiliser `inline` pour définir des attributs statiques directement dans la classe. -#define ANSWER_6 +#define ANSWER_6 true // Si une fonction est définie dans deux fichiers-objets différents, l'erreur se produit lors de la phase de // build. -#define ANSWER_7 +#define ANSWER_7 false // Le mot-clef `auto` sert à définir des variables sans préciser leur valeur initiale. -#define ANSWER_8 +#define ANSWER_8 false // Le type `std::vector` sert à créer des tableaux dynamiques. -#define ANSWER_9 +#define ANSWER_9 true // Il faut toujours penser à appeler `delete` sur les pointeurs observants. -#define ANSWER_10 +#define ANSWER_10 false // Si une classe `MyClass` a un attribut `std::string& _name` alors, par défaut, // la destruction d'une instance o de `MyClass` détruit la chaîne de caractère référencée par o._name. -#define ANSWER_11 +#define ANSWER_11 true // Si une classe `MyBoxingClass` a un attribut `std::vector _data` alors, par défaut, // la destruction d'une instance o de `MyBoxingClass` provoque la destruction de toutes les instances de -// `MyBoxedClass` contenues dans o.date. -#define ANSWER_12 \ No newline at end of file +// `MyBoxedClass` contenues dans o._data. +#define ANSWER_12 true diff --git a/tp04/ex2/src/PhoneBook.hpp b/tp04/ex2/src/PhoneBook.hpp new file mode 100644 index 00000000..98cf2e4f --- /dev/null +++ b/tp04/ex2/src/PhoneBook.hpp @@ -0,0 +1,42 @@ +#pragma once +#include "PhoneBookEntry.hpp" + +#include +#include + +class PhoneBook +{ +private: + std::list _phoneBooks; + +public: + bool add_entry(const PhoneBookEntry& phoneBook) + { + if (!phoneBook.get_number().is_valid()) + { + return false; + } + for (const auto& _phoneBook : _phoneBooks) + { + if (_phoneBook == phoneBook) + { + return false; + } + } + _phoneBooks.emplace_back(phoneBook); + return true; + } + + const PhoneNumber* get_number(const std::string& name) const + { + + for (const auto& phoneBook : _phoneBooks) + { + if (phoneBook.get_name() == name) + { + return &phoneBook.get_number(); + } + } + return nullptr; + } +}; \ No newline at end of file diff --git a/tp04/ex2/src/PhoneBookEntry.hpp b/tp04/ex2/src/PhoneBookEntry.hpp new file mode 100644 index 00000000..2fa6a28a --- /dev/null +++ b/tp04/ex2/src/PhoneBookEntry.hpp @@ -0,0 +1,21 @@ +#include "PhoneNumber.hpp" + +#include +#pragma once + +class PhoneBookEntry +{ +private: + std::string _name; + PhoneNumber _phoneNumber; + +public: + PhoneBookEntry(const std::string& name, const PhoneNumber& PhoneNumber) + : _name { name } + , _phoneNumber { PhoneNumber } {}; + + const std::string& get_name() const { return _name; } + const PhoneNumber& get_number() const { return _phoneNumber; } + + bool operator==(const PhoneBookEntry& op) const { return _name == op._name; } +}; \ No newline at end of file diff --git a/tp04/ex2/src/PhoneNumber.hpp b/tp04/ex2/src/PhoneNumber.hpp new file mode 100644 index 00000000..32e34747 --- /dev/null +++ b/tp04/ex2/src/PhoneNumber.hpp @@ -0,0 +1,48 @@ +#include +#include +#include +#pragma once + +class PhoneNumber +{ +private: + std::vector _nbrs; + +public: + PhoneNumber(int un, int deux, int trois, int quatre, int cinq) + : _nbrs { std::vector { un, deux, trois, quatre, cinq } } {}; + + bool is_valid() const + { + for (const auto nb : _nbrs) + { + if (nb < 0 || nb > 99) + { + return false; + } + } + return true; + } + + int operator[](int index) const + { + if (index >= 0 && index < 5) + { + return _nbrs[index]; + } + return -1; + } + + friend std::ostream& operator<<(std::ostream& op, const PhoneNumber& phone) + { + for (auto& i : phone._nbrs) + { + if (i < 10) + { + op << 0; + } + op << i; + } + return op; + } +}; diff --git a/tp04/ex3/src/01-types.hpp b/tp04/ex3/src/01-types.hpp index 1827edc5..55d6b4a7 100644 --- a/tp04/ex3/src/01-types.hpp +++ b/tp04/ex3/src/01-types.hpp @@ -13,36 +13,35 @@ // /////////////////////////////////////////////////////////////////////////////////////// +// Si vous devez ajouter des directives d'inclusion pour faire compiler le test, vous pouvez dupliquer et +// décommenter la ligne ci-dessous. +#include +#include + // Question 1 // stringify_integer convertit un entier en chaîne de caractères -// -// Rappel: ANSWER_1A stringify_integer(ANSWER_1B integer); -#define ANSWER_1A -#define ANSWER_1B +// ANSWER_1A stringify_integer(ANSWER_1B integer); +#define ANSWER_1A std::string +#define ANSWER_1B int // Question 2 -// count_nb_elements permet de retourner le nombre d'éléments présents dans un tableau -// dynamique d'entiers -// -// Rappel: ANSWER_2A count_nb_elements(ANSWER_2B elements); -#define ANSWER_2A -#define ANSWER_2B +// count_nb_elements permet de retourner le nombre d'éléments présents dans un tableau dynamique +// ANSWER_2A count_nb_elements(ANSWER_2B elements); +#define ANSWER_2A unsigned int +#define ANSWER_2B const std::vector& // Question 3 // WrappedString permet de stocker une chaîne et d'encapsuler ses accès en lecture et en écriture // Pour le constructeur, vous devez indiquer le type du paramètre qui permettra d'intialiser _content: -// -// Rappel: WrappedString(ANSWER_3A initial_content) -#define ANSWER_3A +// WrappedString(ANSWER_3A initial_content) +#define ANSWER_3A const std::string& // La fonction get_readonly_content() doit donner un accès en lecture seule à _content -// -// Rappel: ANSWER_3B get_readonly_content() ANSWER_3C { return _content; } -#define ANSWER_3B -#define ANSWER_3C +// ANSWER_3B get_readonly_content() ANSWER_3C { return _content; } +#define ANSWER_3B const std::string& +#define ANSWER_3C const -// La fonction get_readonly_content() doit donner un accès en lecture et écriture à _content -// -// Rappel: ANSWER_3D get_modifiable_content() ANSWER_3E { return _content; } -#define ANSWER_3D +// La fonction get_modifiable_content() doit donner un accès en lecture et écriture à _content +// ANSWER_3D get_modifiable_content() ANSWER_3E { return _content; } +#define ANSWER_3D std::string& #define ANSWER_3E diff --git a/tp04/ex3/src/02-vector.hpp b/tp04/ex3/src/02-vector.hpp index 5ab3d84b..572939dc 100644 --- a/tp04/ex3/src/02-vector.hpp +++ b/tp04/ex3/src/02-vector.hpp @@ -1,25 +1,25 @@ // Question 1 // Quelle est la fonction qui permet d'obtenir le nombre d'éléments dans un vector ? // auto nb_elements = values.ANSWER_1(); -#define ANSWER_1 +#define ANSWER_1 size // Question 2 // Comment écrit-on une boucle permettant d'itérer sur tous les éléments d'un vector ? // ANSWER_2A (auto value ANSWER_2B values) -#define ANSWER_2A -#define ANSWER_2B +#define ANSWER_2A for +#define ANSWER_2B : // Question 3 // Comment accède-t-on au dernier élément d'un vector ? // auto last_value = values.ANSWER_3(); -#define ANSWER_3 +#define ANSWER_3 back // Question 4 // Comment ajoute-t-on un élément dans un vector ? // values.ANSWER_4(new_value); -#define ANSWER_4 +#define ANSWER_4 emplace_back // Question 5 // Comment supprime-t-on tous les éléments d'un vector ? // values.ANSWER_5(); -#define ANSWER_5 +#define ANSWER_5 clear