C Primer Plus 第5章 运算符、表达式和语句 5.7 一个示例程序
2016-05-22 00:00
411 查看
摘要: 一个对于长跑运动员有用的程序
使用类型指派,使您的目的不但对读者很明显,就是对编译器也很明显。
[code=language-objectivec]/*running.c --一个对于长跑运动员有用的程序*/ #include <stdio.h> const int S_PER_M = 60; const int S_PER_H = 3600; const double M_PER_K = 0.62137; int main() { double distk,distm; //分别以英里和公里计的跑过的距离 double rate; //以英里/小时为单位的平均速度 int min,sec; //跑步用的分钟数和秒数 int time; //用秒表示的跑步用时 double mtime; //跑完一英里所用的时间,以秒计 int mmin,msec; //跑完一英里所用的时间,以分钟和秒计 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表示读取一个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); //把时间转换为全部用秒表示 time = S_PER_M*min + sec; //把公里转换为英里 distm = M_PER_K * distk; //英里/秒X秒/小时=英里/小时 rate = distm/time * S_PER_H; //时间/距离=跑完每英里的用时 mtime = (double)time/distm; mmin = (int)mtime/S_PER_M; //求出分钟数 msec = (int)mtime%S_PER_M; //求出剩余的秒数 printf("You ram %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.\n Your average speed was %1.2f mph.\n",msec,rate); return 0; }
使用类型指派,使您的目的不但对读者很明显,就是对编译器也很明显。
相关文章推荐
- “拿来主义”——怎样嗅探一个串口通信程序与硬件之间的通信(一)
- Unity官方示例UnityWaterSurface分析
- 数据库定时备份原理,代码
- FTP方式的文件上传和下载的Java代码
- C++左右值及引用类型关系
- C++函数模板参数推断
- 开发Yii2过滤器并通过behaviors()行为调用
- window mysql 5.7 解压版 安装过程详解
- ReSharper 配置及用法1
- 第9课:Spark Streaming源码解读之Receiver在Driver的精妙实现全生命周期彻
- 第10课:Spark Streaming源码解读之流数据不断接收全生命周期彻底研究和思考
- 第11课:Spark Streaming源码解读之Driver中的ReceiverTracker架构
- 第12课:Spark Streaming源码解读之Executor容错安全性
- 第13课:Spark Streaming源码解读之Driver容错安全性
- swift:使用cocoapods引入Alamofire
- Laravel5学生成绩管理系统-07-添加侧边栏
- PHP代码调试和测试
- WPF经典编程模式-MVVM示例讲解
- mybatis的接口实现操作数据库
- mybatis的接口实现操作数据库(注解)