第一站NO.5--《C Primer Plus(第五版)中文版》第五章_5.6 带有参数的函数和5.7 一个示例程序

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;

}

程序运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值