将秒转换为天、分钟和秒

问题描述 投票:0回答:5

我已经设置了将秒转换为天、分钟和秒格式的“挑战”。

例如: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;

它有效并给出了正确的答案,但完成后我查看了其他人是如何解决这个问题的,他们的答案有所不同。我想知道我是否遗漏了一些东西。

c++ date time seconds
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;

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

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

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

0
投票

将秒转换为天、小时、分钟和秒(效果很好,但占用大量空间):

#包括 使用命名空间 std;

int main() {

const long 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);
long 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;

}

© www.soinside.com 2019 - 2024. All rights reserved.