/** * Assignment #9 * Program to sort hockey statistics * Author: Mark Allen Weiss * SSN: 000-00-0000 * Course: COP-2210 Section 9 * Date: December 1, 1998 * * CERTIFICATION OF SOLE AUTHORSHIP * I certify that this work is solely my own and * that none if it is the work of any other person. * I further certify that I have not provided any * assistance to other students enrolled in COP-2210 that * would render their CERTIFICATION OF SOLE AUTHORSHIP * invalid. I understand that violations of this pledge * may result in severe sanctions. * * * * ----------------------------------- * (Mark Allen Weiss) <--- Put your name here, and sign above */ #include #include #include #include "apstring.h" #include "apvector.h" struct Player { apstring firstName; apstring lastName; int goals; int assists; int points; }; void openInput( apstring & infile, ifstream & fin ); void openOutput( apstring infile, apstring outfile, ofstream & fout ); void processFile( istream & fin, ostream & fout ); void sort( apvector & a ); bool isOrdered( const Player & p1, const Player & p2 ); int main( ) { apstring infile; // Input file name apstring outfile; // Output file name ifstream fin; // Input file stream ofstream fout; // Output file stream openInput( infile, fin ); openOutput( infile, outfile, fout ); processFile( fin, fout ); return 0; } /** * Usual stuff; you should copy this. */ void openInput( apstring & infile, ifstream & fin ) { for( ; ; ) { cout << "Enter the input file name: "; cin >> infile; fin.open( infile.c_str( ), ios::in | ios::nocreate ); if( !fin ) { cout << "Bad file." << endl; fin.clear( ); } else break; } } /** * Usual stuff. This routine also checks to * make sure that outfile is different from infile. * To avoid this test, call it with infile equal to "". */ void openOutput( apstring infile, apstring outfile, ofstream & fout ) { for( ; ; ) { cout << "Enter the output file name: "; cin >> outfile; if( infile == outfile ) { cout << "Input and output files are identical!!" << endl; continue; } fout.open( outfile.c_str( ) ); if( !fout ) { cout << "Bad file." << endl; fout.clear( ); } else break; } } /** * Read from fin; write sorted output to fout. * Pre: fin and fout are open and ready for reading and writing, respectively. */ void processFile( istream & fin, ostream & fout ) { apvector array( 100 ); int itemsRead = 0; Player p; while( fin >> p.firstName >> p.lastName >> p.goals >> p.assists >> p.points ) { if( itemsRead == array.length( ) ) array.resize( array.length( ) * 2 ); array[ itemsRead ] = p; itemsRead++; } array.resize( itemsRead ); sort( array ); for( int i = 0; i < array.length( ); i++ ) fout << setw( 12 ) << array[ i ].firstName << " " << setw( 15 ) << array[ i ].lastName << " " << setw( 3 ) << array[ i ].goals << " " << setw( 3 ) << array[ i ].assists << " " << setw( 3 ) << array[ i ].points << endl; } /** * Comparison function for Player. */ bool isOrdered( const Player & p1, const Player & p2 ) { if( p1.points != p2.points ) return p1.points > p2.points; if( p1.goals != p2.goals ) return p1.goals > p2.goals; if( p1.lastName != p2.lastName ) return p1.lastName < p2.lastName; return p1.firstName < p2.firstName; } /** * Insertion sort (different than in text) */ void sort( apvector & a ) { for( int p = 1; p < a.length( ); p++ ) { Player tmp = a[ p ]; int j; for( j = p; j > 0 && isOrdered( tmp, a[ j - 1 ] ); j-- ) a[ j ] = a[ j - 1 ]; a[ j ] = tmp; } }