输入秒数,转化成小时分钟秒的格式。比如输入3601,则输出01:00:01,不足两位数的,用前置0补齐。小时数可以超过两位数。
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int conversion_time_fun() {
double input_seconds ;
double seconds,minutes,hours;
int seconds02,minutes02, hours02;
cout << "请输入秒数:" << endl;
cin >> input_seconds;
// 计算小时
hours = input_seconds / 60 / 60; // 1
hours02 = (int)hours;
//cout << hours02 << endl;
// 计算分钟
minutes = input_seconds / 60;
if (minutes >= 60) {
//cout << minutes << endl;
//cout << 60 * hours02 << endl;
minutes02 = minutes - 60 * (double)hours02;
}
else {
minutes02 = minutes;
}
//cout << minutes02 << endl;
// 计算秒数
seconds = (60 * 60 * hours02) + (60 * minutes02);
seconds02 = input_seconds - seconds;
//cout << input_seconds << endl;
//cout << seconds << endl;
//cout << seconds02 << endl;
// 输出格式
//cout << setw(2) << hours02 <<":" << setw(2) << minutes02 << ":" << setw(2) << seconds02 << endl;
printf("时间:%02d:%02d:%02d", hours02, minutes02, seconds02); // 自动填充0,2代表填充的个数(位数)
return 0;
}
?
|