时间类型 - 9. 推算时间(时间加秒数)(函数)

请编写函数,计算某时间过若干秒后的时间。

函数原型

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;
    
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值