|
|
back to boardWhy does my program get WA? I can't find where error is. #include <math.h> #include <stdio.h> #include <string.h> #define DIAMETER 6875.0 #define CIRCLE_PI 3.1415926536 typedef struct { int sign; int deg; int min; int sec; } degree; typedef struct { degree latitude; degree longitude; } coordinate; typedef struct { double x; double y; double z; } vector; coordinate ship; coordinate iceberg; double dist; void str_to_coord(degree *ptr,char *p,char positive) { while(*p<'0'||*p>'9') p++; ptr->deg=0; while(*p>='0'&&*p<='9') ptr->deg=ptr->deg*10+(*(p++)-'0'); while(*p<'0'||*p>'9') p++; ptr->min=0; while(*p>='0'&&*p<='9') ptr->min=ptr->min*10+(*(p++)-'0'); while(*p<'0'||*p>'9') p++; ptr->sec=0; while(*p>='0'&&*p<='9') ptr->sec=ptr->sec*10+(*(p++)-'0'); while(*p<'A'||*p>'Z') p++; ptr->sign=(*p==positive?1:-1); } void init() { char buf[80]; gets(buf); gets(buf); gets(buf); gets(buf); str_to_coord(&(ship.latitude),buf,'N'); gets(buf); str_to_coord(&(ship.longitude),buf,'E'); gets(buf); gets(buf); str_to_coord(&(iceberg.latitude),buf,'N'); gets(buf); str_to_coord(&(iceberg.longitude),buf,'E'); } double deg_to_rad(degree angle) { double res; res=(double)angle.deg+(double)angle.min/60.0+(double) angle.sec/3600.0; res=res/180.0*CIRCLE_PI; if(angle.sign==-1) res=-res; return res; } void coord_to_vec(vector *vec,coordinate coord) { double phi,theta; phi=deg_to_rad(coord.latitude); theta=deg_to_rad(coord.longitude); vec->x=cos(phi)*cos(theta)*DIAMETER/2.0; vec->y=cos(phi)*sin(theta)*DIAMETER/2.0; vec->z=sin(phi)*DIAMETER/2.0; } double distance(vector a,vector b) { double t1=a.x-b.x; double t2=a.y-b.y; double t3=a.z-b.z; t1=t1*t1; t2=t2*t2; t3=t3*t3; return sqrt(t1+t2+t3); } void solve() { vector a,b; double t; coord_to_vec(&a,ship); coord_to_vec(&b,iceberg); t=distance(a,b); dist=acos(1.0-(t*t)/(2.0*(DIAMETER/2.0)*(DIAMETER/2.0)))* (DIAMETER/2.0); } void done() { printf("The distance to the iceberg: %0.2lf miles.\n",dist); if(floor(dist*100.0+0.5)<10000.0) printf("DANGER!\n"); } main() { init(); solve(); done(); return 0; } Re: I think the problem is with this; if(floor(dist*100.0+0.5)<10000.0) printf("DANGER!\n"); i can make a test in which the answer is much nearer to 100 than you expect; one way to correct this: #include <string.h> ... void done() { if ( printf("The distance to the iceberg: %0.2lf miles.\n",dist) == strlen("The distance to the iceberg: 100.00 miles.\n") ) printf("DANGER!\n"); } Good luck. Re: sorry, a mistake -- not == but < |
|
|