home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-15 | 2.8 KB | 113 lines | [TEXT/CWIE] |
- #include "Solution.h"
-
- #include "ProblemUtils.h"
-
- #include <stdio.h>
- #include <string.h>
- #include <Files.h>
- #include <Errors.h>
-
- static OSErr WriteRouteToHandle( Handle output, UInt32ArrayHandle route )
- {
- OSErr err;
- char node[20];
- long i;
- UInt32 *p;
- UInt32 count;
-
- if ( route ) {
- HLock( (Handle)route );
- count = GetHandleSize((Handle)route) / sizeof(UInt32);
- p = *route;
- for ( i = 1; i <= count; i++ ) {
- sprintf( node, "%ld", p[i-1] );
- err = ProblemWriteStringToHandle( output, node );
- if ( err != noErr ) break;
- if ( i < count ) {
- err = ProblemWriteStringToHandle( output, "," );
- if ( err != noErr ) break;
- }
- }
- HUnlock( (Handle)route );
- if ( err != noErr ) return err;
- err = ProblemWriteLineToHandle( output, "" );
- } else {
- err = ProblemWriteLineToHandle( output, "No Route" );
- }
- return err;
- }
-
- pascal OSErr CheckGraph( const FSSpec* infile, const FSSpec* outfile )
- {
- OSErr err;
- Handle data;
- Handle result;
- char line[MAX_LINE_LEN];
- char *linep;
- char command[MAX_LINE_LEN];
- UInt32 node_count;
- Handle graph;
- UInt32ArrayHandle route;
- UInt32 vertex1, vertex2;
-
- result = NewHandle( 0 );
- err = MemError();
- ProblemLogError( err, "CheckGraph: NewHandle: result" );
-
- err = ProblemFileRead( infile, &data );
- ProblemLogError( err, "CheckGraph: ProblemFileRead" );
- if ( err == noErr ) {
- linep = line;
- if ( !ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) || !ProblemGetUInt32( &linep, &node_count ) ) {
- err = -1;
- ProblemLogError( err, "CheckGraph: node_count" );
- }
- }
- if ( err == noErr ) {
- GraphInit( &graph, node_count );
-
- while ( (err == noErr) && ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) ) {
- linep = line;
- ProblemGetCString( &linep, command, MAX_LINE_LEN );
- if ( strcmp( command, "ADD" ) == 0 ) {
- err = ProblemGetUInt32( &linep, &vertex1 ) ? noErr : -1;
- ProblemLogError( err, "CheckGraph: vertex1" );
- if ( err != noErr ) break;
- err = ProblemGetUInt32( &linep, &vertex2 ) ? noErr : -1;
- ProblemLogError( err, "CheckGraph: vertex2" );
- if ( err != noErr ) break;
- GraphAddDirectedEdge( graph, vertex1, vertex2 );
- } else if ( strcmp( command, "FIND" ) == 0 ) {
- err = ProblemGetUInt32( &linep, &vertex1 ) ? noErr : -1;
- ProblemLogError( err, "CheckGraph: vertex1" );
- if ( err != noErr ) break;
- err = ProblemGetUInt32( &linep, &vertex2 ) ? noErr : -1;
- ProblemLogError( err, "CheckGraph: vertex2" );
- if ( err != noErr ) break;
- GraphFindRoute( graph, vertex1, vertex2, &route );
- err = WriteRouteToHandle( result, route );
- DisposeHandle( (Handle)route );
- }
- if ( err != noErr ) break;
- }
-
- DisposeHandle( graph );
- }
-
- err = ProblemFileWrite( outfile, result );
-
- DisposeHandle( result );
- DisposeHandle( data );
- return err;
- }
-
- int main()
- {
- printf( "Starting\n" );
-
- ProblemRunFileTests( CheckGraph );
-
- return 0;
- }
-
-