C++ 将秒转换为小时、分钟和秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25696992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:23:47  来源:igfitidea点击:

Converting seconds to hours and minutes and seconds

c++

提问by Shadow

#include <cstdio>
#include <iostream>
using namespace std;
int main ()
{
    int seconds, hours, minutes;
    cin >> seconds;
    hours = seconds/3600;
    cout << seconds << " seconds is equivalent to " << int(hours) << " hours " << seconds%(hours*60) 
         << " minutes " << (seconds%(hours*3600))-((seconds%(hours*60))*60) << " seconds.";
}

For some reason, this program works with only numbers above 3600. Does anyone know how to fix this problem? Whenever I do a number below 3600, the screen shows up with a message from Windows saying that the program has stopped working.

出于某种原因,这个程序只适用于 3600 以上的数字。有谁知道如何解决这个问题?每当我执行低于 3600 的数字时,屏幕都会显示一条来自 Windows 的消息,说该程序已停止工作。

回答by Shadow

Try this out instead, tested and works:

试试这个,测试并工作:

int seconds, hours, minutes;
cin >> seconds;
minutes = seconds / 60;
hours = minutes / 60;
cout << seconds << " seconds is equivalent to " << int(hours) << " hours " << int(minutes%60) 
     << " minutes " << int(seconds%60) << " seconds.";

Because minutes is seconds/60, dividing it by 60 again is equivalent to diving seconds by 3600, which is why it works.

因为分钟是秒/60,再除以 60 就等于将秒除以 3600,这就是它起作用的原因。

回答by T.C.

seconds/3600is integer division, so for seconds < 3600, hoursis 0, then things like seconds%(hours*3600)becomes seconds % 0, causing a division-by-zero.

seconds/3600是整数除法,所以对于seconds < 3600hours0,那么像seconds%(hours*3600)变成seconds % 0,导致除以零。



Let's first get the logic right. Suppose you want to write 5000 secondsas xhours yminutes zseconds, such that all three are integers and neither ynor zis greater than 59. What do you do?

让我们先搞清楚逻辑。假设你想要写5000 secondsx小时yz秒,这样,所有这三个是整数,既不y也不z比大于59,你做什么?

Well, you can first write it as qminutes zseconds, such that both are integers and zis not greater than 59. That's easy:

好了,你可以先写为q分钟z秒,使得两个是整数,并且z不大于59.这很简单:

q = 5000 / 60 = 83  // integer division
z = 5000 % 60 = 20

So 5000 seconds is 83 minutes 20 seconds. Now how do you write 83 minutesinto xhours yminutes, such that both are integers and yis no greater than 59? You do the same thing:

所以 5000 秒是 83 分 20 秒。现在,你如何写83 minutesx小时y分钟,使得两个是整数,并且y不大于59?你做同样的事情:

x = 83 / 60 = 1
y = 83 % 60 = 23

OK, let's generalize this:

好的,让我们概括一下:

int total, seconds, hours, minutes;
cin >> total;
minutes = total / 60;
seconds = total % 60;
hours = minutes / 60;
minutes = minutes % 60;
cout << total << " seconds is equivalent to " << hours << " hours " << minutes 
     << " minutes " << seconds << " seconds.\n" ;

回答by Makoto

You've got a divide-by-zero problem here:

你在这里有一个被零除的问题:

seconds % (hours*60);

hoursis 0 by virtue of integer division.

hours由于整数除法而为 0。

hours = seconds/3600;

From what you're trying to do, you should consider conditional logic to print the minutes if the total number of seconds is greater than 3600. You'll also want to investigate similar logic in the next part of your print stream.

从您尝试执行的操作来看,如果总秒数大于 3600,您应该考虑使用条件逻辑来打印分钟数。您还需要在打印流的下一部分中研究类似的逻辑。

My C++ is rusty, so forgive if this isn't exactlyvalid syntax:

我的 C++ 生锈了,所以如果这不是完全有效的语法,请原谅:

cout << (seconds > 3600 ? seconds % (hours*60) : seconds) << endl;

回答by sarmand asi

by using function;

通过使用功能;

#include<iostream>

    using namespace std;
    int hour(int h)
    {
        int second;
        //second=(h/3600);
        if (h>3600)
            second=h/3600;
        else 
           second=(h/3600);
        return (second);
    }

    int minute(int m)
    {
        int second2;
        second2=(   );
        return(second2);
    }

    int second(int s)
    {
        int second3;
        second3=((s-3600)%60);
        return (second3);
    }

    void main()
    {
        int convert;
        cout<<"please enter seconed to convert it to hour\b";

        cin>>convert;
        cout<<"hr : min : sec \n";
        cout<<hour(convert)<<":"<<minute(convert)<<":"<<second(convert)<<endl;
        system("pause");
    }

回答by Hani Shams

Try this:

尝试这个:

int totalSecond;
cin >> totalSecond;

int hour = totalSecond / 3600;
int minute = (totalSecond % 3600) / 60;
int second = totalSecond % 60;

cout << hour << ":" << minute << ":" << second << endl;

Assuming that totalSecondsis the number of seconds since midnight and is less than 86400

假设这totalSeconds是自午夜以来的秒数并且小于 86400

回答by ahmed

Have a look at this code:

看看这个代码:

#include <iostream>
using namespace std;

int main()
{
  int seconds;
  cout << "Enter an integer for seconds: ";
  cin >> seconds;
  int minutes = seconds / 60;
  int remainingSeconds = seconds % 60;
  cout << seconds << " seconds is " << minutes <<
    " minutes and " << remainingSeconds << " seconds " << endl;

  return 0;

}