Directory:Derek Elder/Programs/BinaryFileStatesSorted

MyWikiBiz, Author Your Legacy — Thursday October 06, 2022
Jump to navigationJump to search
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstring>
using namespace std;

struct stateInfo
{
	char name[15];
	int population;
	int area;
	int year;
	int order;
	char capital[15];
	char abbrev[3];
	int zip;
};

const int STRUC_SIZE = sizeof(stateInfo);
const int MAX = 50;
typedef stateInfo stateList[MAX];

void LoadData(stateList, ifstream& Input, int& numStates);
//Purpose:To load data from the binary file.
//Precondition:None
//Postcondition:Array of structs contains data.
int Split(stateList list, int First, int Last);
//Purpose:Modulation for Quicksort.
//Precondition:Called in Quicksort.
//Postcondition:Data split for Quicksort.
void QuickSort(stateList, int First, int Last);
//Purpose:To sort data in increasing order based upon name.
//Precondition:Array of structs contains data.
//Postcondition:Array of structs sorted properly.
void SelectionSort(stateList, int numStates);
//Purpose:To sort data in decreasing order based upon population.
//Precondition:Array of structs contains data.
//Postcondition:Array of structs sorted properly.
void PrintStates(stateList, ofstream& Output, int numStates);
//Purpose:To print data to console/output file.
//Precondition:None
//Postcondition:Data printed to console/output file.

void main()
{
	ifstream Input;
	ofstream Output;
	stateList States;
	int numStates = 0;

	Input.open("States.dat", ::ios.binary);
	Output.open("Output.txt");
	LoadData(States, Input, numStates);

	Output<<endl<<"-------------------------SelectionSort-------------------------"<<endl;
	cout<<endl<<"-------------------------SelectionSort-------------------------"<<endl;
	SelectionSort(States, numStates);
	PrintStates(States, Output, numStates);

	cout<<endl<<"QuickSort data will now be displayed.";
	cin.get();

	Output<<endl<<"---------------------------QuickSort---------------------------"<<endl;
	cout<<endl<<"---------------------------QuickSort---------------------------"<<endl;
	QuickSort(States, 0, (numStates - 1));
	PrintStates(States, Output, numStates);

	Output.close();
	cout<<endl<<"Program terminated."<<endl;
}
void LoadData(stateList States, ifstream& Input, int& numStates)
{
	int i;
	string junk;
	stateInfo TempInfo;

	for(i = 0; i < MAX; i++)
	{
		Input.read((char *)&TempInfo,STRUC_SIZE);
		if(Input.eof())
			break;

		States[i] = TempInfo;
	}
	Input.close();
	numStates = i;
	cout<<numStates<<" states loaded."<<endl;
}
void PrintStates(stateList States, ofstream& Output, int numStates)
{
	cout<<"Name             Pop     Area  Year Order Capital  Abbrev Zip"<<endl;
	cout<<"---------------------------------------------------------------"<<endl;

	for(int i = 0; i < numStates; i++)
	{
		cout<<setw(14)<<States[i].name<<setw(10)<<States[i].population<<setw(7)<<States[i].area<<setw(5)<<States[i].year
		    <<setw(3)<<States[i].order<<setw(15)<<States[i].capital<<setw(3)<<States[i].abbrev<<setw(6)<<States[i].zip<<endl;
	}

	/*Output<<"Name             Pop     Area  Year Order Capital  Abbrev Zip"<<endl;
	Output<<"-----------------------------------------------------------------------------"<<endl;

	for(int i = 0; i < numStates; i++)
	{
		Output<<setw(14)<<States[i].name<<setw(10)<<States[i].population<<setw(7)<<States[i].area<<setw(5)<<States[i].year
		    <<setw(3)<<States[i].order<<setw(15)<<States[i].capital<<setw(3)<<States[i].abbrev<<setw(6)<<States[i].zip<<endl;
	}*/
}
int Split(stateList list, int First, int Last)
{
	stateInfo SplitVal;
	SplitVal = list[First];

	while(First < Last)
	{
		while(strcmp(SplitVal.name,list[Last].name) < 0 && First < Last)
			Last--;
		if(First < Last)
		{
			list[First] = list[Last];
			First++;
			while(strcmp(SplitVal.name,list[First].name) > 0 && First < Last)
				First++;
			if(First < Last)
			{
				list[Last] = list[First];
				Last--;
			}
		}
	}
	list[Last] = SplitVal;
	return First;
}
void QuickSort(stateList list, int First, int Last)
{
	int SplitPoint;

	if(First < Last)
	{
		SplitPoint = Split(list, First, Last);
		QuickSort(list, First, SplitPoint - 1);
		QuickSort(list, SplitPoint + 1, Last);
	}
}
void SelectionSort(stateList list, int numStates)
{
	int lowIndex;
	stateInfo temp;

	for(int i = 0; i < numStates - 1; i++)
	{
		lowIndex = i;
		for(int j = i + 1; j < numStates; j++)
			if(list[j].population > list[lowIndex].population)
				lowIndex = j;
		if(lowIndex != i)
		{
			temp = list[lowIndex];
			list[lowIndex] = list[i];
			list[i] = temp;
		}
	}
}