输入一个时间数值,再输入秒数 n,输出该时间再过 n 秒后的时间值,时间的表示形式为时:分:秒,
#include<stdio.h>structtime{inthour,minute,second;};voidtimecal(structtime*p,intn);in...
#include <stdio.h>struct time{ int hour, minute, second;};void timecal(struct time *p, int n);
int main(void){ int n; int repeat, ri; struct time time;
scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri++){ scanf("%d:%d:%d", &time.hour, &time.minute, &time.second); scanf("%d",&n); timecal(&time,n); printf("time: %d:%d:%d\n", time.hour, time.minute, time.second);}
void timecal(stuct time*p, int n);{ if(time.second+n<60)time.second+=n; else if(time.second+n==60){ time.second=0; time.minute++; if(time.minute==60){ time.minute=0; time.hour++; if(time.hour==24) time.hour=0; } } else{ time.second=(n+time.second)%60; if((time.minute+(time.second+n)/60)<60) time.minute+=(time.second+n)/60; else if((time.minute+(time.second+n)/60)==60){ time.minute=0; time.hour++; if(time.hour==24) time.hour=0; } else{ time.minute=(time.minute+(time.second+n)/60)%60; if((time.hour+(time.minute+(time.second+n)/60)/60)<24)time.hour=0; else time.hour=(time.hour+(time.minute+(time.second+n)/60)/60)%24; } } }
程序有点问题,求修正,另外有木有大神有更简单的程序分享 展开
int main(void){ int n; int repeat, ri; struct time time;
scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri++){ scanf("%d:%d:%d", &time.hour, &time.minute, &time.second); scanf("%d",&n); timecal(&time,n); printf("time: %d:%d:%d\n", time.hour, time.minute, time.second);}
void timecal(stuct time*p, int n);{ if(time.second+n<60)time.second+=n; else if(time.second+n==60){ time.second=0; time.minute++; if(time.minute==60){ time.minute=0; time.hour++; if(time.hour==24) time.hour=0; } } else{ time.second=(n+time.second)%60; if((time.minute+(time.second+n)/60)<60) time.minute+=(time.second+n)/60; else if((time.minute+(time.second+n)/60)==60){ time.minute=0; time.hour++; if(time.hour==24) time.hour=0; } else{ time.minute=(time.minute+(time.second+n)/60)%60; if((time.hour+(time.minute+(time.second+n)/60)/60)<24)time.hour=0; else time.hour=(time.hour+(time.minute+(time.second+n)/60)/60)%24; } } }
程序有点问题,求修正,另外有木有大神有更简单的程序分享 展开
1个回答
展开全部
#include <stdio.h>
struct time{
int hour, minute, second;
};
void timecal(struct time *p, int n)
{
p->second += n;
if (p->second>=60)
{
p->minute += p->second/60;//60s一分钟
p->second %= 60;//余数为当前秒数
}
if(p->minute>=60)
{
p->hour += p->minute/60;//60分一小时
p->minute %=60;//余数为当前分钟数
}
if (p->hour>=24)
{
p->hour %= 24;//余数为当前小时
}
}
int main(void)
{
int n;
int repeat, ri;
struct time time;
scanf("%d", &repeat);
for(ri = 1; ri <= repeat; ri++){
scanf("%d%d%d", &time.hour, &time.minute, &time.second);
scanf("%d",&n);
timecal(&time,n);
printf("time: %d:%d:%d\n", time.hour, time.minute, time.second);
}
return 0;
}
//测试无误
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询