Advertisement
avr39ripe

cppPersonStructBase

Oct 20th, 2021
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 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.  
  11. void initPerson(Person& person, uint16_t id, const char* fname, const char* sname, uint8_t age)
  12. {
  13.     person.id = id;
  14.  
  15.     size_t strLen{ strlen(fname) + 1 };
  16.     person.fname = new char[strLen];
  17.     strcpy_s(person.fname, strLen, fname);
  18.  
  19.     strLen = strlen(sname) + 1;
  20.     person.sname = new char[strLen];
  21.     strcpy_s(person.sname, strLen, sname);
  22.  
  23.     person.age = age;
  24. }
  25.  
  26. // BAD !!!! void printPerson(uint16_t id, const char* fname, const char* sname, uint8_t age)
  27. void printPerson(const Person& person, bool fnameFirst = true)
  28. {
  29.     std::cout << person.id << " : " <<
  30.             (fnameFirst ? person.fname : person.sname)
  31.             << ' ' <<
  32.             (fnameFirst ? person.sname : person.fname)
  33.             << " - " << +person.age;
  34. }
  35.  
  36. void setPersonFName(Person& person, const char* fname)
  37. {
  38.     size_t pStrLen{ strlen(person.fname) + 1 };
  39.     size_t strLen{ strlen(fname) + 1 };
  40.  
  41.     // if memory not limited
  42.     if (pStrLen < strLen)
  43.     {
  44.         delete[] person.fname;
  45.         person.fname = new char[strLen];
  46.     }
  47.     strcpy_s(person.fname, strLen, fname);
  48. }
  49.  
  50. void setPersonSName(Person& person, const char* sname)
  51. {
  52.     size_t pStrLen{ strlen(person.sname) + 1 };
  53.     size_t strLen{ strlen(sname) + 1 };
  54.  
  55.     // if memory not limited
  56.     if (pStrLen < strLen)
  57.     {
  58.         delete[] person.sname;
  59.         person.sname = new char[strLen];
  60.     }
  61.     strcpy_s(person.sname, strLen, sname);
  62. }
  63.  
  64. void inputPerson(Person& person)
  65. {
  66.     char buf[50];
  67.     std::cout << "Enter id: "; std::cin >> person.id;
  68.     std::cin.ignore(100, '\n');
  69.    
  70.     std::cout << "Enter First name: ";
  71.     std::cin.getline(buf, 49);
  72.     setPersonFName(person, buf);
  73.    
  74.     std::cout << "Enter Second name: ";
  75.     std::cin.getline(buf, 49);
  76.     setPersonSName(person, buf);
  77.  
  78.     int age;
  79.     std::cout << "Enter age: "; std::cin >> age;  person.age = age;
  80.     std::cin.ignore(100, '\n');
  81. }
  82.  
  83. void clearPerson(Person& person)
  84. {
  85.     delete[] person.fname;
  86.     delete[] person.sname;
  87. }
  88.  
  89. void sortPerson(Person* arr, int arrSize,  bool(*criteria)(const Person&, const Person&))
  90. {
  91.     Person 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.     //Person p1;
  119.     //initPerson(p1, 13, "Fedor", "Strelcov", 25);
  120.  
  121.     //printPerson(p1); std::cout << '\n';
  122.     //p1.id = 12;
  123.     //p1.age = 26;
  124.  
  125.     //setPersonFName(p1, "Teodor");
  126.     //setPersonSName(p1, "Firerer");
  127.     //printPerson(p1, false); std::cout << '\n';
  128.  
  129.     //inputPerson(p1);
  130.     //printPerson(p1); std::cout << '\n';
  131.     //
  132.     //clearPerson(p1);
  133.  
  134.     const int peopleCount{ 3 };
  135.     Person people[peopleCount];
  136.  
  137.     initPerson(people[0], 21, "Ivan", "Petrov", 40);
  138.     initPerson(people[1], 2, "Sidor", "Ivanov", 19);
  139.     initPerson(people[2], 32, "Petr", "Sidorov", 33);
  140.  
  141.     for (int i{ 0 }; i < peopleCount; ++i)
  142.     {
  143.         printPerson(people[i]); std::cout << '\n';
  144.     }
  145.  
  146.     std::cout << "Sort by preson id:\n";
  147.  
  148.     sortPerson(people, peopleCount, personById);
  149.  
  150.     for (int i{ 0 }; i < peopleCount; ++i)
  151.     {
  152.         printPerson(people[i]); std::cout << '\n';
  153.     }
  154.  
  155.     std::cout << "Sort by preson age:\n";
  156.  
  157.     sortPerson(people, peopleCount, personByAge);
  158.  
  159.     for (int i{ 0 }; i < peopleCount; ++i)
  160.     {
  161.         printPerson(people[i]); std::cout << '\n';
  162.     }
  163.  
  164.     for (int i{ 0 }; i < peopleCount; ++i)
  165.     {
  166.         clearPerson(people[i]);
  167.     }
  168.     return 0;
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement