• C Primer Plus(6) 中文版 第5章 运算符、表达式和语句 5.7 示例程序


    // running.c -- A useful program for runners
    #include
    const int S_PER_M = 60;         // seconds in a minute
    const int S_PER_H = 3600;       // seconds in an hour
    const double M_PER_K = 0.62137; // miles in a kilometer
    int main(void)
    {
        double distk, distm;  // distance run in km and in miles
        double rate;          // average speed in mph
        int min, sec;         // minutes and seconds of running time
        int time;             // running time in seconds only
        double mtime;         // time in seconds for one mile
        int mmin, msec;       // minutes and seconds for one mile
        
        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 kilometers, the distance run.\n");
        scanf("%lf", &distk);  // %lf for type double
        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);
        // converts time to pure seconds
        time = S_PER_M * min + sec;
        // converts kilometers to miles
        distm = M_PER_K * distk;
        // miles per sec x sec per hour = mph
        rate = distm / time * S_PER_H;
        // time/distance = time per mile
        mtime = (double) time / distm;
        mmin = (int) mtime / S_PER_M; // find whole minutes
        msec = (int) mtime % S_PER_M; // find remaining seconds
        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);
        
        return 0;
    }

    /* 输出:

    */

    使用强制类型转换在所有系统上都没有问题。对读者而言,强制类型转换强调了转换类型的意图,对编译器而言也是如此。 

  • 相关阅读:
    【数据结构】如何设计循环队列?图文解析(LeetCode)
    用python做图片搜索引擎并保存到本地
    计算机毕业设计(附源码)python疫情下公共场所卫生安全管理系统
    数据库(mysql)之用户管理
    Innodb底层原理与Mysql日志机制
    Mybatis杂谈
    CSS(二)css选择器+css背景
    力扣(LeetCode)54. 螺旋矩阵(C++)
    普洛斯探索新型算力基础设施“智”冷之道,发布制冷系统预制集成技术白皮书
    微信聚合聊天系统的便捷功能:自动回复
  • 原文地址:https://blog.csdn.net/weixin_40186813/article/details/126196813