Advertisement
avr39ripe

cppMedalsTable

Mar 22nd, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3.  
  4. class MedalRow
  5. {
  6.     char country[4];
  7.     int medals[3];
  8. public:
  9.     static const int GOLD{ 0 };
  10.     static const int SILVER{ 1 };
  11.     static const int BRONSE{ 2 };
  12.  
  13.     MedalRow(const char* countryP, const int* medalsP)
  14.     {
  15.         strcpy_s(country, 4, countryP ? countryP : "NON");
  16.        
  17.         for (int i{ 0 }; i < 3; ++i)
  18.         {
  19.             medals[i] = medalsP ? medalsP[i] : 0;
  20.         }
  21.     }
  22.     MedalRow() : MedalRow(nullptr, nullptr) {}
  23.     MedalRow& setCountry(const char* countryP)
  24.     {
  25.         if (countryP)
  26.         {
  27.             strcpy_s(country, 4, countryP);
  28.         }
  29.         return *this;
  30.     }
  31.     const char* getCountry()const { return country; }
  32.     int& operator[](int idx)
  33.     {
  34.         assert((idx >= 0 and idx < 3) and "Index out of range!");
  35.         return medals[idx];
  36.     }
  37.     int operator[](int idx)const
  38.     {
  39.         assert((idx >= 0 and idx < 3) and "Index out of range!");
  40.         return medals[idx];
  41.     }
  42.     void print()const
  43.     {
  44.         std::cout << '[' << country << "]-( ";
  45.         for (int i{ 0 }; i < 3; ++i)
  46.         {
  47.             std::cout << medals[i];
  48.             if (i < 2) { std::cout << '\t'; }
  49.         }
  50.         std::cout << " )\n";
  51.     }
  52. };
  53.  
  54. class MedalsTable
  55. {
  56. public:
  57.     static const int maxSize{ 10 };
  58. private:
  59.     MedalRow medalRows[MedalsTable::maxSize];
  60.     int size;
  61.     int findCountry(const char* country)const
  62.     {
  63.         for (int i{ 0 }; i < size; ++i)
  64.         {
  65.             if (strcmp(medalRows[i].getCountry(), country) == 0)
  66.             {
  67.                 return i;
  68.             }
  69.         }
  70.         return -1;
  71.     }
  72. public:
  73.     MedalsTable() : size{ 0 } {};
  74.     MedalRow& operator[](const char* country)
  75.     {
  76.         assert(size < MedalsTable::maxSize and "Table is FULL!");
  77.  
  78.         int idx{ findCountry(country) };
  79.  
  80.         if (idx == -1)
  81.         {
  82.             idx = size++;
  83.             medalRows[idx].setCountry(country);
  84.         }
  85.  
  86.         return medalRows[idx];
  87.     }
  88.     const MedalRow& operator[](const char* country)const
  89.     {
  90.         int idx{ findCountry(country) };
  91.         assert(idx != -1 and "Country not found on const table");
  92.         return medalRows[idx];
  93.     }
  94.     void print()const
  95.     {
  96.         for (int i{ 0 }; i < size; ++i)
  97.         {
  98.             medalRows[i].print();
  99.         }
  100.     }
  101. };
  102.  
  103. class DynArray
  104. {
  105.     int* arr;
  106.     int size;
  107. public:
  108.     DynArray(int sizeP)
  109.         : arr{ new int[sizeP] {} }, size{ sizeP }
  110.     {
  111.     }
  112.     DynArray() : DynArray(5) {}
  113.     DynArray(const DynArray& object)
  114.         : arr{ new int[object.size] }, size{ object.size }
  115.     {
  116.         for (int i{ 0 }; i < size; ++i)
  117.         {
  118.             arr[i] = object.arr[i];
  119.         };
  120.     }
  121.  
  122.     DynArray(DynArray&& object)
  123.         : arr{ object.arr }, size{ object.size }
  124.     {
  125.         object.arr = nullptr;
  126.         object.size = 0;
  127.     }
  128.     DynArray& operator=(const DynArray& object)
  129.     {
  130.         if (!(this == &object))
  131.         {
  132.             if (size != object.size)
  133.             {
  134.                 delete arr;
  135.                 arr = new int[object.size];
  136.             }
  137.  
  138.             size = object.size;
  139.             int* dest{ arr };
  140.             int* src{ object.arr };
  141.             int* const end{ arr + size };
  142.  
  143.             while (dest < end)
  144.             {
  145.                 *dest++ = *src++;
  146.             }
  147.         }
  148.         return *this;
  149.     }
  150.     DynArray& operator=(DynArray&& object)
  151.     {
  152.         if (!(this == &object))
  153.         {
  154.             delete arr;
  155.  
  156.             arr = object.arr;
  157.             size = object.size;
  158.  
  159.             object.arr = nullptr;
  160.             object.size = 0;
  161.  
  162.         }
  163.         return *this;
  164.     }
  165.     void print()const;
  166.     ~DynArray()
  167.     {
  168.         delete[] arr;
  169.     }
  170. };
  171. void DynArray::print()const
  172. {
  173.     for (int i{ 0 }; i < size; ++i)
  174.     {
  175.         std::cout << arr[i] << ' ';
  176.     }
  177.     std::cout << '\n';
  178. }
  179.  
  180.  
  181.  
  182.  
  183. int main()
  184. {
  185.     //MedalRow mr;
  186.     //mr.setCountry("UKR");
  187.     //std::cout << "Country is: " << mr.getCountry() << '\n';
  188.     //mr[MedalRow::GOLD] = 3;
  189.     //mr[MedalRow::BRONSE] = 2;
  190.     //mr[MedalRow::SILVER] = 4;
  191.     //MedalRow mr1{ mr };
  192.  
  193.     //mr1.print();
  194.  
  195.     //mr1[MedalRow::GOLD] = 11;
  196.     //mr1[MedalRow::BRONSE] = 22;
  197.     //mr1[MedalRow::SILVER] = 33;
  198.  
  199.     //mr1.print();
  200.  
  201.     //mr.print();
  202.  
  203.     MedalsTable mt;
  204.  
  205.     mt["UKR"][MedalRow::GOLD] = 39;
  206.     mt["UKR"].print();
  207.     mt["UKR"][MedalRow::SILVER] = 5;
  208.     mt["HUN"][MedalRow::BRONSE] = 42;
  209.     mt["UKR"].print();
  210.     mt["HUN"].print();
  211.     mt["POL"].print();
  212.  
  213.     const MedalsTable mt1{ mt };
  214.     mt1.print();
  215.     mt1["HUN"].print();
  216.     mt1["SLO"].print();
  217.     return 0;
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement