5.6 带有参数的函数
程序清单5.15 pound.c 【程序代码:/home/biyj/C_Primer_Plus/chp5/src/learn/pound.c】
程序运行结果:
5.7 一个示例程序
程序清单 5.16 running.c 【程序路径:/home/biyj/C_Primer_Plus/chp5/src/learn/running.c】
/* 一个对于长跑运动员有用的例子 */
#include <stdio.h>
const int S_PRE_M = 60; //每分钟秒数
const int S_PRE_H = 3600; //每小时秒数
const double M_PRE_K = 0.62137; //每公里的英里数
int main(void)
{
double distk, distm; //分别以公里和英里记录跑过的距离
double rate; //以英里/小时为单位的平均速度
int min, sec; //跑步用的分钟数和秒数
int time; //用秒表示跑步用时
double mtime; //每1英里所需的秒数
int mmin, msec; //每1英里所需的分钟数+秒数
printf("This program converts your time for meric 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 kilometers, 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 sconds.\n");
scanf("%d", &sec);
//将时间转换为秒表示
time = S_PRE_M * min + sec;
//把公里转换为英里
distm = M_PRE_K * distk;
//英里/秒 * 秒/小时 = 英里/小时
rate = distm/time * S_PRE_H;
//时间/距离 = 跑完每英里的用时
mtime = (double)time / distm;
mmin = (int)mtime/S_PRE_M; //分钟数
msec = (int)mtime%S_PRE_M; //剩余秒数
printf("You ran %1.2f km (%1.2f miles) in %d min %d sec.\n",
distk, distm, mmin, msec);
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);
return 0;
}
程序运行结果: