1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
#include <iostream>
#include <cstring>
#include "potus.h"
#define FILENAME "presidents.dat"
#define CHARLEN 27
using namespace std;
void sortName( struct president * [], char * [], int ); // sorts the presidents by last name
void sortYear( struct president * [], int ); // sort the presidents by inauguration year
void sortState( struct president * [], int ); // sorts the president by home state
void swap( struct president ** , struct president ** ); // swaps the two things that are sent to it
int main()
{
struct president * potusList[50]; // array of pointers to structure
int numPotus; // number of potus structures extracted from file
char temp[27]; // temperorary variable
char * ptr; // pointer that points at tokens
char * lastName[numPotus]; // array of pointers that point at last name
int count; // count for loop
//call function to load structures
loadPotus( potusList, FILENAME );
numPotus = loadPotus( potusList, FILENAME );
// call functions to sort array
for( count = 0; count < numPotus; count++ )
{
strcpy( temp, potusList[count] -> name );
ptr = strtok( temp, " ." );
while( ptr )
{
ptr = strtok( NULL, " .\n\t" );
lastName[count] = ptr;
}
}
sortName( potusList, lastName, numPotus );
// pause program
cout << "Press ENTER to continue";
cin.get ( );
cin.ignore ( 1, '\n' );
for( count = 0; count < numPotus; count++ )
{
strcpy( temp, potusList[count] -> term );
ptr = strtok( temp, "- " );
if( ptr )
{
year[count] = ptr;
}
}
sortYear( potusList, year, numPotus );
// pause program
cout << "Press ENTER to continue";
cin.get ( );
cin.ignore ( 1, '\n' );
sortState( potusList, numPotus );
return 0;
}
/* sortName: sorts all the potus by last name and prints them out
* Parameters:
* potusList: array of pointers that point at potus structures
* numPotus: number of elements in array
* Returns: nothing
*/
void sortName( struct president * potus[], char * lastName[], int numPotus )
{
int count; // count for loop
int swapCtr; // counter for sort loop
int min = 0; // first name in list
// loop to sort
for( swapCtr = 0; swapCtr < numPotus; swapCtr ++ )
{
min = swapCtr;
for( count = min + 1; count < numPotus; count ++ )
{
if( lastName[count] < lastName[swapCtr] )
{
swap( & potus[swapCtr], & potus[swapCtr + 1] );
}
}
}
// loop to print sorted array
cout << "Sorted by Last Name:\n";
for( count = 0; count < numPotus; count++ )
{
cout << potus[count] -> name << "\n";
cout << potus[count] -> term << "\n";
cout << potus[count] -> state << "\n";
}
}
/* sortYear: sorts all the potus by inauguration year
* Parameters:
* potusList: array of pointers that point to potus structures
* numPotus: number of elements in array
* Returns: nothing
*/
void sortYear( struct president * potus[], char * year [], int numPotus )
{
int count; // count for loop
int swapCtr; // count for outer loop
int sorter; // count for inner loop
int min = 0; // first name in list
// loop to find last name and sort
for( count = 0; count < numPotus; count++ )
{
min = count;
for( swapCtr = count + 1; swapCtr < numPotus; swapCtr ++ )
{
if( year[swapCtr] < year[count] )
{
swap( & potus[count], & potus[swapCtr] );
}
}
}
// loop to print sorted array
cout << "Sorted by Inauguration Year:\n";
for( count = 0; count < numPotus; count++ )
{
cout << year[count] << "\n";
cout << potus[count] -> name << "\n";
cout << potus[count] -> term << "\n";
cout << potus[count] -> state << "\n";
}
}
/* sortState: sorts all the potus by home state
* Parameters:
* potusList: array of pointers that point to potus structures
* numPotus: number of elements in array
* Returns: nothing
*/
void sortState( struct president * potus[], int numPotus )
{
int count; // count for loop
int swapCtr; // count for swap loop
int min = 0; // first name in list
for( count = 0; count < numPotus; count++ )
{
min = count;
for( swapCtr = count + 1; swapCtr < numPotus; swapCtr ++ )
{
if( * potus[swapCtr] -> state < * potus[min] -> state )
{
swap( & potus[min], & potus[swapCtr] );
}
}
}
// loop to print sorted array
cout << "Sorted by Home State:\n";
for( count = 0; count < numPotus; count++ )
{
cout << potus[count] -> name << "\n";
cout << potus[count] -> term << "\n";
cout << potus[count] -> state << "\n";
}
}
/* swap: swaps the 2 pointers that are sent to the function
* Parameters:
* lesser: pointer that will be replaced
* greater: pointer that will take the place of another
* Returns: nothing
*/
void swap( struct president ** lesser, struct president ** greater )
{
struct president * temp = 0; // temp variable for structure pointer
// swap pointers
temp = * lesser;
* lesser = * greater;
* greater = temp;
}
|