Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <optional>
- #include <cassert>
- #include <vector>
- #include <algorithm>
- #include <cctype>
- #include <unordered_map>
- struct Number {
- std::string number_;
- Number() = default;
- explicit Number(const std::string& number) : number_(number) {}
- std::string GetNormalizedString() const {
- return number_[0] == '+' ? number_.substr(1, number_.length() - 1) : number_;
- }
- };
- struct Address {
- std::optional<std::string> address_;
- Address() = default;
- explicit Address(const std::string& address) {
- address_.emplace(address);
- }
- std::string GetNormalizedString() const {
- return address_.has_value() ? address_.value() : "";
- }
- };
- struct Name {
- std::string name_;
- Name() = default;
- explicit Name(const std::string& name) : name_(name){}
- std::string GetNormalizedString() const {
- std::vector<std::string> vector;
- int j = -1;
- for (size_t i = 0; i < name_.length(); ++i) {
- if (!isalpha(name_[i])) {
- if (i != j + 1) {
- vector.push_back(name_.substr(j + 1, i - j - 1));
- }
- j = i;
- }
- }
- if (j != name_.length() - 1) {
- vector.push_back(name_.substr(j + 1, name_.length() - j));
- }
- for (auto& elem : vector) {
- for (auto& ch : elem) {
- ch = tolower(ch);
- }
- }
- std::sort(vector.begin(), vector.end());
- std::string res;
- for (const auto& elem : vector) {
- res += elem;
- res += " ";
- }
- return res.back() == ' ' ? res.substr(0, res.length() - 1) : res;
- }
- };
- class Contact {
- private:
- Name name_;
- Number number_;
- Address address_;
- public:
- Contact() : name_(Name()), number_(Number()), address_(Address()) {}
- Contact(const std::string& name, const std::string& number, const std::string& address)
- : name_(name), number_(number), address_(address){}
- std::string GetName() const {
- return name_.name_;
- }
- std::string GetNumber() const {
- return number_.number_;
- }
- std::string GetAddress() const {
- return address_.address_.has_value() ? address_.address_.value() : "";
- }
- void SetName(const std::string name) {
- name_.name_ = name;
- }
- void SetNumber(const std::string number) {
- number_.number_ = number;
- }
- void SetAddress(const std::string address) {
- address_.address_.emplace(address);
- }
- std::string GetNormalizedName() const {
- return name_.GetNormalizedString();
- }
- std::string GetNormalizedNumber() const {
- return number_.GetNormalizedString();
- }
- std::string GetNormalizedAddress() const {
- return address_.GetNormalizedString();
- }
- };
- class PhoneBook {
- private:
- std::unordered_map<int, Contact*> map_id_;
- int max_id_ = 0;
- public:
- PhoneBook() : max_id_(0) {}
- int AddContact(const std::string& name, const std::string& number, const std::string& address) {
- auto n = Name(name).GetNormalizedString();
- for (const auto& elem : map_id_) {
- if (elem.second->GetNormalizedName() == n) {
- return -1;
- }
- }
- auto m = Number(number).GetNormalizedString();
- for (const auto& elem : map_id_) {
- if (elem.second->GetNormalizedNumber() == m) {
- return -1;
- }
- }
- auto* contact = new Contact(name, number, address);
- ++max_id_;
- map_id_[max_id_ - 1] = contact;
- return max_id_ - 1;
- }
- ~PhoneBook() {
- for (const auto& elem : map_id_) {
- delete elem.second;
- }
- }
- PhoneBook(const PhoneBook& rhs) : max_id_(rhs.max_id_) {
- for (const auto& elem : map_id_) {
- delete map_id_[elem.first];
- }
- map_id_.clear();
- for (const auto& elem : rhs.map_id_) {
- auto* contact = new Contact(elem.second->GetName(), elem.second->GetNumber(),
- elem.second->GetAddress());
- map_id_[elem.first] = contact;
- }
- }
- PhoneBook& operator=(const PhoneBook& rhs) {
- if (this != &rhs) {
- max_id_ = rhs.max_id_;
- for (const auto& elem : map_id_) {
- delete map_id_[elem.first];
- }
- map_id_.clear();
- for (const auto& elem : rhs.map_id_) {
- auto* contact = new Contact(elem.second->GetName(), elem.second->GetNumber(),
- elem.second->GetAddress());
- map_id_[elem.first] = contact;
- }
- }
- return *this;
- }
- PhoneBook(PhoneBook&& rhs) = default;
- PhoneBook& operator=(PhoneBook&& rhs) = default;
- Contact* GetContactById(int id) const {
- if (map_id_.count(id) == 0) {
- return nullptr;
- }
- return map_id_.at(id);
- }
- Contact* GetContactByName(const std::string& name) const {
- auto n = Name(name).GetNormalizedString();
- for (const auto& elem : map_id_) {
- if (elem.second->GetNormalizedName() == n) {
- return elem.second;
- }
- }
- return nullptr;
- }
- Contact* GetContactByNumber(const std::string& number) const {
- auto m = Number(number).GetNormalizedString();
- for (const auto& elem : map_id_) {
- if (elem.second->GetNormalizedNumber() == m) {
- return elem.second;
- }
- }
- return nullptr;
- }
- std::vector<Contact*> GetContactsByAddress(const std::string& address) const {
- std::vector<Contact*> vector;
- for (const auto& elem : map_id_) {
- if (elem.second->GetNormalizedAddress().find(address) != std::string::npos) {
- vector.push_back(elem.second);
- }
- }
- return vector;
- }
- Contact GetContact(int id, bool& flag) const {
- if (map_id_.count(id) == 0) {
- flag = false;
- return Contact();
- }
- flag = true;
- return *(map_id_.at(id));
- }
- void SetContact(int id, const Contact& contact) {
- if (!map_id_.count(id)) {
- return;
- }
- auto n = Name(contact.GetName()).GetNormalizedString();
- for (const auto& elem : map_id_) {
- if (elem.second->GetNormalizedName() == n && elem.first != id) {
- return;
- }
- }
- auto m = Number(contact.GetNumber()).GetNormalizedString();
- for (const auto& elem : map_id_) {
- if (elem.second->GetNormalizedNumber() == m && elem.first != id) {
- return;
- }
- }
- map_id_[id]->SetName(contact.GetName());
- map_id_[id]->SetNumber(contact.GetNumber());
- map_id_[id]->SetAddress(contact.GetAddress());
- }
- void DeleteContact(int id) {
- if (map_id_.count(id) == 0) {
- return;
- }
- delete map_id_[id];
- map_id_.erase(id);
- }
- };
- int main() {
- {
- Number n;
- n.number_ = "+123";
- assert(n.number_ == "+123");
- assert(n.number_[0] == '+');
- assert(n.number_[1] == '1');
- assert(n.GetNormalizedString() == "123");
- Number m("123");
- assert(m.number_ == "123");
- assert(m.number_[0] == '1');
- assert(m.number_[1] == '2');
- assert(m.GetNormalizedString() == "123");
- }
- {
- Address a;
- assert(!a.address_.has_value());
- assert(a.GetNormalizedString().empty());
- a.address_ = "123";
- assert(a.address_ == "123");
- assert(a.address_.value()[0] == '1');
- assert(a.address_.value()[1] == '2');
- assert(a.GetNormalizedString() == "123");
- Address b("123");
- assert(a.address_.has_value());
- assert(a.address_ == "123");
- assert(a.address_.value()[0] == '1');
- assert(a.address_.value()[1] == '2');
- assert(a.GetNormalizedString() == "123");
- }
- {
- Name n;
- assert(n.name_.empty());
- n.name_ = "a23";
- assert(n.name_[0] == 'a');
- assert(n.name_[1] == '2');
- Name m("abc");
- assert(m.name_ == "abc");
- assert(m.name_[0] == 'a');
- assert(m.name_[1] == 'b');
- Name a(" A- bc^^32423423423%%d ");
- assert(a.GetNormalizedString() == "a bc d");
- Name b("aBc");
- assert(b.GetNormalizedString() == "abc");
- Name c;
- assert(c.GetNormalizedString().empty());
- Name d("a b c d");
- assert(d.GetNormalizedString() == d.name_);
- Name e("abc ");
- assert(e.GetNormalizedString() == "abc");
- Name f("A_ bC ");
- assert(f.GetNormalizedString() == "a bc");
- Name g("bc _A_ ");
- assert(g.GetNormalizedString() == "a bc");
- }
- {
- Contact c;
- assert(c.GetName().empty());
- assert(c.GetNumber().empty());
- assert(c.GetAddress().empty());
- assert(c.GetNormalizedName().empty());
- assert(c.GetNormalizedNumber().empty());
- assert(c.GetNormalizedAddress().empty());
- c.SetName("B A cdE");
- c.SetNumber("+123 23");
- c.SetAddress("12");
- assert(c.GetName() == "B A cdE");
- assert(c.GetNumber() == "+123 23");
- assert(c.GetAddress() == "12");
- assert(c.GetNormalizedName() == "a b cde");
- assert(c.GetNormalizedNumber() == "123 23");
- assert(c.GetNormalizedAddress() == "12");
- Contact d( " B a ", "123", "aA");
- assert(d.GetName() == " B a ");
- assert(d.GetNumber() == "123");
- assert(d.GetAddress() == "aA");
- assert(d.GetNormalizedName() == "a b");
- assert(d.GetNormalizedNumber() == "123");
- assert(d.GetNormalizedAddress() == "aA");
- Contact e("a_b", "+1", "a");
- assert(e.GetName() == "a_b");
- assert(e.GetNumber() == "+1");
- assert(e.GetAddress() == "a");
- assert(e.GetNormalizedName() == "a b");
- assert(e.GetNormalizedNumber() == "1");
- assert(e.GetNormalizedAddress() == "a");
- }
- {
- PhoneBook p;
- assert(p.AddContact(" b A ", "+1", "1") == 0);
- assert(p.GetContactById(0)->GetName() == " b A ");
- assert(p.GetContactById(0)->GetNumber() == "+1");
- assert(p.GetContactById(0)->GetAddress() == "1");
- p.GetContactById(0)->SetName("art");
- assert(p.GetContactById(0)->GetName() == "art");
- p.GetContactById(0)->SetName(" b A ");
- assert(p.GetContactByName(" B A ") == p.GetContactByName("a b") && p.GetContactByName("a b") != nullptr);
- assert(p.GetContactByName("B A") == p.GetContactByName(" b A ") && p.GetContactByName(" b A ") != nullptr);
- assert(p.GetContactByNumber("1") == p.GetContactByNumber("+1") && p.GetContactByNumber("+1") != nullptr);
- assert(p.GetContactById(1) == nullptr);
- assert(p.GetContactByName(" a ") == nullptr);
- assert(p.GetContactByNumber("+2") == nullptr);
- assert(p.AddContact("a b ", "+2", "2") == -1);
- assert(p.GetContactById(1) == nullptr);
- assert(p.AddContact(" b a ", "+1", "2") == -1);
- assert(p.GetContactById(1) == nullptr);
- assert(p.AddContact("c", "+1", "2") == -1);
- assert(p.GetContactById(1) == nullptr);
- assert(p.AddContact(" c ", "+2 ", "2") == 1);
- assert(p.GetContactById(0)->GetName() == " b A ");
- assert(p.GetContactById(0)->GetNumber() == "+1");
- assert(p.GetContactById(0)->GetAddress() == "1");
- assert(p.GetContactById(1)->GetName() == " c ");
- assert(p.GetContactById(1)->GetNumber() == "+2 ");
- assert(p.GetContactById(1)->GetAddress() == "2");
- assert(p.GetContactById(2) == nullptr);
- p.DeleteContact(0);
- assert(p.GetContactById(0) == nullptr);
- assert(p.GetContactByName("a b") == nullptr);
- assert(p.GetContactByName("+1") == nullptr);
- assert(p.GetContactById(2) == nullptr);
- assert(p.GetContactById(1)->GetName() == " c ");
- assert(p.GetContactById(1)->GetNumber() == "+2 ");
- assert(p.GetContactById(1)->GetAddress() == "2");
- p.DeleteContact(2);
- assert(p.GetContactById(0) == nullptr);
- assert(p.GetContactById(2) == nullptr);
- }
- {
- PhoneBook p;
- assert(p.AddContact("a", "1", "1") == 0);
- assert(p.AddContact("b", "2", "2") == 1);
- assert(p.AddContact("c", "3", "3") == 2);
- bool flag;
- Contact c = p.GetContact(1, flag);
- assert(c.GetName() == "b");
- assert(c.GetNumber() == "2");
- assert(c.GetAddress() == "2");
- assert(flag);
- c.SetName("name");
- assert(p.GetContactById(1)->GetName() == "b");
- assert(p.GetContactByName("name") == nullptr);
- p.DeleteContact(1);
- assert(c.GetName() == "name");
- assert(c.GetNumber() == "2");
- assert(c.GetAddress() == "2");
- Contact d = p.GetContact(10, flag);
- assert(!flag);
- assert(d.GetName().empty());
- assert(d.GetNumber().empty());
- assert(d.GetAddress().empty());
- }
- {
- PhoneBook p;
- assert(p.AddContact("a", "1", "1") == 0);
- assert(p.AddContact("b", "2", "2") == 1);
- assert(p.AddContact("c", "3", "3") == 2);
- assert(p.GetContactById(0)->GetName() == "a");
- PhoneBook b(p);
- assert(b.GetContactById(0)->GetName() == "a");
- assert(b.GetContactById(0)->GetNumber() == "1");
- assert(b.GetContactByName("c")->GetAddress() == "3");
- assert(b.GetContactByNumber("2")->GetName() == "b");
- b.DeleteContact(0);
- assert(p.GetContactById(0) != nullptr);
- p.DeleteContact(2);
- assert(b.GetContactById(2) != nullptr);
- PhoneBook c = b;
- assert(c.GetContactById(0) == nullptr);
- assert(c.GetContactById(1)->GetNumber() == "2");
- assert(c.GetContactByName("c")->GetAddress() == "3");
- assert(c.GetContactByNumber("2")->GetName() == "b");
- c.DeleteContact(1);
- assert(c.GetContactById(1) == nullptr);
- assert(p.GetContactById(1) != nullptr);
- assert(b.GetContactById(1) != nullptr);
- }
- {
- PhoneBook p;
- assert(p.AddContact("a", "1", "1") == 0);
- assert(p.AddContact("b", "2", "2") == 1);
- assert(p.AddContact("c", "3", "3") == 2);
- p.SetContact(1, Contact("_name_", "+number", "address"));
- assert(p.GetContactById(1)->GetName() == "_name_");
- assert(p.GetContactById(1)->GetNumber() == "+number");
- assert(p.GetContactById(1)->GetAddress() == "address");
- assert(p.GetContactByName("name") != nullptr);
- assert(p.GetContactByNumber("number") != nullptr);
- assert(p.GetContactByName("b") == nullptr);
- assert(p.GetContactByNumber("2") == nullptr);
- p.SetContact(3, Contact("_name_", "+number", "address"));
- assert(p.GetContactById(3) == nullptr);
- p.SetContact(2, Contact("a", "123", "123"));
- assert(p.GetContactById(0)->GetName() == "a");
- assert(p.GetContactById(0)->GetNumber() == "1");
- assert(p.GetContactById(0)->GetAddress() == "1");
- assert(p.GetContactById(2)->GetName() == "c");
- p.SetContact(2, Contact("aa", "1", "123"));
- assert(p.GetContactById(0)->GetName() == "a");
- assert(p.GetContactById(0)->GetNumber() == "1");
- assert(p.GetContactById(0)->GetAddress() == "1");
- assert(p.GetContactById(2)->GetName() == "c");
- p.SetContact(2, Contact("a", "123", "123"));
- assert(p.GetContactById(0)->GetName() == "a");
- assert(p.GetContactById(0)->GetNumber() == "1");
- assert(p.GetContactById(0)->GetAddress() == "1");
- assert(p.GetContactById(2)->GetName() == "c");
- }
- {
- PhoneBook p;
- assert(p.AddContact("a", "1", "12345a") == 0);
- assert(p.AddContact("b", "2", "2345b55") == 1);
- assert(p.AddContact("c", "3", "345c") == 2);
- auto v = p.GetContactsByAddress("345");
- assert(v.size() == 3);
- assert(v[0] == p.GetContactById(0) || v[0] == p.GetContactById(1) || v[0] == p.GetContactById(2));
- assert(v[1] == p.GetContactById(0) || v[1] == p.GetContactById(1) || v[1] == p.GetContactById(2));
- assert(v[2] == p.GetContactById(0) || v[2] == p.GetContactById(1) || v[2] == p.GetContactById(2));
- auto w = p.GetContactsByAddress("12");
- assert(w.size() == 1);
- assert(w[0] == p.GetContactById(0));
- auto u = p.GetContactsByAddress("5b5");
- assert(u.size() == 1);
- assert(u[0] == p.GetContactById(1));
- auto t = p.GetContactsByAddress("35");
- assert(t.empty());
- p.SetContact(1, Contact("x y", "x", "x"));
- assert(u[0]->GetName() == "x y");
- w[0]->SetName("aaa");
- w[0]->SetAddress("123");
- assert(p.GetContactById(0)->GetName() == "aaa");
- assert(p.GetContactById(0)->GetAddress() == "123");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement