C++转换分钟和秒

C++ to convert minutes and seconds

本文关键字:分钟 转换 C++      更新时间:2023-10-16

我有一个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;
}

谢谢

似乎这个函数应该需要int秒数,然后将其解析为小时 + 分钟 + 秒。

#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  

工作示例