Advertisement
RobertDeMilo

Теория. Применяем фреймворк на практике

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