请编写函数,计算某时间过若干秒后的时间。
函数原型
void AddSecond(MYTIME *dstTime, const MYTIME *srcTime, int second);
说明:参数 srcTime
为指示源时间的指针, second
为秒数, dstTime
为指示结果时间的指针。
裁判程序
#include <stdio.h>
......
void AddSecond(MYTIME *dstTime, const MYTIME *srcTime, int second);
int main()
{
MYTIME a, b;
int s;
TimeInput(&a);
scanf("%d", &s);
AddSecond(&b, &a, s);
TimeOutput(&b);
putchar('\n');
return 0;
}
......
/* 你提交的代码将被嵌在这里 */
输入样例1
8:59:59
2
输出样例1
09:00:01
输入样例2
9:0:1
-2
输出样例2
08:59:59
void AddSecond(MYTIME *dstTime, const MYTIME *srcTime, int second) {
int total_seconds = srcTime->hour * 3600 + srcTime->minute * 60 + srcTime->second + second;
while(total_seconds > 3600*24)total_seconds -= 3600*24;
while(total_seconds < 0)total_seconds += 3600*24;
dstTime->hour = total_seconds / 3600;
dstTime->minute = (total_seconds % 3600) / 60;
dstTime->second = (total_seconds % 3600) % 60;
}