首页

2009年5月14日星期四

running.c --一个对于长跑运动员有用的程序

1 //running.c --一个对于长跑运动员有用的程序
2 #include
3 const int S_PER_M =60;
4 const int S_PER_H = 3600;
5 const double M_PER_K = 0.62137;
6 int main(void)
7 {
8 double distk,distm;//分别以公里和英里计跑过的距离
9 double rate;//以英里/小时为单位的平均速度
10 int min,sec;//跑步用时的分钟数和秒数
11 int time;//用秒表示跑步的用时
12 double mtime;//跑完1英里所用的时间以秒计算
13 int mmin,msec;//跑完1英里所用时间,以分钟和秒计
14 printf("This program converts your time for a metric race\n");
15 printf("to a time for running a mile and to your average\n");
16 printf("speed in miles per hour.\n");
17 printf("Please enter,in kilometers,the distance run.\n");
18 scanf("%1f",&distk);//%1f表示读取一个double类型的数值
19 printf("Next enter the time in minutes and seconds.\n");
20 printf("Begin by entering the minutes.\n");
21 scanf("%d",&min);
22 printf("Now enter the seconds.\n");
23 scanf("%d",&sec);
24 //把时间转换为全部用秒表示
25 time=S_PER_M*min+sec;
26 //把公里转换为英里
27 distm=M_PER_K * distk;
28 //英里/秒X秒/小时=英里/小时
29 rate=distm/time*S_PER_H;
30 //时间/距离=跑完每英里的用时
31 mtime=(double)time/distm;
32 mmin=(int)mtime/S_PER_M;//求分钟数
33 msec=(int)mtime%S_PER_M;//求出剩余的秒数
34 printf("You ran %1.2f km(%1.2f miles)in %d min,%d sec.\n",distk,dist m,min,sec);
35 printf("That pace corresponds to running a mile in %d min.\n",mmin);
36 printf("%d sec.\nYou average speed was %1.2f mph.\n",msec,rate);
37 return 0;
38 }

没有评论:

发表评论