fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct BorrowedBook {
  7. string title;
  8. string author;
  9.  
  10. void printStructureData() {
  11. cout << "Title: " << title << ", Author: " << author << endl;
  12. }
  13. };
  14.  
  15. struct LibraryMember {
  16. string name;
  17. int id;
  18. BorrowedBook book;
  19.  
  20. void printStructureData() {
  21. cout << "Member Name: " << name << ", Member ID: " << id << endl;
  22. book.printStructureData();
  23. }
  24. };
  25.  
  26. bool compareParameters(const LibraryMember &member1, const LibraryMember &member2) {
  27. return member1.name == member2.name && member1.id == member2.id;
  28. }
  29.  
  30. void sortMembers(LibraryMember* members, int size) {
  31. for (int i = 0; i < size - 1; i++) {
  32. for (int j = 0; j < size - i - 1; j++) {
  33. if (members[j].id > members[j + 1].id) {
  34. LibraryMember temp = members[j];
  35. members[j] = members[j + 1];
  36. members[j + 1] = temp;
  37. }
  38. }
  39. }
  40. }
  41.  
  42. int main() {
  43. const int SIZE = 2;
  44. LibraryMember members[SIZE];
  45.  
  46. members[0].name = "Sulu";
  47. members[0].id = 2;
  48. members[0].book.title = "Sad";
  49. members[0].book.author = "Zhambyl";
  50.  
  51. members[1].name = "Beka";
  52. members[1].id = 1;
  53. members[1].book.title = "Hello";
  54. members[1].book.author = "Abay";
  55.  
  56. for (int i = 0; i < SIZE; i++) {
  57. members[i].printStructureData();
  58. cout << endl;
  59. }
  60.  
  61. bool areEqual = compareParameters(members[0], members[1]);
  62. cout << "Are the two members equal? " << (areEqual ? "Yes" : "No") << endl;
  63.  
  64. sortMembers(members, SIZE);
  65.  
  66. cout << "\nSorted Members by ID:\n";
  67. for (int i = 0; i < SIZE; i++) {
  68. members[i].printStructureData();
  69. cout << endl;
  70. }
  71.  
  72. return 0;
  73. }
  74.  
Success #stdin #stdout 0s 5284KB
stdin
3
3
stdout
Member Name: Sulu, Member ID: 2
Title: Sad, Author: Zhambyl

Member Name: Beka, Member ID: 1
Title: Hello, Author: Abay

Are the two members equal? No

Sorted Members by ID:
Member Name: Beka, Member ID: 1
Title: Hello, Author: Abay

Member Name: Sulu, Member ID: 2
Title: Sad, Author: Zhambyl