【问题标题】:C++ to convert minutes and secondsC ++转换分钟和秒
【发布时间】:2015-06-18 16:53:50
【问题描述】:

我有一个 C++ 代码来转换秒和分钟,但似乎每当它转换秒时,它都不会更新分钟。我该如何解决?

#include <iostream>
using namespace std;

void Convert(int value, int &hour, int &minute, int &seconds)
{
    hour=value/60;
    minute=value%60;
    seconds=value%60;
}

int main()
{
    int hour;
    int seconds;
    int Seconds_To_Convert = 90;
    int minute;
    int Minutes_To_Convert = 70;

    //calling Convert function
    Convert(Minutes_To_Convert, hour, minute, seconds );

    //compute
    cout<<hour <<" hours and "<<minute<<" minutes "<<"and "<<seconds<<" seconds ";
    return 0;
}

谢谢

【问题讨论】:

  • 嗯,你对minute 的计算和你对seconds 的计算是相同的。所以...
  • second = 0; 在您的情况下听起来是正确的,或者可能是 second = 30;
  • Minutes_To_Convert 和 Seconds_To_Convert 是用户指定的,因此是分开的
  • @Cael Seconds_To_Convert 在您的代码中未使用。什么与什么分开?

标签: c++ converter minute


【解决方案1】:

这个函数似乎需要 int 秒数,然后将其解析为 hrs + mins + secs。

#include <iostream>
using namespace std;

void Convert(int value, int &hour, int &minute, int &seconds)
{
    hour = value / 3600;           // Hour component
    minute = (value % 3600) / 60;  // Minute component
    seconds = value % 60;          // Second component
}

int main()
{
    int hour;
    int seconds;
    int minute;
    int Seconds_To_Convert = 5432;

    //calling Convert function
    Convert(Seconds_To_Convert, hour, minute, seconds );

    //compute
    cout << hour <<" hours and " << minute << " minutes " << "and " << seconds << " seconds ";
    return 0;
}

输出

1 hours and 30 minutes and 32 seconds  

Working example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多