Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <optional>
  4. #include <cassert>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <cctype>
  8. #include <unordered_map>
  9.  
  10. struct Number {
  11.     std::string number_;
  12.  
  13.     Number() = default;
  14.  
  15.     explicit Number(const std::string& number) : number_(number) {}
  16.  
  17.     std::string GetNormalizedString() const {
  18.         return number_[0] == '+' ? number_.substr(1, number_.length() - 1) : number_;
  19.     }
  20. };
  21.  
  22. struct Address {
  23.     std::optional<std::string> address_;
  24.  
  25.     Address() = default;
  26.  
  27.     explicit Address(const std::string& address) {
  28.         address_.emplace(address);
  29.     }
  30.  
  31.     std::string GetNormalizedString() const {
  32.         return address_.has_value() ? address_.value() : "";
  33.     }
  34. };
  35.  
  36. struct Name {
  37.     std::string name_;
  38.  
  39.     Name() = default;
  40.  
  41.     explicit Name(const std::string& name) : name_(name){}
  42.  
  43.     std::string GetNormalizedString() const {
  44.         std::vector<std::string> vector;
  45.         int j = -1;
  46.         for (size_t i = 0; i < name_.length(); ++i) {
  47.             if (!isalpha(name_[i])) {
  48.                 if (i != j + 1) {
  49.                     vector.push_back(name_.substr(j + 1, i - j - 1));
  50.                 }
  51.                 j = i;
  52.             }
  53.         }
  54.         if (j != name_.length() - 1) {
  55.             vector.push_back(name_.substr(j + 1, name_.length() - j));
  56.         }
  57.         for (auto& elem : vector) {
  58.             for (auto& ch : elem) {
  59.                 ch = tolower(ch);
  60.             }
  61.         }
  62.         std::sort(vector.begin(), vector.end());
  63.         std::string res;
  64.         for (const auto& elem : vector) {
  65.             res += elem;
  66.             res += " ";
  67.         }
  68.         return res.back() == ' ' ? res.substr(0, res.length() - 1) : res;
  69.     }
  70. };
  71.  
  72. class Contact {
  73. private:
  74.     Name name_;
  75.     Number number_;
  76.     Address address_;
  77.  
  78. public:
  79.      Contact() : name_(Name()), number_(Number()), address_(Address()) {}
  80.  
  81.     Contact(const std::string& name, const std::string& number, const std::string& address)
  82.             : name_(name), number_(number), address_(address){}
  83.  
  84.      std::string GetName() const {
  85.          return name_.name_;
  86.      }
  87.  
  88.      std::string GetNumber() const {
  89.          return number_.number_;
  90.      }
  91.  
  92.      std::string GetAddress() const {
  93.          return address_.address_.has_value() ? address_.address_.value() : "";
  94.      }
  95.  
  96.      void SetName(const std::string name) {
  97.          name_.name_ = name;
  98.      }
  99.  
  100.      void SetNumber(const std::string number) {
  101.          number_.number_ = number;
  102.      }
  103.  
  104.      void SetAddress(const std::string address) {
  105.          address_.address_.emplace(address);
  106.      }
  107.  
  108.      std::string GetNormalizedName() const {
  109.          return name_.GetNormalizedString();
  110.      }
  111.  
  112.     std::string GetNormalizedNumber() const {
  113.         return number_.GetNormalizedString();
  114.     }
  115.  
  116.     std::string GetNormalizedAddress() const {
  117.         return address_.GetNormalizedString();
  118.     }
  119.  
  120.  
  121. };
  122.  
  123. class PhoneBook {
  124. private:
  125.     std::unordered_map<int, Contact*> map_id_;
  126.     int max_id_ = 0;
  127.  
  128. public:
  129.     PhoneBook() : max_id_(0) {}
  130.  
  131.     int AddContact(const std::string& name, const std::string& number, const std::string& address) {
  132.         auto n = Name(name).GetNormalizedString();
  133.         for (const auto& elem : map_id_) {
  134.             if (elem.second->GetNormalizedName() == n) {
  135.                 return -1;
  136.             }
  137.         }
  138.         auto m = Number(number).GetNormalizedString();
  139.         for (const auto& elem : map_id_) {
  140.             if (elem.second->GetNormalizedNumber() == m) {
  141.                 return -1;
  142.             }
  143.         }
  144.         auto* contact = new Contact(name, number, address);
  145.         ++max_id_;
  146.         map_id_[max_id_ - 1] = contact;
  147.         return max_id_ - 1;
  148.     }
  149.  
  150.     ~PhoneBook() {
  151.         for (const auto& elem : map_id_) {
  152.             delete elem.second;
  153.         }
  154.     }
  155.  
  156.     PhoneBook(const PhoneBook& rhs) : max_id_(rhs.max_id_) {
  157.         for (const auto& elem : map_id_) {
  158.             delete map_id_[elem.first];
  159.         }
  160.         map_id_.clear();
  161.         for (const auto& elem : rhs.map_id_) {
  162.             auto* contact = new Contact(elem.second->GetName(), elem.second->GetNumber(),
  163.                     elem.second->GetAddress());
  164.             map_id_[elem.first] = contact;
  165.         }
  166.     }
  167.  
  168.     PhoneBook& operator=(const PhoneBook& rhs) {
  169.         if (this != &rhs) {
  170.             max_id_ = rhs.max_id_;
  171.             for (const auto& elem : map_id_) {
  172.                 delete map_id_[elem.first];
  173.             }
  174.             map_id_.clear();
  175.             for (const auto& elem : rhs.map_id_) {
  176.                 auto* contact = new Contact(elem.second->GetName(), elem.second->GetNumber(),
  177.                                             elem.second->GetAddress());
  178.                 map_id_[elem.first] = contact;
  179.             }
  180.         }
  181.         return *this;
  182.     }
  183.  
  184.     PhoneBook(PhoneBook&& rhs) = default;
  185.  
  186.     PhoneBook& operator=(PhoneBook&& rhs) = default;
  187.  
  188.     Contact* GetContactById(int id) const {
  189.         if (map_id_.count(id) == 0) {
  190.             return nullptr;
  191.         }
  192.         return map_id_.at(id);
  193.     }
  194.  
  195.     Contact* GetContactByName(const std::string& name) const {
  196.         auto n = Name(name).GetNormalizedString();
  197.         for (const auto& elem : map_id_) {
  198.             if (elem.second->GetNormalizedName() == n) {
  199.                 return elem.second;
  200.             }
  201.         }
  202.         return nullptr;
  203.     }
  204.  
  205.     Contact* GetContactByNumber(const std::string& number) const {
  206.         auto m = Number(number).GetNormalizedString();
  207.         for (const auto& elem : map_id_) {
  208.             if (elem.second->GetNormalizedNumber() == m) {
  209.                 return elem.second;
  210.             }
  211.         }
  212.         return nullptr;
  213.     }
  214.  
  215.     std::vector<Contact*> GetContactsByAddress(const std::string& address) const {
  216.         std::vector<Contact*> vector;
  217.         for (const auto& elem : map_id_) {
  218.             if (elem.second->GetNormalizedAddress().find(address) != std::string::npos) {
  219.                 vector.push_back(elem.second);
  220.             }
  221.         }
  222.         return vector;
  223.     }
  224.  
  225.     Contact GetContact(int id, bool& flag) const {
  226.         if (map_id_.count(id) == 0) {
  227.             flag = false;
  228.             return Contact();
  229.         }
  230.         flag = true;
  231.         return *(map_id_.at(id));
  232.     }
  233.  
  234.     void SetContact(int id, const Contact& contact) {
  235.         if (!map_id_.count(id)) {
  236.             return;
  237.         }
  238.         auto n = Name(contact.GetName()).GetNormalizedString();
  239.         for (const auto& elem : map_id_) {
  240.             if (elem.second->GetNormalizedName() == n && elem.first != id) {
  241.                 return;
  242.             }
  243.         }
  244.         auto m = Number(contact.GetNumber()).GetNormalizedString();
  245.         for (const auto& elem : map_id_) {
  246.             if (elem.second->GetNormalizedNumber() == m && elem.first != id) {
  247.                 return;
  248.             }
  249.         }
  250.         map_id_[id]->SetName(contact.GetName());
  251.         map_id_[id]->SetNumber(contact.GetNumber());
  252.         map_id_[id]->SetAddress(contact.GetAddress());
  253.     }
  254.  
  255.     void DeleteContact(int id) {
  256.         if (map_id_.count(id) == 0) {
  257.             return;
  258.         }
  259.         delete map_id_[id];
  260.         map_id_.erase(id);
  261.     }
  262. };
  263.  
  264. int main() {
  265.     {
  266.         Number n;
  267.         n.number_ = "+123";
  268.         assert(n.number_ == "+123");
  269.         assert(n.number_[0] == '+');
  270.         assert(n.number_[1] == '1');
  271.         assert(n.GetNormalizedString() == "123");
  272.         Number m("123");
  273.         assert(m.number_ == "123");
  274.         assert(m.number_[0] == '1');
  275.         assert(m.number_[1] == '2');
  276.         assert(m.GetNormalizedString() == "123");
  277.     }
  278.  
  279.     {
  280.         Address a;
  281.         assert(!a.address_.has_value());
  282.         assert(a.GetNormalizedString().empty());
  283.         a.address_ = "123";
  284.         assert(a.address_ == "123");
  285.         assert(a.address_.value()[0] == '1');
  286.         assert(a.address_.value()[1] == '2');
  287.         assert(a.GetNormalizedString() == "123");
  288.         Address b("123");
  289.         assert(a.address_.has_value());
  290.         assert(a.address_ == "123");
  291.         assert(a.address_.value()[0] == '1');
  292.         assert(a.address_.value()[1] == '2');
  293.         assert(a.GetNormalizedString() == "123");
  294.     }
  295.  
  296.     {
  297.         Name n;
  298.         assert(n.name_.empty());
  299.         n.name_ = "a23";
  300.         assert(n.name_[0] == 'a');
  301.         assert(n.name_[1] == '2');
  302.         Name m("abc");
  303.         assert(m.name_ == "abc");
  304.         assert(m.name_[0] == 'a');
  305.         assert(m.name_[1] == 'b');
  306.         Name a("  A- bc^^32423423423%%d  ");
  307.         assert(a.GetNormalizedString() == "a bc d");
  308.         Name b("aBc");
  309.         assert(b.GetNormalizedString() == "abc");
  310.         Name c;
  311.         assert(c.GetNormalizedString().empty());
  312.         Name d("a b c d");
  313.         assert(d.GetNormalizedString() == d.name_);
  314.         Name e("abc ");
  315.         assert(e.GetNormalizedString() == "abc");
  316.         Name f("A_  bC ");
  317.         assert(f.GetNormalizedString() == "a bc");
  318.         Name g("bc _A_ ");
  319.         assert(g.GetNormalizedString() == "a bc");
  320.     }
  321.  
  322.     {
  323.         Contact c;
  324.         assert(c.GetName().empty());
  325.         assert(c.GetNumber().empty());
  326.         assert(c.GetAddress().empty());
  327.         assert(c.GetNormalizedName().empty());
  328.         assert(c.GetNormalizedNumber().empty());
  329.         assert(c.GetNormalizedAddress().empty());
  330.         c.SetName("B A cdE");
  331.         c.SetNumber("+123 23");
  332.         c.SetAddress("12");
  333.         assert(c.GetName() == "B A cdE");
  334.         assert(c.GetNumber() == "+123 23");
  335.         assert(c.GetAddress() == "12");
  336.         assert(c.GetNormalizedName() == "a b cde");
  337.         assert(c.GetNormalizedNumber() == "123 23");
  338.         assert(c.GetNormalizedAddress() == "12");
  339.         Contact d( "  B   a  ", "123", "aA");
  340.         assert(d.GetName() == "  B   a  ");
  341.         assert(d.GetNumber() == "123");
  342.         assert(d.GetAddress() == "aA");
  343.         assert(d.GetNormalizedName() == "a b");
  344.         assert(d.GetNormalizedNumber() == "123");
  345.         assert(d.GetNormalizedAddress() == "aA");
  346.         Contact e("a_b", "+1", "a");
  347.         assert(e.GetName() == "a_b");
  348.         assert(e.GetNumber() == "+1");
  349.         assert(e.GetAddress() == "a");
  350.         assert(e.GetNormalizedName() == "a b");
  351.         assert(e.GetNormalizedNumber() == "1");
  352.         assert(e.GetNormalizedAddress() == "a");
  353.     }
  354.  
  355.     {
  356.         PhoneBook p;
  357.         assert(p.AddContact(" b A ", "+1", "1") == 0);
  358.         assert(p.GetContactById(0)->GetName() == " b A ");
  359.         assert(p.GetContactById(0)->GetNumber() == "+1");
  360.         assert(p.GetContactById(0)->GetAddress() == "1");
  361.         p.GetContactById(0)->SetName("art");
  362.         assert(p.GetContactById(0)->GetName() == "art");
  363.         p.GetContactById(0)->SetName(" b A ");
  364.         assert(p.GetContactByName(" B  A ") == p.GetContactByName("a b") && p.GetContactByName("a b") != nullptr);
  365.         assert(p.GetContactByName("B   A") == p.GetContactByName(" b A ") && p.GetContactByName(" b A ") != nullptr);
  366.         assert(p.GetContactByNumber("1") == p.GetContactByNumber("+1") && p.GetContactByNumber("+1") != nullptr);
  367.         assert(p.GetContactById(1) == nullptr);
  368.         assert(p.GetContactByName(" a ") == nullptr);
  369.         assert(p.GetContactByNumber("+2") == nullptr);
  370.         assert(p.AddContact("a  b  ", "+2", "2") == -1);
  371.         assert(p.GetContactById(1) == nullptr);
  372.         assert(p.AddContact(" b  a  ", "+1", "2") == -1);
  373.         assert(p.GetContactById(1) == nullptr);
  374.         assert(p.AddContact("c", "+1", "2") == -1);
  375.         assert(p.GetContactById(1) == nullptr);
  376.         assert(p.AddContact("  c  ", "+2 ", "2") == 1);
  377.         assert(p.GetContactById(0)->GetName() == " b A ");
  378.         assert(p.GetContactById(0)->GetNumber() == "+1");
  379.         assert(p.GetContactById(0)->GetAddress() == "1");
  380.         assert(p.GetContactById(1)->GetName() == "  c  ");
  381.         assert(p.GetContactById(1)->GetNumber() == "+2 ");
  382.         assert(p.GetContactById(1)->GetAddress() == "2");
  383.         assert(p.GetContactById(2) == nullptr);
  384.         p.DeleteContact(0);
  385.         assert(p.GetContactById(0) == nullptr);
  386.         assert(p.GetContactByName("a b") == nullptr);
  387.         assert(p.GetContactByName("+1") == nullptr);
  388.         assert(p.GetContactById(2) == nullptr);
  389.         assert(p.GetContactById(1)->GetName() == "  c  ");
  390.         assert(p.GetContactById(1)->GetNumber() == "+2 ");
  391.         assert(p.GetContactById(1)->GetAddress() == "2");
  392.         p.DeleteContact(2);
  393.         assert(p.GetContactById(0) == nullptr);
  394.         assert(p.GetContactById(2) == nullptr);
  395.     }
  396.  
  397.     {
  398.         PhoneBook p;
  399.         assert(p.AddContact("a", "1", "1") == 0);
  400.         assert(p.AddContact("b", "2", "2") == 1);
  401.         assert(p.AddContact("c", "3", "3") == 2);
  402.         bool flag;
  403.         Contact c = p.GetContact(1, flag);
  404.         assert(c.GetName() == "b");
  405.         assert(c.GetNumber() == "2");
  406.         assert(c.GetAddress() == "2");
  407.         assert(flag);
  408.         c.SetName("name");
  409.         assert(p.GetContactById(1)->GetName() == "b");
  410.         assert(p.GetContactByName("name") == nullptr);
  411.         p.DeleteContact(1);
  412.         assert(c.GetName() == "name");
  413.         assert(c.GetNumber() == "2");
  414.         assert(c.GetAddress() == "2");
  415.         Contact d = p.GetContact(10, flag);
  416.         assert(!flag);
  417.         assert(d.GetName().empty());
  418.         assert(d.GetNumber().empty());
  419.         assert(d.GetAddress().empty());
  420.     }
  421.  
  422.     {
  423.         PhoneBook p;
  424.         assert(p.AddContact("a", "1", "1") == 0);
  425.         assert(p.AddContact("b", "2", "2") == 1);
  426.         assert(p.AddContact("c", "3", "3") == 2);
  427.         assert(p.GetContactById(0)->GetName() == "a");
  428.         PhoneBook b(p);
  429.         assert(b.GetContactById(0)->GetName() == "a");
  430.         assert(b.GetContactById(0)->GetNumber() == "1");
  431.         assert(b.GetContactByName("c")->GetAddress() == "3");
  432.         assert(b.GetContactByNumber("2")->GetName() == "b");
  433.         b.DeleteContact(0);
  434.         assert(p.GetContactById(0) != nullptr);
  435.         p.DeleteContact(2);
  436.         assert(b.GetContactById(2) != nullptr);
  437.         PhoneBook c = b;
  438.         assert(c.GetContactById(0) == nullptr);
  439.         assert(c.GetContactById(1)->GetNumber() == "2");
  440.         assert(c.GetContactByName("c")->GetAddress() == "3");
  441.         assert(c.GetContactByNumber("2")->GetName() == "b");
  442.         c.DeleteContact(1);
  443.         assert(c.GetContactById(1) == nullptr);
  444.         assert(p.GetContactById(1) != nullptr);
  445.         assert(b.GetContactById(1) != nullptr);
  446.     }
  447.  
  448.     {
  449.         PhoneBook p;
  450.         assert(p.AddContact("a", "1", "1") == 0);
  451.         assert(p.AddContact("b", "2", "2") == 1);
  452.         assert(p.AddContact("c", "3", "3") == 2);
  453.         p.SetContact(1, Contact("_name_", "+number", "address"));
  454.         assert(p.GetContactById(1)->GetName() == "_name_");
  455.         assert(p.GetContactById(1)->GetNumber() == "+number");
  456.         assert(p.GetContactById(1)->GetAddress() == "address");
  457.         assert(p.GetContactByName("name") != nullptr);
  458.         assert(p.GetContactByNumber("number") != nullptr);
  459.         assert(p.GetContactByName("b") == nullptr);
  460.         assert(p.GetContactByNumber("2") == nullptr);
  461.         p.SetContact(3, Contact("_name_", "+number", "address"));
  462.         assert(p.GetContactById(3) == nullptr);
  463.         p.SetContact(2, Contact("a", "123", "123"));
  464.         assert(p.GetContactById(0)->GetName() == "a");
  465.         assert(p.GetContactById(0)->GetNumber() == "1");
  466.         assert(p.GetContactById(0)->GetAddress() == "1");
  467.         assert(p.GetContactById(2)->GetName() == "c");
  468.         p.SetContact(2, Contact("aa", "1", "123"));
  469.         assert(p.GetContactById(0)->GetName() == "a");
  470.         assert(p.GetContactById(0)->GetNumber() == "1");
  471.         assert(p.GetContactById(0)->GetAddress() == "1");
  472.         assert(p.GetContactById(2)->GetName() == "c");
  473.         p.SetContact(2, Contact("a", "123", "123"));
  474.         assert(p.GetContactById(0)->GetName() == "a");
  475.         assert(p.GetContactById(0)->GetNumber() == "1");
  476.         assert(p.GetContactById(0)->GetAddress() == "1");
  477.         assert(p.GetContactById(2)->GetName() == "c");
  478.     }
  479.  
  480.     {
  481.         PhoneBook p;
  482.         assert(p.AddContact("a", "1", "12345a") == 0);
  483.         assert(p.AddContact("b", "2", "2345b55") == 1);
  484.         assert(p.AddContact("c", "3", "345c") == 2);
  485.         auto v = p.GetContactsByAddress("345");
  486.  
  487.         assert(v.size() == 3);
  488.         assert(v[0] == p.GetContactById(0) || v[0] == p.GetContactById(1) || v[0] == p.GetContactById(2));
  489.         assert(v[1] == p.GetContactById(0) || v[1] == p.GetContactById(1) || v[1] == p.GetContactById(2));
  490.         assert(v[2] == p.GetContactById(0) || v[2] == p.GetContactById(1) || v[2] == p.GetContactById(2));
  491.  
  492.         auto w = p.GetContactsByAddress("12");
  493.         assert(w.size() == 1);
  494.         assert(w[0] == p.GetContactById(0));
  495.  
  496.         auto u = p.GetContactsByAddress("5b5");
  497.         assert(u.size() == 1);
  498.         assert(u[0] == p.GetContactById(1));
  499.  
  500.         auto t = p.GetContactsByAddress("35");
  501.         assert(t.empty());
  502.  
  503.         p.SetContact(1, Contact("x y", "x", "x"));
  504.         assert(u[0]->GetName() == "x y");
  505.  
  506.         w[0]->SetName("aaa");
  507.         w[0]->SetAddress("123");
  508.         assert(p.GetContactById(0)->GetName() == "aaa");
  509.         assert(p.GetContactById(0)->GetAddress() == "123");
  510.     }
  511. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement