Advertisement
RobertDeMilo

YB3.5 Понятия объявления и определения

Oct 30th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.02 KB | None | 0 0
  1. main
  2.  
  3. #include "tests.h"
  4. #include "synonyms.h"
  5.  
  6. #include <iostream>
  7. #include <exception>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14.     TestAll();
  15.  
  16.     int q;
  17.     cin >> q;
  18.     map<string, set<string>>synonyms;
  19.  
  20.     for (int i = 0; i < q; i++)
  21.     {
  22.         string operation_code;
  23.         cin >> operation_code;
  24.  
  25.         if (operation_code == "ADD")
  26.         {
  27.             string first_word, second_word;
  28.             cin >> first_word >> second_word;
  29.  
  30.             ADDSYNONYMS(synonyms, first_word, second_word);
  31.            
  32.         }
  33.         else if (operation_code == "COUNT")
  34.         {
  35.             string word;
  36.             cin >> word;
  37.  
  38.             cout << GETSYNONYMCOUNT(synonyms, word) << endl;
  39.            
  40.         }
  41.         else if (operation_code == "CHECK")
  42.         {
  43.             string first_word, second_word;
  44.             cin >> first_word >> second_word;
  45.  
  46.             if (ARESYNONYMS(synonyms, first_word, second_word))
  47.             {
  48.                 cout << "YES" << endl;
  49.             }
  50.             else
  51.             {
  52.                 cout << "NO" << endl;
  53.             }
  54.         }
  55.     }
  56.  
  57.     return 0;
  58. }
  59. ####################################################################################################################
  60. synonyms.h
  61.  
  62. #pragma once
  63.  
  64. #include <string>
  65. #include <set>
  66. #include <map>
  67.  
  68. using namespace std;
  69.  
  70. using SYNONYMS = map<string, set<string>>;
  71.  
  72. void ADDSYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word);
  73. bool ARESYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word);
  74. size_t GETSYNONYMCOUNT(map<string, set<string>>& synonyms, const string& first_word);
  75.  
  76. //**********************************************************************************************************
  77.  
  78. void ADDSYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
  79. {
  80.     synonyms[second_word].insert(first_word);
  81.     synonyms[first_word].insert(second_word);
  82. }
  83.  
  84. //**********************************************************************************************************
  85.  
  86. size_t GETSYNONYMCOUNT(map<string, set<string>>& synonyms, const string& first_word)
  87. {
  88.     return synonyms[first_word].size();
  89. }
  90.  
  91. //**********************************************************************************************************
  92.  
  93. bool ARESYNONYMS(map<string, set<string>>& synonyms, const string& first_word, const string& second_word)
  94. {
  95.     return synonyms[first_word].count(second_word) == 1;
  96. }
  97.  
  98. //**********************************************************************************************************
  99. ####################################################################################################################
  100. test_runner.h
  101.  
  102. #pragma once
  103.  
  104. #include <iostream>
  105. #include <sstream>
  106. #include <string>
  107. #include <set>
  108. #include <map>
  109.  
  110. using namespace std;
  111.  
  112. template<class T>
  113. ostream& operator<<(ostream& os, const set<T>& s);
  114.  
  115. template <class K, class V>
  116. ostream& operator<<(ostream& os, const map<K, V>& m);
  117.  
  118. template<class T, class U>
  119. void AssertEqual(const T& t, const U& u, const string& hint);
  120.  
  121. void Assert(bool b, const string& hint);
  122.  
  123. class TestRunner
  124. {
  125. public:
  126.  
  127.     template <class TestFunc>
  128.     void RunTest(TestFunc func, const string& test_name);
  129.  
  130.     ~TestRunner();
  131.  
  132. private:
  133.  
  134.     int fail_count = 0;
  135. };
  136.  
  137.  
  138. template<class T>
  139. ostream& operator<<(ostream& os, const set<T>& s)
  140. {
  141.     os << "{";
  142.     bool first = true;
  143.  
  144.     for (const auto& x : s)
  145.     {
  146.         if (!first)
  147.         {
  148.             os << ", ";
  149.         }
  150.         first = false;
  151.         os << x;
  152.     }
  153.     return os << "}";
  154. }
  155. //**********************************************************************************************************
  156.  
  157. template <class K, class V>
  158. ostream& operator<<(ostream& os, const map<K, V>& m)
  159. {
  160.     os << "{";
  161.     bool first = true;
  162.     for (const auto& kv : m)
  163.     {
  164.         if (!first)
  165.         {
  166.             os << ", ";
  167.         }
  168.         first = false;
  169.         os << kv.first << ": " << kv.second;
  170.     }
  171.     return os << "}";
  172. }
  173.  
  174. //**********************************************************************************************************
  175.  
  176. template<class T, class U>
  177. void AssertEqual(const T& t, const U& u, const string& hint)
  178. {
  179.     if (t != u)
  180.     {
  181.         ostringstream os;
  182.         os << "Assertion failed: " << t << " != " << u << " Hint: " << hint;
  183.         throw runtime_error(os.str());
  184.     }
  185. }
  186.  
  187. //**********************************************************************************************************
  188.  
  189. void Assert(bool b, const string& hint)
  190. {
  191.     AssertEqual(b, true, hint);
  192. }
  193.  
  194. //**********************************************************************************************************
  195.  
  196. template <class TestFunc>
  197. void TestRunner::RunTest(TestFunc func, const string& test_name)
  198. {
  199.     try
  200.     {
  201.         func();
  202.         cerr << test_name << " OK" << endl;
  203.     }
  204.     catch (runtime_error& e)
  205.     {
  206.         ++fail_count;
  207.         cerr << test_name << " fail: " << e.what() << endl;
  208.     }
  209. }
  210.  
  211. TestRunner::~TestRunner()
  212. {
  213.     if (fail_count > 0)
  214.     {
  215.         cerr << fail_count << " tests failed. Terminate";
  216.         exit(1);
  217.     }
  218. }
  219. ####################################################################################################################
  220. tests.h
  221.  
  222. #pragma once
  223.  
  224. #include "test_runner.h"
  225. #include "synonyms.h"
  226.  
  227. // Interface
  228.  
  229. void TESTADDSYNONYMS();
  230. void TESTARESYNONYMS();  
  231. void TESTCOUNT();
  232. void TestAll();
  233.  
  234. // Implementation
  235.  
  236. void TESTADDSYNONYMS()
  237. {
  238.     {
  239.         map<string, set<string>> empty;
  240.         ADDSYNONYMS(empty, "a", "b");
  241.         const map<string, set<string>> expected = { {"a",{"b"}},
  242.                                                     {"b",{"a"}} };
  243.    
  244.         AssertEqual(empty, expected, "Add to empty");
  245.     }
  246.  
  247.     {
  248.         map<string, set<string>> synonyms = { {"a",{"b"}},
  249.                                               {"b",{"a","c"}},
  250.                                               {"c",{"b"}} };
  251.         ADDSYNONYMS(synonyms, "a", "c");
  252.         const map<string, set<string>> expected = { {"a",{"b","c"}},
  253.                                                     {"b",{"a","c"}},
  254.                                                     {"c",{"b","a"}} };
  255.        
  256.         AssertEqual(synonyms, expected, "Add to non-empty");
  257.  
  258.     }
  259. }
  260. //**********************************************************************************************************
  261.  
  262. void TESTCOUNT()
  263. {
  264.     {
  265.         map<string, set<string>> empty;
  266.      
  267.         AssertEqual(GETSYNONYMCOUNT(empty, "a"), 0u, "count for empty");
  268.     }
  269.  
  270.     {
  271.         map<string, set<string>> synonyms = { {"a",{"b","c"}},
  272.                                               {"b",{"a"}},
  273.                                               {"c",{"a"}} };
  274.  
  275.        
  276.         AssertEqual(GETSYNONYMCOUNT(synonyms, "a"), 2u, "count for a");
  277.         AssertEqual(GETSYNONYMCOUNT(synonyms, "b"), 1u, "count for b");
  278.         AssertEqual(GETSYNONYMCOUNT(synonyms, "z"), 0u, "count for z");
  279.     }
  280. }
  281. //**********************************************************************************************************
  282.  
  283. void TESTARESYNONYMS()
  284. {
  285.     {
  286.         map<string, set<string>> empty;
  287.        
  288.         Assert(!ARESYNONYMS(empty, "a", "b"), "empty a b");
  289.         Assert(!ARESYNONYMS(empty, "a", "b"), "empty b a");
  290.     }
  291.  
  292.     {
  293.         map<string, set<string>> synonyms = { {"a",{"b","c"}},
  294.                                               {"b",{"a"}},
  295.                                               {"c",{"a"}} };
  296.  
  297.         Assert(ARESYNONYMS(synonyms, "a", "b"), " ");
  298.         Assert(ARESYNONYMS(synonyms, "b", "a"), " ");
  299.         Assert(ARESYNONYMS(synonyms, "a", "c"), " ");
  300.         Assert(ARESYNONYMS(synonyms, "c", "a"), " ");
  301.         Assert(!ARESYNONYMS(synonyms, "b", "c"), " ");
  302.         Assert(!ARESYNONYMS(synonyms, "c", "b"), " ");
  303.     }
  304.    
  305. }
  306. //**********************************************************************************************************
  307.  
  308. void TestAll()
  309. {
  310.     TestRunner tr;
  311.  
  312.     tr.RunTest(TESTARESYNONYMS, "TESTARESYNONYMS");
  313.     tr.RunTest(TESTADDSYNONYMS, "TESTADDSYNONYMS");
  314.     tr.RunTest(TESTCOUNT, "TESTCOUNT");
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement