qq_36599218
humanint
2016-11-03 04:18
采纳率: 66.7%
浏览 1.1k
已采纳

c primer plus 第六版第5章程序清单5.16的问题

我使用visual studio2015 community编写了c primer plus 第六版第5章程序清单5.16。
#include
const int S_PER_M = 60;
const int S_PER_H = 3600;
const double M_PER_K = 62137;
int main(void)
{
double distk, distm;
double rate;
int min, sec;
int time;
double mtime;
int mmin, msec;

printf("this program converts your time for a metric race\n");
printf("to a time for running a mile and to your average\n");
printf("speed in miles per hour.\n");
printf("please enter,in kiometers,the distance run.\n");
scanf("%lf", &distk);
printf("next enter the time in minutes and seconds.\n");
printf("begin by entering the minutes.\n");
scanf("%d", &min);
printf("now enter the seconds.\n");
scanf("%d", &sec);

time = S_PER_M*min + sec;
distm = M_PER_K*distk;
rate = distm / time*S_PER_H;
mtime = (double)time / distm;
mmin = (int)mtime / S_PER_M;
msec = (int)mtime % S_PER_M;

printf("you ran %1.2f km (%1.2f miles) in %d min,%d sec.\n",
    distk, distm, min, sec);
printf("that pace corresponds to running a mile in %d min,",
    mmin);
printf("%d sec.\nyour average speed was %1.2f mph.\n", msec,
        rate);
getchar();
getchar();
return 0;

}
然后调试
this program converts your time for a metric race
to a time for running a mile and to your average
speed in miles per hour.
please enter,in kiometers,the distance run.
10.0
next enter the time in minutes and seconds.
begin by entering the minutes.
36
now enter the seconds.
23
you ran 10.00 km (621370.00 miles) in 36 min,23 sec.
that pace corresponds to running a mile in 0 min,0 sec.
your average speed was 1024705.45 mph.
最后三行和书上的数据不不一样,
图片说明
distm,min,sec,mmin,msec,rate返回的数据都不对,程序检查了很多遍。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • YXTS122
    YXTS122 2016-11-03 08:19
    已采纳

    const double M_PER_K =0.62137;
    应该是这样吧

    点赞 评论
  • qq_31258245
    qq_31258245 2016-11-03 04:25

    课本上存在印刷错误。

    点赞 评论

相关推荐