Advertisement
kutuzzzov

урок 4

Aug 26th, 2022
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <iomanip>
  4. #include <map>
  5. #include <set>
  6. #include <string>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. // вывод запятых
  12. template <typename Term>
  13. void Print (ostream& out, const Term& container) {
  14.     bool is_first = ", ";
  15.     for (const auto& element : container) {
  16.         if (is_first) {
  17.             out << element; is_first = !", ";
  18.         } else {
  19.             out << ", " << element;
  20.         }
  21.     }
  22. return;
  23. }
  24. // шаблон для вектора
  25. template <typename T>
  26. ostream& operator<<(ostream& out, const vector<T>& container) {
  27.     Print(out << "["s, container);
  28. return out << "]"s;
  29. }
  30. // шаблон для множества
  31. template <typename T, typename U>
  32. ostream& operator<<(ostream& out, const set<T, U>& container) {
  33.     Print(out << "{"s, container);
  34. return out << "}"s;
  35. }
  36. // шаблон для пар
  37. template <typename T, typename U>
  38. ostream& operator<<(ostream& out, const pair<T, U>& container) {
  39.     out << container.first;
  40.     out << ": "s;
  41.     out << container.second;
  42.     return out;
  43. }
  44. // шаблон для словаря
  45. template <typename T, typename U>
  46. ostream& operator<<(ostream& out, const map<T, U>& container) {
  47.     out << "{";
  48.     Print (out, container);
  49.     out << "}";
  50.     return out;
  51. }
  52.  
  53. template <typename T, typename U>
  54. void AssertEqualImpl(const T& t, const U& u, const string& t_str, const string& u_str, const string& file,
  55.                      const string& func, unsigned line, const string& hint) {
  56.     if (t != u) {
  57.         cout << boolalpha;
  58.         cout << file << "("s << line << "): "s << func << ": "s;
  59.         cout << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
  60.         cout << t << " != "s << u << "."s;
  61.         if (!hint.empty()) {
  62.             cout << " Hint: "s << hint;
  63.         }
  64.         cout << endl;
  65.         abort();
  66.     }
  67. }
  68.  
  69. #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
  70.  
  71. #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))
  72.  
  73. void AssertImpl(bool value, const string& expr_str, const string& file, const string& func, unsigned line,
  74.                 const string& hint) {
  75.     if (!value) {
  76.         cout << file << "("s << line << "): "s << func << ": "s;
  77.         cout << "ASSERT("s << expr_str << ") failed."s;
  78.         if (!hint.empty()) {
  79.             cout << " Hint: "s << hint;
  80.         }
  81.         cout << endl;
  82.         abort();
  83.     }
  84. }
  85.  
  86. #define ASSERT(expr) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
  87.  
  88. #define ASSERT_HINT(expr, hint) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
  89.  
  90. vector<int> TakeEvens(const vector<int>& numbers) {
  91.     vector<int> evens;
  92.     for (int x : numbers) {
  93.         if (x % 2 == 0) {
  94.             evens.push_back(x);
  95.         }
  96.     }
  97.     return evens;
  98. }
  99.  
  100. map<string, int> TakeAdults(const map<string, int>& people) {
  101.     map<string, int> adults;
  102.     for (const auto& [name, age] : people) {
  103.         if (age >= 18) {
  104.             adults[name] = age;
  105.         }
  106.     }
  107.     return adults;
  108. }
  109.  
  110. bool IsPrime(int n) {
  111.     if (n < 2) {
  112.         return false;
  113.     }
  114.     int i = 2;
  115.     while (i * i <= n) {
  116.         if (n % i == 0) {
  117.             return false;
  118.         }
  119.         ++i;
  120.     }
  121.     return true;
  122. }
  123.  
  124. set<int> TakePrimes(const set<int>& numbers) {
  125.     set<int> primes;
  126.     for (int number : numbers) {
  127.         if (IsPrime(number)) {
  128.             primes.insert(number);
  129.         }
  130.     }
  131.     return primes;
  132. }
  133.  
  134. int main() {
  135.     {
  136.         const set<int> numbers = {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
  137.         const set<int> expected_primes = {2, 3, 5, 7, 11, 13};
  138.         ASSERT_EQUAL(TakePrimes(numbers), expected_primes);
  139.     }
  140.  
  141.     {
  142.         const map<string, int> people = {{"Ivan"s, 19}, {"Sergey"s, 16}, {"Alexey"s, 18}};
  143.         const map<string, int> expected_adults = {{"Alexey"s, 18}, {"Ivan"s, 19}};
  144.         ASSERT_EQUAL(TakeAdults(people), expected_adults);
  145.     }
  146.  
  147.     {
  148.         const vector<int> numbers = {3, 2, 1, 0, 3, 6};
  149.         const vector<int> expected_evens = {2, 0, 6};
  150.         ASSERT_EQUAL(TakeEvens(numbers), expected_evens);
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement