Advertisement
avr39ripe

cppPersonStructAdv

Oct 20th, 2021
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct Person
  4. {
  5.     uint16_t id;
  6.     char* fname;
  7.     char* sname;
  8.     uint8_t age;
  9.  
  10.     void init(uint16_t idP, const char* fnameP, const char* snameP, uint8_t ageP)
  11.     {
  12.         id = idP;
  13.  
  14.         size_t strLen{ strlen(fnameP) + 1 };
  15.         fname = new char[strLen];
  16.         strcpy_s(fname, strLen, fnameP);
  17.  
  18.         strLen = strlen(snameP) + 1;
  19.         sname = new char[strLen];
  20.         strcpy_s(sname, strLen, snameP);
  21.  
  22.         age = ageP;
  23.     }
  24.  
  25.     void print(bool fnameFirst = true)
  26.     {
  27.         std::cout << id << " : " <<
  28.             (fnameFirst ? fname : sname)
  29.             << ' ' <<
  30.             (fnameFirst ? sname :fname)
  31.             << " - " << +age;
  32.     }
  33.  
  34.     void setFName(const char* fnameP)
  35.     {
  36.         size_t pStrLen{ strlen(fname) + 1 };
  37.         size_t strLen{ strlen(fnameP) + 1 };
  38.  
  39.         // if memory not limited
  40.         if (pStrLen < strLen)
  41.         {
  42.             delete[] fname;
  43.             fname = new char[strLen];
  44.         }
  45.         strcpy_s(fname, strLen, fnameP);
  46.     }
  47.  
  48.     void setSName(const char* snameP)
  49.     {
  50.         size_t pStrLen{ strlen(sname) + 1 };
  51.         size_t strLen{ strlen(snameP) + 1 };
  52.  
  53.         // if memory not limited
  54.         if (pStrLen < strLen)
  55.         {
  56.             delete[] sname;
  57.             sname = new char[strLen];
  58.         }
  59.         strcpy_s(sname, strLen, snameP);
  60.     }
  61.  
  62.     void input()
  63.     {
  64.         char buf[50];
  65.         std::cout << "Enter id: "; std::cin >> id;
  66.         std::cin.ignore(100, '\n');
  67.  
  68.         std::cout << "Enter First name: ";
  69.         std::cin.getline(buf, 49);
  70.         setFName(buf);
  71.  
  72.         std::cout << "Enter Second name: ";
  73.         std::cin.getline(buf, 49);
  74.         setSName(buf);
  75.  
  76.         int ageP;
  77.         std::cout << "Enter age: "; std::cin >> ageP;  age = ageP;
  78.         std::cin.ignore(100, '\n');
  79.     }
  80.  
  81.     void clear()
  82.     {
  83.         delete[] fname;
  84.         delete[] sname;
  85.     }
  86. };
  87.  
  88. template <typename T>
  89. void sort(T* arr, int arrSize,  bool(*criteria)(const T&, const T&))
  90. {
  91.     T copy;
  92.     for (int head{ 0 }; head < arrSize; ++head)
  93.     {
  94.         for (int tail{ arrSize - 1 }; tail > head; --tail)
  95.         {
  96.             if (criteria(arr[tail], arr[head]))
  97.             {
  98.                 copy = arr[tail];
  99.                 arr[tail] = arr[head];
  100.                 arr[head] = copy;
  101.             }
  102.         }
  103.     }
  104. }
  105.  
  106. bool personById(const Person& personA, const Person& personB)
  107. {
  108.     return personA.id < personB.id;
  109. }
  110.  
  111. bool personByAge(const Person& personA, const Person& personB)
  112. {
  113.     return personA.age < personB.age;
  114. }
  115.  
  116. int main()
  117. {
  118.     const int peopleCount{ 3 };
  119.     Person people[peopleCount];
  120.  
  121.     people[0].init(21, "Ivan", "Petrov", 40);
  122.     people[1].init(2, "Sidor", "Ivanov", 19);
  123.     people[2].init(32, "Petr", "Sidorov", 33);
  124.  
  125.     for (int i{ 0 }; i < peopleCount; ++i)
  126.     {
  127.         people[i].print(); std::cout << '\n';
  128.     }
  129.  
  130.     std::cout << "Sort by preson id:\n";
  131.  
  132.     sort(people, peopleCount, personById);
  133.  
  134.     for (int i{ 0 }; i < peopleCount; ++i)
  135.     {
  136.         people[i].print(); std::cout << '\n';
  137.     }
  138.  
  139.     std::cout << "Sort by preson age:\n";
  140.  
  141.     sort(people, peopleCount, personByAge);
  142.  
  143.     for (int i{ 0 }; i < peopleCount; ++i)
  144.     {
  145.         people[i].print(); std::cout << '\n';
  146.     }
  147.  
  148.     for (int i{ 0 }; i < peopleCount; ++i)
  149.     {
  150.         people[i].clear();
  151.     }
  152.     return 0;
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement