• 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;
    }

    /* 输出:

    */

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

  • 相关阅读:
    如何设计一个短地址服务
    反向传播详解BP
    【我的OpenGL学习进阶之旅】着色器GLSL运行时报错 GLSL compile error: Premature end of line
    Postman CSRF 配置
    分享Figma一些非常快速、省时、省力的功能和小技巧
    2023.9.3 三色标记、Shenandoah和ZGC收集器、Redis消息队列
    web —— css(1)
    Android---自定义View
    15-4 创建运行时类的对象和获、调用运行时类的完整结构
    SpringCloud-7-Spring Boot使用Jetty容器
  • 原文地址:https://blog.csdn.net/weixin_40186813/article/details/126196813