Category : Files from Magazines
Archive   : CGAZ2-4.ZIP
Filename : SORTTEST.C
Output of file : SORTTEST.C contained in archive : CGAZ2-4.ZIP
* Displays variety of sorting schemes using qsort() *
* Small Memory Model. (c) 1988 Billy Rubin. *
* May be used freely if copyright is stated. *
*******************************************************/
/* Verified for Datalt., DeSmet, Ecosoft, Lattice, MetaWare, MS, Turbo */
/* Portability Notes:
DeSmet, Ecosoft and Lattice could not properly handle the #elif
in compare(). Deleting the unused code worked in all cases except
Lattice which misprinted the floats in printf(); ints worked OK.
MetaWare has no qsort().
-- All these problems are violations of ANSI draft --
*/
#include
#define SELLERS 6
#define DEPT_ASC_SALES_DESC 1
struct rec { /* Seller's Record */
int dept;
char name [30];
float sales;
}
eom[SELLERS] = /* Sample Data */
{
106, "Brown, George", 138.25,
106, "Anderson, F.", 254.78,
106, "Carter, Paul", 300.00,
112, "Sartle, Amy", 1289.45,
101, "Virgil, Dominic", 455.20,
101, "Crane, Sallie", 399.40
};
int compare();
main()
{
int i;
qsort (&eom[0], SELLERS, sizeof (struct rec), compare);
for (i = 0; i < SELLERS; i++)
{
printf ("%d %-30s %8.2f\n",
eom[i].dept, eom[i].name, eom[i].sales);
}
}
int compare (p1, p2) /* Compare two elements */
struct rec *p1, *p2;
{
#if NAME_ASC /* By name, ascending */
return (strcmp (p1->name, p2->name));
#elif NAME_DESC /* By name, descending */
return (strcmp (p2->name, p1->name));
#elif DEPT_NAME_ASC /* By dept, the name */
if (p1->dept == p2->dept) /* both ascending */
return (strcmp (p1->name, p2->name));
else
return (p1->dept > p2->dept ? 1 : -1);
#elif DEPT_ASC_SALES_DESC /* Dept. ascending and */
if (p1->dept == p2->dept) /* sales descending. */
return (p1->sales < p2->sales ? 1: -1);
else
return (p1->dept > p2->dept ? 1 : -1);
#endif
}
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/