// File: C_ACM_input_ex.c // Date: 10/22/2001 // Sample program showing how to use the routines in input.c // Assume the input file is "input.in" and has the following format. // // There are a number of cases, beginning with a number defining how // many stations are part of that case. Each station line has a string // label, a letter, integer x & y coordinates, and a float distance. One // of the stations also has a trailing 1, denoting that it is the // start station for this case. // // 4 // Station1 A 12 34 2.4 // Station2 B 43 56 2.1 // Station3 C 65 4 2.3 1 // Station4 D 8 23 1.0 // 0 #include "input.c" #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #define LINE_LEN 100 int main( int argc, char *argv[] ) { char line[LINE_LEN+1]; char stationName[20]; int stations, x, y; int startStation = 0; char stationLetter; float dist; FILE *fp = fopen( "input.in", "r" ); if( fp == NULL ) { fprintf( stderr, "Could not open input file.\n" ); exit( -1 ); } printf( "Testing macros...\n" ); printf( " abs( -100 ): %d\n", abs( -100 ) ); printf( " min( 50.1, 100 ): %.1f\n", min( 50.1, 100 ) ); printf( " max( 50.1, 50.2 ): %.1f\n", max( 50.1, 50.2 ) ); printf( " deg( %f ): %.1f\n", M_PI, deg( M_PI ) ); printf( " rad( 180.0 ): %8.6f\n", rad( 180.0 ) ); printf( " Dist2d (should be 5): %d\n", ( int )dist2d( 0.0, 0.0, 4.0, 3.0 ) ); printf( " Dist3d (should be 5): %d\n", ( int )dist3d( 0.0, 0.0, 0.0, 4.0, 3.0, 0.0 ) ); printf( " bit_set( 3, 0 ) (should be 1): %d\n", ( int )bit_set( 3, 0 ) ); printf( " bit_set( 2, 1 ) (should be 1): %d\n", ( int )bit_set( 2, 1 ) ); printf( " bit_set( 54, 0 ) (should be 0): %d\n", ( int )bit_set( 54, 0 ) ); printf( " bit_set( set_bit( 0, 4 ), 4 ) (should be 1): %d\n", bit_set( set_bit( 0, 4 ), 4 ) ); printf( " bit_set( clear_bit( 0, 4 ), 4 ) (should be 0): %d\n", bit_set( clear_bit( 0, 4 ), 4 ) ); printf( " bit_set( toggle_bit( 6, 2 ), 2 ) (should be 0): %d\n\n", bit_set( toggle_bit( 6, 2 ), 2 ) ); printf( "Parsing input file...\n" ); getLine( fp, line, LINE_LEN ); getInt( stations ); for( int i = 0; i < stations; i++ ) { if( stations == 0 ) { printf( "Done with all cases.\n" ); break; } getLine( fp, line, LINE_LEN ); getStr( stationName ); getChar( stationLetter ); getInt( x ); getInt( y ); getFloat( dist ); printf( "Case %d: %s %c %d %d %.1f", i, stationName, stationLetter, x, y, dist ); if( getInt( startStation ) ) { printf( " (Start)" ); } printf( "\n" ); } }