#include #include #include #include #include char buffer[204]; void find_distance(int len, char* direction, float* x, float* y) { if (direction[0] == 'N') { if (direction[1] == 'E') { *x = *x + ((sqrt(2)/2)*len); *y = *y + ((sqrt(2)/2)*len); } else if (direction[1] == 'W') { *x = *x - ((sqrt(2)/2)*len); *y = *y + ((sqrt(2)/2)*len); } else *y = *y + len; } else if (direction[0] == 'S') { if (direction[1] == 'E') { *x = *x + ((sqrt(2)/2)*len); *y = *y - ((sqrt(2)/2)*len); } else if (direction[1] == 'W') { *x = *x - ((sqrt(2)/2)*len); *y = *y - ((sqrt(2)/2)*len); } else *y = *y - len; } else if (direction[0] == 'W') *x = *x - len; else if (direction[0] == 'E') *x = *x + len; } void read_map() { int diff=0; char direction[3], c; char numbuf[6]; int len = 0, unit = 1, index = 0; float x = 0, y = 0; sscanf(&(buffer[diff++]),"%c", &c); while (c != '.') { if (isdigit(c)) { int i=0; while (isdigit(c)) { numbuf[i++]=c; sscanf(&(buffer[diff++]),"%c", &c); } numbuf[i]=0; len=atoi(numbuf); unit = 1; while (!((c == ',') || (c == '.'))) { direction[index++] = c; sscanf(&(buffer[diff++]),"%c", &c); } direction[index] = '\0'; index = 0; } find_distance(len, direction, &x, &y); len = 0; if ( c == ',') { sscanf(&(buffer[diff++]),"%c", &c); } } printf("The treasure is located at (%.3f,%.3f).\n", x, y); printf("The distance to the treasure is %.3f.\n\n", sqrt(x*x + y*y)); } int main() { int map = 1; FILE* in = fopen("treasure.in","r"); fscanf(in,"%s\n", buffer); while (strncmp(buffer, "END", 3)) { printf("Map #%d\n", map++); read_map(); fscanf(in,"%s\n", buffer); } fclose(in); return 0; }