Advertisement
RobertDeMilo

YB3.1 Введение в разработку в нескольких файлах на примере задачи «Синонимы»

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