fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define diy 365.25
  5. #define R 6.371*1000000 /* radius of the Earth */
  6. #define pi 3.14159
  7.  
  8. double lat, lon, altitude, time; /* input lf */
  9. int junk, yy, mh, dd, hh, mn, ss; /* input d */
  10. int samples, duration, iyy, imh, idd, ihh, imn, iss; /* input data Stage 2 */
  11.  
  12. /* calculation data Stage 2 */
  13. int pyy, pmh, pdd, phh, pmn, pss;
  14. int pavinterval, avinterval, maxinterval, interval;
  15.  
  16. /* calculation data Stage 3 */
  17. double plat, plon;
  18. double total_sdist, sdist, a, hdist, total_hdist;
  19.  
  20. /* calculation data Stage 4 */
  21. double elapsed_time, uspeed, pudist;
  22. int update_time, uyy, umh, udd, uhh, umn, uss;
  23.  
  24. int
  25. main(int argc, char **argv){
  26.  
  27. /* !! skip first 6 lines !! */
  28.  
  29. /* read inital value */
  30. scanf("%lf,%lf,%d,%lf,%lf,%4d-%2d-%2d,%2d:%2d:%2d",
  31. &lat, &lon, &junk, &altitude, &time, &yy, &mh, &dd, &hh, &mn, &ss);
  32.  
  33. /* print Stage 1 */
  34. printf("Stage 1\n=======\n");
  35. printf("GPS trace commences : %04d-%02d-%02d, %02d:%02d:%02d\n\n",yy, mh, dd, hh, mn, ss);
  36.  
  37. lat *= (pi/180);
  38. lon *= (pi/180);
  39.  
  40. /* store inital values */
  41. iyy=yy;imh=mh;idd=dd;ihh=hh;imn=mn;iss=ss; /* starting time */
  42. pyy=yy;pmh=mh;pdd=dd;phh=hh;pmn=mn;pss=ss;plon=lon;plat=lat; /* previous line's data */
  43. pavinterval=0;total_sdist=0;total_hdist=0; /* distance and previous interval's to zero */
  44. uyy=yy;umh=mh;udd=dd;uhh=hh;umn=mn;uss=ss;pudist=0; /* initalising update itervals */
  45. samples = 1; /* read first sample */
  46.  
  47. printf("Stage 4\n=======\n");
  48.  
  49. /* scan rest of files */
  50. while(scanf("%lf,%lf,%d,%lf,%lf,%4d-%2d-%2d,%2d:%2d:%2d", &lat, &lon, &junk, &altitude, &time, &yy, &mh, &dd, &hh, &mn, &ss)==11){
  51.  
  52. lat *= (pi/180);
  53. lon *= (pi/180);
  54.  
  55. /* calculate interval */
  56. interval = ((((((yy-pyy)*diy + (dd-pdd))*24 + (hh-phh))*60 + (mn-pmn))*60 + (ss-pss)));
  57.  
  58. if (interval > maxinterval){
  59. maxinterval = interval;
  60. }
  61.  
  62. /* !! simple distance formula !! */
  63. sdist = R*sqrt(((lon-plon)*cos((plat+lat)/2))*((lon-plon)*cos((plat+lat)/2)) + (plat-lat)*(plat-lat));
  64. total_sdist += sdist;
  65.  
  66. /* !! haversine distance formula !! */
  67. a = sin((lat-plat)/2)*sin((lat-plat)/2) + cos(lat)*cos(plat)*sin((lon-plon)/2)*sin((lon-plon)/2);
  68. hdist = 2*R*atan2(sqrt(a),sqrt(1-a));
  69. total_hdist += hdist;
  70.  
  71. /* speed update */
  72. /* !! */ update_time = ((((((yy-uyy)*diy + (dd-udd))*24 + (hh-uhh))*60 + (mn-umn))*60 + (ss-uss)));
  73. if (update_time>5*60){
  74. uspeed=((total_hdist-pudist)/(update_time))*3.6;
  75. printf("Period ending %04d-%02d-%02d, %02d:%02d:%02d, average %5.1lf km/hr \n", yy, mh, dd, hh, mn, ss, uspeed);
  76. uyy=yy;umh=mh;udd=dd;uhh=hh;umn=mn;uss=ss;pudist=total_hdist;
  77. }
  78. samples++;
  79. pyy=yy;pmh=mh;pdd=dd;phh=hh;pmn=mn;pss=ss;plon=lon;plat=lat;pavinterval=avinterval;
  80.  
  81. }
  82.  
  83. /* calculate duration of data */
  84. duration = ((((((yy-iyy)*diy + (dd-idd))*24 + (hh-ihh))*60 + (mn-imn))*60 + (ss-iss)));
  85. avinterval = duration/(samples-1);
  86.  
  87. /* print Stage 2 */
  88. printf("\nStage 2\n=======\n");
  89. printf("GPS trace terminates : %04d-%02d-%02d, %02d:%02d:%02d\n",yy, mh, dd, hh, mn, ss);
  90. printf("GPS trace samples : %20d\n",samples);
  91. printf("GPS trace duration : %20d seconds (%.1lf hours)\n",duration,duration/(60.0*60.0));
  92. printf("Average interval between samples : %20d seconds\n", avinterval);
  93. printf("Maximum interval between samples : %20d seconds\n", maxinterval);
  94.  
  95. /* print Stage 3 */
  96. printf("\nStage 3a\n=======\n");
  97. printf("Total distance travelled using simple rule : %5d metres \n",(int)total_sdist);
  98. printf("Average speed over perios of trace : %3.2lf m/sec \n",total_sdist/duration);
  99. printf("\nStage 3b\n=======\n");
  100. printf("Total distance travelled using haversine rule : %5d metres \n",(int)total_hdist);
  101. printf("Average speed over period of trace : %3.2lf m/sec \n",total_sdist/duration);
  102. printf("Difference between simple and haversine : %7.1e metres \n",total_sdist-total_hdist);
  103.  
  104.  
  105. }
Success #stdin #stdout 0s 5284KB
stdin
39.847111,116.472481,0,174,39727.9927083333,2008-10-06,23:49:30
39.866378,116.444516,0,184,39727.9933449074,2008-10-06,23:50:25
39.870523,116.415378,0,266,39727.9939930556,2008-10-06,23:51:21
39.86818,116.396858,0,200,39727.9946296296,2008-10-06,23:52:16
39.866261,116.384228,0,125,39727.9952777778,2008-10-06,23:53:12
39.864121,116.374793,0,98,39727.9959837963,2008-10-06,23:54:13
39.863854,116.367971,0,187,39728.0007523148,2008-10-07,00:01:05
39.866378,116.368195,0,148,39728.0018981481,2008-10-07,00:02:44
39.867626,116.368216,0,135,39728.0044097222,2008-10-07,00:06:21
39.867648,116.368211,0,131,39728.0050462963,2008-10-07,00:07:16
39.867641,116.36821,0,131,39728.0056944444,2008-10-07,00:08:12
39.870371,116.368203,0,125,39728.0072569444,2008-10-07,00:10:27
39.874991,116.368369,0,187,39728.0079050926,2008-10-07,00:11:23
39.876726,116.368485,0,433,39728.0085416667,2008-10-07,00:12:18
39.879958,116.368583,0,269,39728.0091898148,2008-10-07,00:13:14
39.880881,116.368534,0,318,39728.0105092593,2008-10-07,00:15:08
39.881533,116.368533,0,226,39728.0117361111,2008-10-07,00:16:54
39.885054,116.368395,0,256,39728.0130324074,2008-10-07,00:18:46
39.886601,116.368486,0,217,39728.0142476852,2008-10-07,00:20:31
39.890761,116.368408,0,207,39728.0159953704,2008-10-07,00:23:02
39.891753,116.368593,0,272,39728.0166319444,2008-10-07,00:23:57
39.893464,116.368291,0,276,39728.0178935185,2008-10-07,00:25:46
39.894446,116.368423,0,217,39728.0191435185,2008-10-07,00:27:34
39.897178,116.368211,0,230,39728.0197916667,2008-10-07,00:28:30
39.942715,116.346376,0,440,39728.0375810185,2008-10-07,00:54:07
39.946863,116.3452,0,79,39728.0382291667,2008-10-07,00:55:03
39.955409,116.342323,0,69,39728.0388657407,2008-10-07,00:55:58
39.963874,116.33907,0,20,39728.0395138889,2008-10-07,00:56:54
39.977011,116.333673,0,335,39728.0420138889,2008-10-07,01:00:30
39.987119,116.331833,0,184,39728.042662037,2008-10-07,01:01:26
39.991441,116.331208,0,184,39728.0444444444,2008-10-07,01:04:00
39.991436,116.330311,0,207,39728.0450810185,2008-10-07,01:04:55
39.991351,116.329415,0,233,39728.0457291667,2008-10-07,01:05:51
39.991206,116.328511,0,289,39728.0463657407,2008-10-07,01:06:46
39.991123,116.327638,0,318,39728.0470138889,2008-10-07,01:07:42
stdout
Stage 1
=======
GPS trace commences                           : 2008-10-06, 23:49:30

Stage 4
=======
Period ending 2008-10-07, 00:01:05, average  51.1 km/hr 
Period ending 2008-10-07, 00:06:21, average   4.8 km/hr 
Period ending 2008-10-07, 00:11:23, average   9.8 km/hr 
Period ending 2008-10-07, 00:16:54, average   7.9 km/hr 
Period ending 2008-10-07, 00:23:02, average  10.0 km/hr 
Period ending 2008-10-07, 00:28:30, average   7.9 km/hr 
Period ending 2008-10-07, 00:54:07, average  12.6 km/hr 
Period ending 2008-10-07, 01:00:30, average  37.3 km/hr 
Period ending 2008-10-07, 01:05:51, average  19.9 km/hr 

Stage 2
=======
GPS trace terminates                          : 2008-10-07, 01:07:42
GPS trace samples                             :                   35
GPS trace duration                            :                 4692 seconds (1.3 hours)
Average interval between samples              :                  138 seconds
Maximum interval between samples              :                 1537 seconds

Stage 3a
=======
Total distance travelled using simple rule    : 24859 metres 
Average speed over perios of trace            : 5.30 m/sec 

Stage 3b
=======
Total distance travelled using haversine rule : 24859 metres 
Average speed over period of trace            : 5.30 m/sec 
Difference between simple and haversine       : 1.1e-04 metres