All pastes #24776 Raw Edit

Unnamed

public text v1 · immutable
#24776 ·published 2005-10-07 00:15 UTC
rendered paste body
#include "wrapper.h"
#include "sminst.h"

//-------------------------------------------------------------------

void Display_Country_Data (preserves Country_Record& data)
// pre: true
// post: displays country_name and population_in_2001 data members on one line
//       of the console, surrounded by parens and separated
//       by a comma
{
	std::cout << "("
		<< data->country_name << ", "
		<< data->population_in_2001 
		<< ")"
		<< std::endl;
}	// Display_Country_Data

//-------------------------------------------------------------------

void Do_Load (produces Record_Queue& q1)
// pre: true
// post: q1 holds all country data found in file entered in by user
//       if the filename entered exists
{
	object File data;
	object Boolean successful;
	object Text data_filename;
	object Integer i, size;
	object Country_Record country_data;

	// Get filename from user and open
	std::cout << "Enter filename: ";
	std::cin >> data_filename;
	data.Open_File (data_filename, File::read, successful);

	if (successful) {
		q1.Clear ();
		// Read data from file
		data.Read (size, successful);
		for (i = 0; i < size; i++) {
			data.Read (country_data->country_name, successful);
			data.Read (country_data->population_in_2001, successful);
			q1.Enqueue (country_data);
		}	// end for
	} else {
		std::cout << "Error: " << data_filename << " not found" << std::endl;
	} // end if
}	// Do_Load

//-------------------------------------------------------------------

void Do_Display (preserves Record_Queue& q)
// pre: true
// post: data in s displayed on console, 22 records at at time
{
	object Integer p, cnt, z;
	object Text x;
	object Country_Record r;

	z = q.Size ();
	cnt = 1;
	for (p = 0; p < z; p++) {
		q.Dequeue (r);
		std::cout << "Record #" << cnt << " ";
		Display_Country_Data (r);
		q.Enqueue (r);

		// Display 22 records at a time
		if ((cnt % 22) == 0) {
			std::cout << "Hit Enter To Continue";
			std::cin >> x;
		} // end if
		cnt++;
	}	// end for
}	// Do_Display

//-------------------------------------------------------------------

void Do_Sort (preserves Record_Queue& q)
// pre: true
// post: q = permutation(#q)  and  q is in sorted order
{
	object Integer p, z;
	object Record_Sorter sorter;
	object Country_Record r;

	// Move data from queue object to sorting machine object
	z = q.Size ();
	for (p = 0; p < z; p++) {
		q.Dequeue (r);
		sorter.Insert (r);
	}	// end for
	
	sorter.Change_To_Extraction_Phase ();

	// Move sorted data back into the queue object
	z = sorter.Size ();
	for (p = 0; p < z; p++) {
		sorter.Extract (r);
		q.Enqueue (r);
	}	// end for
}	// Do_Sort

//-------------------------------------------------------------------

void main (void)
{
	// data_set is used to hold all the data
	object Record_Queue data_set;

	Do_Load (data_set);
	
	std::cout << std::endl << "Unsorted country data:" << std::endl;
	Do_Display (data_set);

	Do_Sort (data_set);

	std::cout << std::endl << "Country data sorted by name descending:" << std::endl;
	Do_Display (data_set);
} // main