将秒转换为天、分钟和秒。

12
我被要求将秒转换为“天,分钟和秒”的格式。
例如:31600000 = 365天46分钟40秒。
using namespace std;
const int hours_in_day = 24;
const int mins_in_hour = 60;
const int secs_to_min = 60;

long input_seconds;
cin >> input_seconds;

long seconds = input_seconds % secs_to_min;
long minutes = input_seconds / secs_to_min % mins_in_hour;
long days = input_seconds / secs_to_min / mins_in_hour / hours_in_day;

cout << input_seconds << " seconds = "
     << days << " days, "
     << minutes << " minutes, "
     << seconds << " seconds ";

return 0;

代码能够正常工作并得出正确答案,但在完成后我看了一下其他人是如何处理的,他们的方式有所不同。我在想我是否遗漏了什么。


天数 = 时间 / 86400 小时 = (时间 / 3600) - (天数 * 24) 分钟 = (时间 / 60) - (天数 * 1440) - (小时 * 60) 秒数 = 时间 mod 60 - Dan
你的评论毫无意义,如果可以的话,我会给它点个踩。 - Mecanik
5个回答

11

这对我来说似乎是将秒转换为DD/hh/mm/ss的最简单方法:

#include <time.h>
#include <iostream>
using namespace std;    

time_t seconds(1641); // you have to convert your input_seconds into time_t
tm *p = gmtime(&seconds); // convert to broken down time

cout << "days = " << p->tm_yday << endl;
cout << "hours = " << p->tm_hour << endl;
cout << "minutes = " << p->tm_min  << endl;
cout << "seconds = " << p->tm_sec << endl;

你确定吗?使用你的代码,我的结果显示1641秒等于0天1小时27分钟21秒,这似乎非常不可能(多了一个小时)。 - Wim
是的,你说得对Wim。在转换为打散时间时,你必须使用gmtime而不是localtime。也许你处于UTC +01:00时区(例如CET),其中一个小时的差异来自于此。 - stoycho
啊,没想到那个,非常感谢 :-) 但是我目前正在使用John Dibling的答案(见上文): int hours = floor(totalseconds/3600.0); int minutes = floor(fmod(totalseconds,3600.0)/60.0); int seconds = fmod(totalseconds,60.0); human_readable.sprintf("%01du %02dm %02ds", hours, minutes, seconds); - Wim
@stoycho 感谢您的回答。我想知道,您如何使用您的解决方案来显示毫秒?干杯!! - Guy Avraham

6

我认为这是Stephen Prata的书中的挑战。我按照以下方式完成了它:

#include <iostream>

using namespace std;

int main()
{
    long input_seconds = 31600000;

    const int cseconds_in_day = 86400;
    const int cseconds_in_hour = 3600;
    const int cseconds_in_minute = 60;
    const int cseconds = 1;

    long long days = input_seconds / cseconds_in_day;
    long hours = (input_seconds % cseconds_in_day) / cseconds_in_hour;
    long minutes = ((input_seconds % cseconds_in_day) % cseconds_in_hour) / cseconds_in_minute;
    long seconds = (((input_seconds % cseconds_in_day) % cseconds_in_hour) % cseconds_in_minute) / cseconds;
    cout << input_seconds << " seconds is " << days << " days, " << hours << " hours, " << minutes << " minutes, and " << seconds << " seconds.";

    cin.get();
    return 0;
}

6

编程的一件事情就是从来没有仅有一种做法。实际上,如果我专注于此,我可能会想出十几种完全不同的方法来完成这个任务。只要你的代码符合需求,你就没有错。

为了娱乐大家,在Windows下格式化小时:分钟:秒的方式如下(elapsed是一个双精度浮点数,表示自某个时间点以来经过的秒数):

sprintf_s<bufSize>(buf, "%01.0f:%02.0f:%02.2f", floor(elapsed/3600.0), floor(fmod(elapsed,3600.0)/60.0), fmod(elapsed,60.0));

1
例如:31600000 = 365天,46分钟,40秒。
真的吗?
$ bc
365*24*60*60 + 46*60 + 40
31538800

365*24*60*60 + 1066*60 + 40
31600000

你是说将输入转换为天、小时、分钟和秒,然后丢弃小时,还是转换为天、一天内的总分钟数(可能超过60),以及秒?
如果是第二种情况,我认为你应该用以下指令替换分钟的说明。
long minutes = input_seconds / secs_to_min % (mins_in_hour * hours_in_day);

是的,我本意是要舍弃小时数,不过还是谢谢你的回复。 - Dan

-1
将秒转换为天、小时、分钟和秒;它完美地工作,而且不会消耗太多资源。
#include <iostream>
using namespace std;

int main()
{

    const int Seconds_Per_Day = 86400;
    const short Seconds_Per_Hour = 3600;
    const short Seconds_Per_Minute = 60;
    int Input_Seconds;

    cout << "Please, enter seconds: \n"; 
    cin >> Input_Seconds;

    short Number_Of_Days = floor(Input_Seconds / Seconds_Per_Day);
    int Reminder_1 = Input_Seconds % Seconds_Per_Day;
    short Number_Of_Hours = floor(Reminder_1 / Seconds_Per_Hour);
    short Reminder_2 = Reminder_1 % Seconds_Per_Hour;
    short Number_Of_Minutes = floor(Reminder_2 / Seconds_Per_Minutes);
    short Number_Of_Seconds = Reminder_2 % Seconds_Per_Minutes;

    cout << "The number of days is " << Number_Of_Days << ", of hours is " << 
    Number_Of_Hours << ", of minutes is " << Number_Of_Minutes << ", and of 
    seconds is " << Number_Of_Seconds << endl;

    return 0;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接