Category : Files from Magazines
Archive   : PTV2N2.ZIP
Filename : DEMO.CPP

 
Output of file : DEMO.CPP contained in archive : PTV2N2.ZIP
// ==========================================================
// Listing 3: demo.cpp
// C++ code for demonstration of Heap class.
// Copyright (C) 1991 by Nicholas Wilt. All rights reserved.
// ==========================================================
// Refer to the MAKEFILE listing (Listing 4) for dependencies
// ----------------------------------------------------------

#include
#include "heap.h"

// ------------------------------------------------------
// This is the comparison function for ASCENDING integers
// ------------------------------------------------------
int comp_ints(void *a, void *b)
{
return( *((int *) a) - *((int *) b));
}

// ---------------------------------------------------------
// This is the comparison function for DESCENDING characters
// ---------------------------------------------------------
int comp_char(void *a, void *b)
{
return( *((char *) b) - *((char *) a));
}

// ==============================================================
main()
{
// An unordered set of integers
int numbers[20] = {1, 2, 4, 5, 3,
19, 15, 18, 0, 13,
11, 14, 12, 16, 17,
6, 9, 8, 7, 10};

// An unordered set of characters
char letters[34] = {"THEQUICKBROWNFOXJUMPSOVERLAZYDOGS"};
// 123456789012345678901234567890123

Heap h(comp_ints); // A heap instance for the integers
Heap ch(comp_char); // A heap instance for the characters

int i;

// --------------------------------------------------------
// Start with the integer data...
// --------------------------------------------------------
// Print out numbers before sorting
for (i = 0; i < 20; i++)
cout << numbers[i] << ' ';
cout << '\n';

// Insert numbers into heap
for (i = 0; i < 20; i++)
h.Insert(numbers+i);

// Print out numbers in order extracted from heap
// (In this case, in ASCENDING order.)
for (i = 0; i < 20; i++)
cout << *((int *) h.Extract()) << ' ';
cout << '\n';

// --------------------------------------------------------------
// Now, try the character data...
// --------------------------------------------------------------

// Print out letters before sorting
for (i = 0; i < 33; i++)
cout << letters[i] << ' ';
cout << '\n';

// Insert letters into heap
for (i = 0; i < 33; i++)
ch.Insert(letters+i);

// Print out letters in order extracted from heap
// (In this case, in DESCENDING order.)
for (i = 0; i < 33; i++)
cout << *((char *) ch.Extract()) << ' ';
cout << '\n';

}


  3 Responses to “Category : Files from Magazines
Archive   : PTV2N2.ZIP
Filename : DEMO.CPP

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. 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/