include
include
using namespace std;
struct Time {
int hours;
int minutes;
int seconds;
};
Time addSeconds(Time t, int secondsToAdd) {
t.seconds += secondsToAdd;
// Convert seconds overflow to minutes
t.minutes += t.seconds / 60;
t.seconds %= 60;
// Convert minutes overflow to hours
t.hours += t.minutes / 60;
t.minutes %= 60;
// Handle overflow for hours (如果超过24小时,则归零)
if (t.hours >= 24) {
t.hours %= 24;
}
return t;
}
int main() {
Time t;
int n;
cout << "请输入时间(格式:小时 分钟 秒):";
cin >> t.hours >> t.minutes >> t.seconds;
cout << "请输入要增加的秒数(小于60):";
cin >> n;
if (n >= 60 || n < 0) {
cout << "输入的秒数无效,请输入一个小于60的秒数。" << endl;
return 1;
}
Time result = addSeconds(t, n);
cout << "再过 " << n << " 秒后的时间是:" << endl;
cout << setw(2) << setfill('0') << result.hours << ":"
<< setw(2) << setfill('0') << result.minutes << ":"
<< setw(2) << setfill('0') << result.seconds << endl;
return 0;
}
标签:60,int,每日,11.25,hours,seconds,Time,打卡,minutes From: https://www.cnblogs.com/formygloryandpeacefulday/p/18568910