Korialstrasza
Gawd
- Joined
- Jan 21, 2004
- Messages
- 561
so, for school I have to write a program which uses a structure to store information on books like a library would have. My problem comes at the sorting part. I have to sort the books alphabetically by title, author, etc. I got it to work fine, but I have a sort function for each method, which the teacher isn't going to like. How can I compress it down to a single function (or two, one for ints (# of pages and rating of book) and one for strings (author names, titles, etc)?
Here's what I have now.
int index is the total number of books in the structure, btw.
the prototypes for the sorting functions:
switch case in main() for the user's choice on what to sort
all the sort functions
Thanks guys.
Here's what I have now.
int index is the total number of books in the structure, btw.
the prototypes for the sorting functions:
Code:
void sortName(int, struct BOOKS[]); // sort by name
void sortGenre(int, struct BOOKS[]); // sort by genre (category)
void sortAuthor(int, struct BOOKS[]); // sort by author
void sortRating(int, struct BOOKS[]); // sort by rating
void sortPages(int, struct BOOKS[]); // sort by pages
Code:
// structure for holding book information
struct BOOKS
{
char name[50];
char author[50];
char category [50];
int rating;
int pages;
};
switch case in main() for the user's choice on what to sort
Code:
switch (choice)
{
case 1: // sort by name
sortName(index,info);
break;
case 2: // sort by author
sortAuthor(index,info);
break;
case 3: // sort by genre
sortGenre(index,info);
break;
case 4: // sort by rating
sortRating(index,info);
break;
case 5: // sort by pages
sortPages(index,info);
break;
}
all the sort functions
Code:
void sortGenre(int index, struct BOOKS info[])
{
struct BOOKS temp;
int x = 0, y = 0;
for(x = 0; x < index; x++)
for(y = 0; y < index-1; y++)
if(strcmp(info[y].category, info[y+1].category) > 0)
{
temp = info[y];
info[y] = info[y+1];
info[y+1] = temp;
}
printf("\n\nSORTED!\n\n");
}
void sortName(int index, struct BOOKS info[])
{
struct BOOKS temp;
int x = 0, y = 0;
for(x = 0; x < index; x++)
for(y = 0; y < index-1; y++)
if(strcmp(info[y].name, info[y+1].name) > 0)
{
temp = info[y];
info[y] = info[y+1];
info[y+1] = temp;
}
printf("\n\nSORTED!\n\n");
}
void sortAuthor(int index, struct BOOKS info[])
{
struct BOOKS temp;
int x = 0, y = 0;
for(x = 0; x < index; x++)
for(y = 0; y < index-1; y++)
if(strcmp(info[y].author, info[y+1].author) > 0)
{
temp = info[y];
info[y] = info[y+1];
info[y+1] = temp;
}
printf("\n\nSORTED!\n\n");
}
void sortRating(int index, struct BOOKS info[])
{
struct BOOKS rating;
int x = 0, y = 0;
for(x = 0; x < index; x++)
for(y = 0; y < index-1; y++)
if(info[y].rating > info[y+1].rating)
{
rating = info[y];
info[y] = info[y+1];
info[y+1] = rating;
}
printf("\n\nSORTED!\n\n");
}
void sortPages(int index, struct BOOKS info[])
{
struct BOOKS pages;
int x = 0, y = 0;
for(x = 0; x < index; x++)
for(y = 0; y < index-1; y++)
if(info[y].pages > info[y+1].pages)
{
pages = info[y];
info[y] = info[y+1];
info[y+1] = pages;
}
printf("\n\nSORTED!\n\n");
}
Thanks guys.