一、練習計算和資料型別
#include<stdio.h>
#pragma warning(disable:4996)
const int S_PER_M = 60; //1分鐘的秒數
const int S_PER_H = 3600; //1小時的秒數
const double M_PER_K = 0.62137; //1公里的英里數
int D27_1_running(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 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 minute 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; //公里換成英里
rate = distm / time * S_PER_H; //英里/秒 * 秒/小時 = 英里/小時
mtime = (double)time / distm; //時間/距離 = 跑1英里的所用的時間
mmin = (int)mtime / S_PER_M; //求出分鐘數
msec = (int)mtime % S_PER_M; //求出剩余的秒數
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;
}

二、原始碼:
- D27_1_running.c
https://github.com/ruigege66/CPrimerPlus/blob/master/D27_1_running.c- CSDN:https://blog.csdn.net/weixin_44630050
- 博客園:https://www.cnblogs.com/ruigege0000/
- 歡迎關注微信公眾號:傅里葉變換,個人賬號,僅用于技術交流

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/165320.html
標籤:C
上一篇:自己做的第一個“游戲”
