Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8.  
  9. struct patient {
  10. int age;
  11. char name[20];
  12. float balance;
  13. };
  14.  
  15. void display(patient a[]){
  16. for(int i = 0; i < 5; i++){
  17. cout << "Age: " << "\t" << a[i]->age;
  18. cout << "Name: " << "\t" << a[i]->name;
  19. cout << "Balance Due $: " << "\t" << a[i]->balance << endl;
  20. }
  21. }
  22.  
  23. int compareAge(const void* a, const void* b){
  24. const struct patient *aa = a;
  25. const struct patient *bb = b;
  26. if(aa->age < bb->age){
  27. return -1;
  28. } if(aa->age == bb->age){
  29. return 0;
  30. } else
  31. return 1;
  32. }
  33.  
  34. int compareBalance(const void* a, const void* b){
  35. const struct patient *aa = a;
  36. const struct patient *bb = b;
  37. if(aa->balance < bb->balance){
  38. return -1;
  39. } if(aa->balance == bb->balance){
  40. return 0;
  41. } else
  42. return 1;
  43. }
  44.  
  45. int compareName(const void* a, const void* b){
  46. if(strncmp((*(struct patient **)a->name, (*(struct patient **)b->name)))){
  47. return -1;
  48. } if(a->age == b->age){
  49. return 0;
  50. } else
  51. return 1;
  52. // THE FUNCTION RETURNS AN INTEGER AS FOLLOWS:
  53. // -1 IF THE NAME OF THE FIRST PATIENT GOES BEFORE
  54. // THE SECOND PATIENT'S NAME
  55. }
  56.  
  57. int main()
  58. {
  59. int total_patients = 5;
  60.  
  61. // Storing some test data
  62. struct patient patient_list[5] = {
  63. {25, "Juan Valdez ", 1250},
  64. {15, "James Morris ", 2100},
  65. {32, "Tyra Banks ", 750},
  66. {62, "Maria O'Donell", 375},
  67. {53, "Pablo Picasso ", 615}
  68. };
  69.  
  70.  
  71. cout << "Patient List: " << endl;
  72. display(patient_list);
  73. cout << endl;
  74.  
  75. cout << "Sorting..." << endl;
  76. qsort(patient_list, 5, sizeof(int), compareAge);
  77.  
  78. cout << "Patient List - Sorted by Age: " << endl;
  79. cout << display(patient_list);
  80. cout << endl;
  81.  
  82. cout << "Sorting..." << endl;
  83. qsort(patient_list, 5, sizeof(int), compareBalance);
  84.  
  85. cout << "Patient List - Sorted by Balance Due: " << endl;
  86. cout << display(patient_list);
  87. cout << endl;
  88.  
  89. cout << "Sorting..." << endl;
  90. qsort(patient_list, 5, sizeof(int), compareName);
  91.  
  92. cout << "Patient List - Sorted by Name: " << endl;
  93. cout << display(patient_list);
  94. cout << endl;
  95.  
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement