#include #include #define DEBUG #define MAX 256 FILE *inp; char buf[ MAX ]; int n, pNum = 1; int oneCase() { int i, oldi,l; float ox,oy; /* char buchst[4]; */ fscanf( inp, "%s\n", buf ); if ( buf[0] == 'E' ) return 0; /* SOLVING: */ ox = 0; oy = 0; i = 0; while(1) { oldi = i; while(1) { if ( buf[i] == ',' ) break; if ( buf[i] == '.' ) break; i++; } sscanf( &buf[ oldi ], "%d", &l ); switch( buf[i-2] ) { case 'N': {if (buf[i-1] == 'W' ) { oy = oy + (float)l*sqrt(0.5); ox = ox - (float)l*sqrt(0.5); } else if (buf[i-1] == 'E') { oy = oy + (float)l*sqrt(0.5); ox = ox + (float)l*sqrt(0.5); } } break; case 'S': {if (buf[i-1] == 'W' ) { oy = oy - l*sqrt(0.5); ox = ox - l*sqrt(0.5); } else if (buf[i-1] == 'E') { oy = oy - (float) l *sqrt(0.5); ox = ox + (float) l*sqrt(0.5); } } } if ( buf[i-2] != 'N' && buf[i-2] !='S' ) switch( buf[i-1] ) { case 'N': oy = oy + l; break; case 'S': oy = oy - l; break; case 'W': ox = ox - l; break; case 'E': ox = ox + l; break; } if ( buf[i] == '.' ) break; i++; } printf("Map #%d\n",pNum); printf("The treasure is located at (%.3f,%.3f).\n",ox,oy); printf("The distance to the treasure is %.3f.\n",(sqrt (oy*oy + (ox * ox)))); return 1; } int main() { inp = fopen( "treasure.in", "r" ); /* for ( i=0; i < MAX; i++ ) buf[0]; */ while( oneCase() ) { printf("\n"); pNum++; } fclose( inp ); return 0; }