#include #include #include #include #include int main(void) { FILE *in=fopen("treasure.in","r"); char map[201],direction[3],digit[5]; int i=0,count=0,k=0,j=0,step=0,f,m; double x=0.0,y=0.0; /* fscanf(in,map);*/ fgets(map,201,in); while(!feof(in) && strcmp(map,"END")!=0){ if(!strcmp(map,"END\n")) break; x=0.0; y=0.0; step=0; ++count; i=0; while(map[i]!='.'){ step=0; f=0; for(m=0;m<5;m++) digit[m]='\0'; while(isdigit(map[i])){ digit[f]=map[i]; j++;i++;f++; } step=atoi(digit); k=0; while(map[i]!=',' && map[i]!='.'){ direction[k]=map[i]; k++; i++; } direction[k]='\0'; /*printf("step %d direct %s\n",step,direction);*/ if(!strcmp(direction,"N")) y += step; else if(!strcmp(direction,"NE")){ x += (sqrt(0.5)*step); y += (sqrt(0.5)*step) ; } else if(!strcmp(direction,"E")) x += step; else if(!strcmp(direction,"SE")){ x += (sqrt(0.5)*step); y -= (sqrt(0.5)*step); } else if(!strcmp(direction,"S")) y -= step; else if(!strcmp(direction,"SW")){ x -= (sqrt(0.5)*step); y -= (sqrt(0.5)*step); } else if(!strcmp(direction,"W")) x -= step; else if(!strcmp(direction,"NW")){ x -= (sqrt(0.5)*step); y += (sqrt(0.5)*step); } if(map[i]!='.') ++i; /* printf("x %f y %f\n",x,y);*/ } printf("Map #%d\n",count); 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)); /*fscanf(in,map);*/ /* if(!feof(in))*/ fgets(map,201,in); /*else break;*/ } fclose(in); return 0; }