Advertisement
kutuzzzov

Урок 5

Sep 5th, 2022 (edited)
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. #include <cassert>
  2. #include <iostream>
  3. #include <map>
  4. #include <set>
  5. #include <sstream>
  6. #include <string>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. class Synonyms {
  12. public:
  13.     void Add(const string& first_word, const string& second_word) {
  14.         synonyms_[first_word].insert(second_word);
  15.         synonyms_[second_word].insert(first_word);
  16.     }
  17.  
  18.     size_t GetSynonymCount(const string& word) const {
  19.         if (synonyms_.count(word) != 0) {
  20.             return synonyms_.at(word).size();
  21.         }
  22.         return 0;
  23.     }
  24.  
  25.     bool AreSynonyms(const string& first_word, const string& second_word) const {
  26.         if (synonyms_.count(first_word) != 0) {
  27.             return synonyms_.at(first_word).count(second_word) != 0;
  28.         }
  29.         return false;
  30.     }
  31.  
  32. private:
  33.     map<string, set<string>> synonyms_;
  34. };
  35.  
  36. template <typename T, typename U>
  37. void AssertEqualImpl(const T& t, const U& u, const string& t_str, const string& u_str, const string& file,
  38.     const string& func, unsigned line, const string& hint) {
  39.     if (t != u) {
  40.         cout << boolalpha;
  41.         cout << file << "("s << line << "): "s << func << ": "s;
  42.         cout << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
  43.         cout << t << " != "s << u << "."s;
  44.         if (!hint.empty()) {
  45.             cout << " Hint: "s << hint;
  46.         }
  47.         cout << endl;
  48.         abort();
  49.     }
  50. }
  51.  
  52. #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
  53.  
  54. #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))
  55.  
  56. void AssertImpl(bool value, const string& expr_str, const string& file, const string& func, unsigned line,
  57.     const string& hint) {
  58.     if (!value) {
  59.         cout << file << "("s << line << "): "s << func << ": "s;
  60.         cout << "ASSERT("s << expr_str << ") failed."s;
  61.         if (!hint.empty()) {
  62.             cout << " Hint: "s << hint;
  63.         }
  64.         cout << endl;
  65.         abort();
  66.     }
  67. }
  68.  
  69. #define ASSERT(expr) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
  70.  
  71. #define ASSERT_HINT(expr, hint) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
  72.  
  73. void TestAddingSynonymsIncreasesTheirCount() {
  74.     Synonyms synonyms;
  75.     ASSERT_EQUAL(synonyms.GetSynonymCount("music"s), synonyms.GetSynonymCount("melody"s));
  76.  
  77.     synonyms.Add("music"s, "melody"s);
  78.     ASSERT_EQUAL_HINT(synonyms.GetSynonymCount("music"s) == 1, synonyms.GetSynonymCount("melody"s) == 1, "This will fail"s);
  79.  
  80.     synonyms.Add("music"s, "tune"s);
  81.     ASSERT_HINT(synonyms.GetSynonymCount("music"s) == 2, "This will fail"s);
  82.     ASSERT_EQUAL_HINT(synonyms.GetSynonymCount("tune"s) == 1, synonyms.GetSynonymCount("melody"s) == 1, "This will fail"s);
  83. }
  84.  
  85. void TestAreSynonyms() {
  86.     Synonyms synonyms;
  87.     ASSERT(!synonyms.AreSynonyms("winner"s, "champion"s));
  88.  
  89.     synonyms.Add("winner"s, "champion"s);
  90.     synonyms.Add("good"s, "nice"s);
  91.  
  92.     ASSERT(synonyms.AreSynonyms("winner"s, "champion"s));
  93.     ASSERT(synonyms.AreSynonyms("champion"s, "winner"s));
  94.  
  95.     ASSERT(!synonyms.AreSynonyms("good"s, "champion"s));
  96.     ASSERT(synonyms.AreSynonyms("good"s, "nice"s));
  97. }
  98.  
  99. void TestSynonyms() {
  100.     TestAddingSynonymsIncreasesTheirCount();
  101.     TestAreSynonyms();
  102. }
  103.  
  104. int main() {
  105.     TestSynonyms();
  106.  
  107.     Synonyms synonyms;
  108.  
  109.     string line;
  110.     while (getline(cin, line)) {
  111.         istringstream command(line);
  112.         string action;
  113.         command >> action;
  114.  
  115.         if (action == "ADD"s) {
  116.             string first_word, second_word;
  117.             command >> first_word >> second_word;
  118.             synonyms.Add(first_word, second_word);
  119.         }
  120.         else if (action == "COUNT"s) {
  121.             string word;
  122.             command >> word;
  123.             cout << synonyms.GetSynonymCount(word) << endl;
  124.         }
  125.         else if (action == "CHECK"s) {
  126.             string first_word, second_word;
  127.             command >> first_word >> second_word;
  128.             if (synonyms.AreSynonyms(first_word, second_word)) {
  129.                 cout << "YES"s << endl;
  130.             }
  131.             else {
  132.                 cout << "NO"s << endl;
  133.             }
  134.         }
  135.         else if (action == "EXIT") {
  136.             break;
  137.         }
  138.     }
  139.  
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement