Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- class MedalRow
- {
- char country[4];
- int medals[3];
- public:
- static const int GOLD{ 0 };
- static const int SILVER{ 1 };
- static const int BRONSE{ 2 };
- MedalRow(const char* countryP, const int* medalsP)
- {
- strcpy_s(country, 4, countryP ? countryP : "NON");
- for (int i{ 0 }; i < 3; ++i)
- {
- medals[i] = medalsP ? medalsP[i] : 0;
- }
- }
- MedalRow() : MedalRow(nullptr, nullptr) {}
- MedalRow& setCountry(const char* countryP)
- {
- if (countryP)
- {
- strcpy_s(country, 4, countryP);
- }
- return *this;
- }
- const char* getCountry()const { return country; }
- int& operator[](int idx)
- {
- assert((idx >= 0 and idx < 3) and "Index out of range!");
- return medals[idx];
- }
- int operator[](int idx)const
- {
- assert((idx >= 0 and idx < 3) and "Index out of range!");
- return medals[idx];
- }
- void print()const
- {
- std::cout << '[' << country << "]-( ";
- for (int i{ 0 }; i < 3; ++i)
- {
- std::cout << medals[i];
- if (i < 2) { std::cout << '\t'; }
- }
- std::cout << " )\n";
- }
- };
- class MedalsTable
- {
- public:
- static const int maxSize{ 10 };
- private:
- MedalRow medalRows[MedalsTable::maxSize];
- int size;
- int findCountry(const char* country)const
- {
- for (int i{ 0 }; i < size; ++i)
- {
- if (strcmp(medalRows[i].getCountry(), country) == 0)
- {
- return i;
- }
- }
- return -1;
- }
- public:
- MedalsTable() : size{ 0 } {};
- MedalRow& operator[](const char* country)
- {
- assert(size < MedalsTable::maxSize and "Table is FULL!");
- int idx{ findCountry(country) };
- if (idx == -1)
- {
- idx = size++;
- medalRows[idx].setCountry(country);
- }
- return medalRows[idx];
- }
- const MedalRow& operator[](const char* country)const
- {
- int idx{ findCountry(country) };
- assert(idx != -1 and "Country not found on const table");
- return medalRows[idx];
- }
- void print()const
- {
- for (int i{ 0 }; i < size; ++i)
- {
- medalRows[i].print();
- }
- }
- };
- class DynArray
- {
- int* arr;
- int size;
- public:
- DynArray(int sizeP)
- : arr{ new int[sizeP] {} }, size{ sizeP }
- {
- }
- DynArray() : DynArray(5) {}
- DynArray(const DynArray& object)
- : arr{ new int[object.size] }, size{ object.size }
- {
- for (int i{ 0 }; i < size; ++i)
- {
- arr[i] = object.arr[i];
- };
- }
- DynArray(DynArray&& object)
- : arr{ object.arr }, size{ object.size }
- {
- object.arr = nullptr;
- object.size = 0;
- }
- DynArray& operator=(const DynArray& object)
- {
- if (!(this == &object))
- {
- if (size != object.size)
- {
- delete arr;
- arr = new int[object.size];
- }
- size = object.size;
- int* dest{ arr };
- int* src{ object.arr };
- int* const end{ arr + size };
- while (dest < end)
- {
- *dest++ = *src++;
- }
- }
- return *this;
- }
- DynArray& operator=(DynArray&& object)
- {
- if (!(this == &object))
- {
- delete arr;
- arr = object.arr;
- size = object.size;
- object.arr = nullptr;
- object.size = 0;
- }
- return *this;
- }
- void print()const;
- ~DynArray()
- {
- delete[] arr;
- }
- };
- void DynArray::print()const
- {
- for (int i{ 0 }; i < size; ++i)
- {
- std::cout << arr[i] << ' ';
- }
- std::cout << '\n';
- }
- int main()
- {
- //MedalRow mr;
- //mr.setCountry("UKR");
- //std::cout << "Country is: " << mr.getCountry() << '\n';
- //mr[MedalRow::GOLD] = 3;
- //mr[MedalRow::BRONSE] = 2;
- //mr[MedalRow::SILVER] = 4;
- //MedalRow mr1{ mr };
- //mr1.print();
- //mr1[MedalRow::GOLD] = 11;
- //mr1[MedalRow::BRONSE] = 22;
- //mr1[MedalRow::SILVER] = 33;
- //mr1.print();
- //mr.print();
- MedalsTable mt;
- mt["UKR"][MedalRow::GOLD] = 39;
- mt["UKR"].print();
- mt["UKR"][MedalRow::SILVER] = 5;
- mt["HUN"][MedalRow::BRONSE] = 42;
- mt["UKR"].print();
- mt["HUN"].print();
- mt["POL"].print();
- const MedalsTable mt1{ mt };
- mt1.print();
- mt1["HUN"].print();
- mt1["SLO"].print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement