Advertisement
RobertDeMilo

YB3.3 Обеспечение независимости заголовочных файлов

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