1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <iostream>
#include <string>
using namespace std;
struct Series
{
char name[50];
char genre[50];
char country[50];
float rating;
} s[5] = {
{"SeriEs 1", "Action", "US", 8.7}, // change some cases
{"Series 2", "Fantasy", "US", 8.4},
{"SERIES 3", "drama", "US", 8.1},
{"SeRies 4", "fantasy", "US", 8.2},
{"Series 5", "Fantasy", "US", 8.9}};
enum direction{up,down};
int direction;
int callbackname(const void * n1, const void * n2)
{
if (direction == down)
{
if (tolower(*((struct Series*)n1)->name) > tolower(*((struct Series*)n2)->name)) return -1;
if (tolower(*((struct Series*)n1)->name) < tolower(*((struct Series*)n2)->name)) return 1;
}
else
{
if (tolower(*((struct Series*)n1)->name) > tolower(*((struct Series*)n2)->name)) return 1;
if (tolower(*((struct Series*)n1)->name) < tolower(*((struct Series*)n2)->name)) return -1;
}
}
int callbackgenre(const void * n1, const void * n2)
{
if (direction == down)
{
if (tolower(*((struct Series*)n1)->genre) > tolower(*((struct Series*)n2)->genre)) return -1;
if (tolower(*((struct Series*)n1)->genre) < tolower(*((struct Series*)n2)->genre)) return 1;
}
else
{
if (tolower(*((struct Series*)n1)->genre) > tolower(*((struct Series*)n2)->genre)) return 1;
if (tolower(*((struct Series*)n1)->genre) < tolower(*((struct Series*)n2)->genre)) return -1;
}
}
int callbackcountry(const void * n1, const void * n2)
{
if (direction == down)
{
if (tolower(*((struct Series*)n1)->country) > tolower(*((struct Series*)n2)->country)) return -1;
if (tolower(*((struct Series*)n1)->country) < tolower(*((struct Series*)n2)->country)) return 1;
}
else
{
if (tolower(*((struct Series*)n1)->country) > tolower(*((struct Series*)n2)->country)) return 1;
if (tolower(*((struct Series*)n1)->country) < tolower(*((struct Series*)n2)->country)) return -1;
}
}
int callbackrating(const void * n1, const void * n2)
{
if (direction == down)
{
if (((struct Series*)n1)->rating > ((struct Series*)n2)->rating) return -1;
if (((struct Series*)n1)->rating < ((struct Series*)n2)->rating) return 1;
}
else
{
if (((struct Series*)n1)->rating > ((struct Series*)n2)->rating) return 1;
if (((struct Series*)n1)->rating < ((struct Series*)n2)->rating) return -1;
}
}
void print(Series s[],int num,string field)
{
cout<<field<<endl;
int i;
for(i=0;i<num;i++) cout<<s[i].name<<" "<<s[i].genre<<" "<<s[i].country<<" "<<s[i].rating<<endl;
cout<<endl;
}
int main(void)
{
direction=up;
qsort (s, sizeof(s)/sizeof(*s), sizeof(*s), callbackname);
print(s,5,"Sort by Name");
qsort (s, sizeof(s)/sizeof(*s), sizeof(*s), callbackgenre);
print(s,5,"Sort by Genre");
//qsort (s, sizeof(s)/sizeof(*s), sizeof(*s), callbackcountry); // no need they are all "US"
// print(s,5,"country");
qsort (s, sizeof(s)/sizeof(*s), sizeof(*s), callbackrating);
print(s,5,"Sort by Rating");
cout <<" "<<endl;
cout <<"Press return to end . . ."<<endl;
cin.get();
}
|