#include <stdio.h>
// this creates the structure
struct time
{
int hour;
int minute;
int second;
};
//function that calculates the time
struct time timeUpdate(struct time now)
{
now.second = now.second + 1;
if (now.second == 60)
{
now.second = 0;
now.minute = now.minute + 1;
if (now.minute == 60)
{
now.minute = 0;
now.hour = now.hour + 1;
}
if (now.hour = 60)
{
now.hour = 0;
}
}
return now;
}
//user input and output
int main(void)
{
struct time currentTime, nextTime;
printf("Please enter the current time: ");
scanf("%i:%i:%i\n", ¤tTime.hour, ¤tTime.minute, ¤tTime.second);
nextTime = timeUpdate(currentTime);
printf("%.2i:%.2i:%.2i\n", nextTime.hour, nextTime.minute, nextTime.second);
return 0;
}
答案 0 :(得分:0)
假设您的输入有效(0 <=小时<= 23 && 0 <=分钟<= 59 && 0 <=秒<= 59),这是我的观察结果:
在timeUpdate中,您说now.hour =60。您必须检查now.hour是否等于24
(if (now.hour == 24)
)
这是您的功能应为的方式:
now.second = now.second + 1;
if (now.second == 60)
{
now.second = 0;
now.minute = now.minute + 1;
if (now.minute == 60)
{
now.minute = 0;
now.hour = now.hour + 1;
if (now.hour == 24)
{
now.hour = 0;
}
}
}
return now;
我从scanf删除了'\ n',因为我很困惑为什么程序不显示结果(我必须写一个字符并按Enter键才能看到结果)。现在,该行看起来像这样scanf("%i:%i:%i", ¤tTime.hour, ¤tTime.minute, ¤tTime.second);