Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <cstring>
- #include <stdio.h>
- using namespace std;
- struct patient {
- int age;
- char name[20];
- float balance;
- };
- void display(patient a[]){
- for(int i = 0; i < 5; i++){
- cout << "Age: " << "\t" << a[i]->age;
- cout << "Name: " << "\t" << a[i]->name;
- cout << "Balance Due $: " << "\t" << a[i]->balance << endl;
- }
- }
- int compareAge(const void* a, const void* b){
- const struct patient *aa = a;
- const struct patient *bb = b;
- if(aa->age < bb->age){
- return -1;
- } if(aa->age == bb->age){
- return 0;
- } else
- return 1;
- }
- int compareBalance(const void* a, const void* b){
- const struct patient *aa = a;
- const struct patient *bb = b;
- if(aa->balance < bb->balance){
- return -1;
- } if(aa->balance == bb->balance){
- return 0;
- } else
- return 1;
- }
- int compareName(const void* a, const void* b){
- if(strncmp((*(struct patient **)a->name, (*(struct patient **)b->name)))){
- return -1;
- } if(a->age == b->age){
- return 0;
- } else
- return 1;
- // THE FUNCTION RETURNS AN INTEGER AS FOLLOWS:
- // -1 IF THE NAME OF THE FIRST PATIENT GOES BEFORE
- // THE SECOND PATIENT'S NAME
- }
- int main()
- {
- int total_patients = 5;
- // Storing some test data
- struct patient patient_list[5] = {
- {25, "Juan Valdez ", 1250},
- {15, "James Morris ", 2100},
- {32, "Tyra Banks ", 750},
- {62, "Maria O'Donell", 375},
- {53, "Pablo Picasso ", 615}
- };
- cout << "Patient List: " << endl;
- display(patient_list);
- cout << endl;
- cout << "Sorting..." << endl;
- qsort(patient_list, 5, sizeof(int), compareAge);
- cout << "Patient List - Sorted by Age: " << endl;
- cout << display(patient_list);
- cout << endl;
- cout << "Sorting..." << endl;
- qsort(patient_list, 5, sizeof(int), compareBalance);
- cout << "Patient List - Sorted by Balance Due: " << endl;
- cout << display(patient_list);
- cout << endl;
- cout << "Sorting..." << endl;
- qsort(patient_list, 5, sizeof(int), compareName);
- cout << "Patient List - Sorted by Name: " << endl;
- cout << display(patient_list);
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement