Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct Person
- {
- uint16_t id;
- char* fname;
- char* sname;
- uint8_t age;
- };
- void initPerson(Person& person, uint16_t id, const char* fname, const char* sname, uint8_t age)
- {
- person.id = id;
- size_t strLen{ strlen(fname) + 1 };
- person.fname = new char[strLen];
- strcpy_s(person.fname, strLen, fname);
- strLen = strlen(sname) + 1;
- person.sname = new char[strLen];
- strcpy_s(person.sname, strLen, sname);
- person.age = age;
- }
- // BAD !!!! void printPerson(uint16_t id, const char* fname, const char* sname, uint8_t age)
- void printPerson(const Person& person, bool fnameFirst = true)
- {
- std::cout << person.id << " : " <<
- (fnameFirst ? person.fname : person.sname)
- << ' ' <<
- (fnameFirst ? person.sname : person.fname)
- << " - " << +person.age;
- }
- void setPersonFName(Person& person, const char* fname)
- {
- size_t pStrLen{ strlen(person.fname) + 1 };
- size_t strLen{ strlen(fname) + 1 };
- // if memory not limited
- if (pStrLen < strLen)
- {
- delete[] person.fname;
- person.fname = new char[strLen];
- }
- strcpy_s(person.fname, strLen, fname);
- }
- void setPersonSName(Person& person, const char* sname)
- {
- size_t pStrLen{ strlen(person.sname) + 1 };
- size_t strLen{ strlen(sname) + 1 };
- // if memory not limited
- if (pStrLen < strLen)
- {
- delete[] person.sname;
- person.sname = new char[strLen];
- }
- strcpy_s(person.sname, strLen, sname);
- }
- void inputPerson(Person& person)
- {
- char buf[50];
- std::cout << "Enter id: "; std::cin >> person.id;
- std::cin.ignore(100, '\n');
- std::cout << "Enter First name: ";
- std::cin.getline(buf, 49);
- setPersonFName(person, buf);
- std::cout << "Enter Second name: ";
- std::cin.getline(buf, 49);
- setPersonSName(person, buf);
- int age;
- std::cout << "Enter age: "; std::cin >> age; person.age = age;
- std::cin.ignore(100, '\n');
- }
- void clearPerson(Person& person)
- {
- delete[] person.fname;
- delete[] person.sname;
- }
- void sortPerson(Person* arr, int arrSize, bool(*criteria)(const Person&, const Person&))
- {
- Person copy;
- for (int head{ 0 }; head < arrSize; ++head)
- {
- for (int tail{ arrSize - 1 }; tail > head; --tail)
- {
- if (criteria(arr[tail], arr[head]))
- {
- copy = arr[tail];
- arr[tail] = arr[head];
- arr[head] = copy;
- }
- }
- }
- }
- bool personById(const Person& personA, const Person& personB)
- {
- return personA.id < personB.id;
- }
- bool personByAge(const Person& personA, const Person& personB)
- {
- return personA.age < personB.age;
- }
- int main()
- {
- //Person p1;
- //initPerson(p1, 13, "Fedor", "Strelcov", 25);
- //printPerson(p1); std::cout << '\n';
- //p1.id = 12;
- //p1.age = 26;
- //setPersonFName(p1, "Teodor");
- //setPersonSName(p1, "Firerer");
- //printPerson(p1, false); std::cout << '\n';
- //inputPerson(p1);
- //printPerson(p1); std::cout << '\n';
- //
- //clearPerson(p1);
- const int peopleCount{ 3 };
- Person people[peopleCount];
- initPerson(people[0], 21, "Ivan", "Petrov", 40);
- initPerson(people[1], 2, "Sidor", "Ivanov", 19);
- initPerson(people[2], 32, "Petr", "Sidorov", 33);
- for (int i{ 0 }; i < peopleCount; ++i)
- {
- printPerson(people[i]); std::cout << '\n';
- }
- std::cout << "Sort by preson id:\n";
- sortPerson(people, peopleCount, personById);
- for (int i{ 0 }; i < peopleCount; ++i)
- {
- printPerson(people[i]); std::cout << '\n';
- }
- std::cout << "Sort by preson age:\n";
- sortPerson(people, peopleCount, personByAge);
- for (int i{ 0 }; i < peopleCount; ++i)
- {
- printPerson(people[i]); std::cout << '\n';
- }
- for (int i{ 0 }; i < peopleCount; ++i)
- {
- clearPerson(people[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement