您的位置:首页 > 其它

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

使用类型指派,使您的目的不但对读者很明显,就是对编译器也很明显。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息