120 lines
4.1 KiB
C
120 lines
4.1 KiB
C
// header file
|
|
#include "Console_IO_Utility.h"
|
|
#include "StateUtilities.h"
|
|
|
|
// local constants for program
|
|
const int MAX_NUM_STATES = 50;
|
|
|
|
int main()
|
|
{
|
|
// initialize function/program
|
|
|
|
// initialize variables
|
|
int wkgNumStates, sortingNumStates;
|
|
int capacity = MAX_NUM_STATES;
|
|
char fileName[ MIN_STR_LEN ];
|
|
StateDataType *wkgArray, *sortingArray;
|
|
StateDataType resultData, searchData, removeData;
|
|
|
|
// show title
|
|
// function: showTitle
|
|
showTitle();
|
|
|
|
// create input/working and sorting arrays
|
|
wkgArray = createStateArray( MAX_NUM_STATES );
|
|
sortingArray = createStateArray( MAX_NUM_STATES );
|
|
|
|
// set empty data - data items will only have the state name
|
|
setEmptyElementData( &searchData );
|
|
strcpy( searchData.name, "North Dakota" );
|
|
|
|
setEmptyElementData( &removeData );
|
|
strcpy( removeData.name, "Wyoming" );
|
|
|
|
// get input
|
|
|
|
// get file name
|
|
// function: getFileName
|
|
promptForString( "Enter file name: ", fileName );
|
|
|
|
// upload state data
|
|
// function: uploadstateData
|
|
wkgNumStates = uploadStateData( wkgArray, capacity, fileName );
|
|
|
|
if( wkgNumStates > 0 )
|
|
{
|
|
// make copy of working array for later sorting use
|
|
sortingNumStates = wkgNumStates;
|
|
copyArray( sortingArray, wkgArray, sortingNumStates );
|
|
|
|
// process and display data
|
|
|
|
// display original list
|
|
displayData( wkgArray, wkgNumStates, "Original data after upload" );
|
|
|
|
// find state
|
|
printf( "\nFinding state: %s\n", searchData.name );
|
|
findStateByName( &resultData, wkgArray, wkgNumStates, searchData );
|
|
printf( "State found: %s, %d, %5.2f%%, %c\n", resultData.name,
|
|
resultData.population,
|
|
resultData.growth * 100.00,
|
|
resultData.majority );
|
|
|
|
// remove state
|
|
printf( "\nRemoving state: %s\n", removeData.name );
|
|
removeStateByName( &resultData, wkgArray, &wkgNumStates, removeData );
|
|
|
|
printf( "State removed: %s, %d, %5.2f%%, %c\n", resultData.name,
|
|
resultData.population,
|
|
resultData.growth * 100.00,
|
|
resultData.majority );
|
|
|
|
// display original list
|
|
displayData( wkgArray, wkgNumStates,
|
|
"Original data after state removal" );
|
|
|
|
// bubble sort
|
|
// function: runBubbleSort
|
|
runBubbleSortByName( sortingArray, sortingNumStates );
|
|
|
|
// display bubble-sorted list
|
|
displayData( sortingArray, sortingNumStates,
|
|
"Full list sorted by name using bubble sort" );
|
|
|
|
// insertion sort
|
|
// function: runInsertionSort
|
|
runInsertionSortByPopulation( sortingArray, sortingNumStates );
|
|
|
|
// display insertion-sorted list
|
|
displayData( sortingArray, sortingNumStates,
|
|
"Full list sorted by population using insertion sort" );
|
|
|
|
// selection sort
|
|
// function: runSelectionSort
|
|
runSelectionSortByGrowth( sortingArray, sortingNumStates );
|
|
|
|
// display insertion-sorted list
|
|
displayData( sortingArray, sortingNumStates,
|
|
"Full list sorted by growth using selection sort" );
|
|
}
|
|
|
|
else
|
|
{
|
|
printf( "\nData upload failed - program aborted\n" );
|
|
}
|
|
|
|
// end program
|
|
|
|
// report end program
|
|
// function: printf
|
|
printf( "\nProgram End\n" );
|
|
|
|
// clear dynamic array
|
|
wkgArray = clearStateArray( wkgArray );
|
|
sortingArray = clearStateArray( sortingArray );
|
|
|
|
// return program success
|
|
return 0;
|
|
}
|
|
|
|
|