Hey everyone. I've continuing to learn C++ and I've been set the 'challenge' of converting seconds to format as the Days,Minutes and Seconds.
嘿每个人。我继续学习c++,并设置了把秒转换成时间、分钟和秒的“挑战”。
For example: 31600000 = 365 days, 46 minutes, 40 seconds.
例如: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;
It works and comes up with the correct answer but after completing it I looked at how other people had tackled it and theirs was different. I'm wondering If I'm missing something.
它起作用了,并得出了正确的答案,但在完成之后,我观察了其他人是如何处理它的,他们的是不同的。我想知道我是不是漏掉了什么。
Thanks, Dan.
谢谢你,丹。
4 个解决方案
#1
5
One of the things about programming is that there is never just one way to do something. In fact if I were to set my mind to it, I might be able to come up with a dozen completely different ways to accomplish this. You're not missing anything if your code meets requirements.
关于编程,有一点是从来都不是只有一种方法。事实上,如果我下定决心,我可能会想出一打完全不同的方法来实现这个目标。如果您的代码满足需求,您不会丢失任何东西。
For your amusement, here's a way to format up hours:minutes:seconds under Windows (elapsed
is a double & represents number of seconds elapsed since... something)
有趣的是,这里有一种设置小时格式的方法:分钟:Windows下的秒(运行时间是双秒,表示从……的东西)
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));
#2
8
this seems to me to be the easiest way to convert seconds into DD/hh/mm/ss:
在我看来,这是将秒转换成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;
I hope it helps!
我希望它可以帮助!
Regards,
问候,
Stoycho
Stoycho
#3
4
I think is the challenge from Stephen Prata's book. I did it as follows:
我认为这是来自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;
}
#4
1
For example: 31600000 = 365 days, 46 minutes, 40 seconds.
例如:31600000 = 365天,46分钟,40秒。
Really?
真的吗?
$ bc
365*24*60*60 + 46*60 + 40
31538800
365*24*60*60 + 1066*60 + 40
31600000
Did you mean "convert the input into days, hours, minutes and seconds, and then discard the hours" or "convert the input into days, total minutes within a day (i.e. can be more than 60), and seconds"?
你的意思是“把输入转换成天、小时、分钟和秒,然后放弃小时”还是“把输入转换成天、一天的总分钟(即可以超过60分钟)和秒”?
In the second case I think you should replace the instruction for minutes with
在第二种情况下,我认为你应该用几分钟代替说明书
long minutes = input_seconds / secs_to_min % (mins_in_hour * hours_in_day);
#1
5
One of the things about programming is that there is never just one way to do something. In fact if I were to set my mind to it, I might be able to come up with a dozen completely different ways to accomplish this. You're not missing anything if your code meets requirements.
关于编程,有一点是从来都不是只有一种方法。事实上,如果我下定决心,我可能会想出一打完全不同的方法来实现这个目标。如果您的代码满足需求,您不会丢失任何东西。
For your amusement, here's a way to format up hours:minutes:seconds under Windows (elapsed
is a double & represents number of seconds elapsed since... something)
有趣的是,这里有一种设置小时格式的方法:分钟:Windows下的秒(运行时间是双秒,表示从……的东西)
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));
#2
8
this seems to me to be the easiest way to convert seconds into DD/hh/mm/ss:
在我看来,这是将秒转换成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;
I hope it helps!
我希望它可以帮助!
Regards,
问候,
Stoycho
Stoycho
#3
4
I think is the challenge from Stephen Prata's book. I did it as follows:
我认为这是来自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;
}
#4
1
For example: 31600000 = 365 days, 46 minutes, 40 seconds.
例如:31600000 = 365天,46分钟,40秒。
Really?
真的吗?
$ bc
365*24*60*60 + 46*60 + 40
31538800
365*24*60*60 + 1066*60 + 40
31600000
Did you mean "convert the input into days, hours, minutes and seconds, and then discard the hours" or "convert the input into days, total minutes within a day (i.e. can be more than 60), and seconds"?
你的意思是“把输入转换成天、小时、分钟和秒,然后放弃小时”还是“把输入转换成天、一天的总分钟(即可以超过60分钟)和秒”?
In the second case I think you should replace the instruction for minutes with
在第二种情况下,我认为你应该用几分钟代替说明书
long minutes = input_seconds / secs_to_min % (mins_in_hour * hours_in_day);