C语言的闹钟
Alarm clock in C
代码运行完美,直到它打印 xx.00.00,它打印了两次。所有其他代码运行没有问题,但每次从 23.59.59 -) 00.00.00 开始,它都会打印两次。请帮助我
#include <stdio.h>
void check(int number) {
if (number < 10) {
printf("0%d", number);
}
else {
printf("%d", number);
}
}
void count(int present_time, int time_for_alarm) {
int present_hour = present_time/10000;
int present_min = (present_time/100) % 100;
int present_sec = (present_time % 100);
int alarm_hour = time_for_alarm/10000;
int alarm_min = (time_for_alarm/100) % 100;
int alarm_sec = (time_for_alarm % 100);
while (present_hour != alarm_hour) {
if (present_hour == alarm_hour && present_min == alarm_min && present_sec == alarm_sec) {
printf("ALARM");
}
else {
if (present_sec <= 60) {
present_sec++;
if (present_sec == 60) {
present_sec = 0;
present_min++;
}
if (present_min == 60) {
present_min = 0;
present_hour++;
}
if (present_hour == 24) {
present_hour = 0;
}
check(present_hour);
printf(":");
check(present_min);
printf(":");
check(present_sec);
printf("\n");
}
}
}
while (present_min != alarm_min) {
if (present_hour == alarm_hour && present_min == alarm_min && present_sec == alarm_sec) {
printf("ALARM");
}
else {
if (present_sec <= 60) {
present_sec++;
if (present_sec == 60) {
present_sec = 0;
present_min++;
}
check(present_hour);
printf(":");
check(present_min);
printf(":");
check(present_sec);
printf("\n");
}
}
}
while (present_sec != alarm_sec) {
check(present_hour);
printf(":");
check(present_min);
printf(":");
check(present_sec);
printf("\n");
present_sec++;
}
}
int main() {
int present_time, time_for_alarm;
printf("What time is it? (HHMMSS)");
scanf("%d", &present_time);
printf("What time should the alarm go off? (HHMMSS)");
scanf("%d", &time_for_alarm);
count(present_time, time_for_alarm);
printf("ALARM");
return 0;
}
问题就在这里:
while (present_sec != alarm_sec) {
check(present_hour);
printf(":");
check(present_min);
printf(":");
check(present_sec);
printf("\n");
present_sec++;
}
在每隔一个循环中,先前进 present_sec
,然后打印时间。在这个循环中,你在之后前进。当你到达正确的小时和分钟时,你就进入了这个循环,你将永远在这里复制时间。尝试输入 225555 和 225605。您会看到 22:56:00 重复。
代码运行完美,直到它打印 xx.00.00,它打印了两次。所有其他代码运行没有问题,但每次从 23.59.59 -) 00.00.00 开始,它都会打印两次。请帮助我
#include <stdio.h>
void check(int number) {
if (number < 10) {
printf("0%d", number);
}
else {
printf("%d", number);
}
}
void count(int present_time, int time_for_alarm) {
int present_hour = present_time/10000;
int present_min = (present_time/100) % 100;
int present_sec = (present_time % 100);
int alarm_hour = time_for_alarm/10000;
int alarm_min = (time_for_alarm/100) % 100;
int alarm_sec = (time_for_alarm % 100);
while (present_hour != alarm_hour) {
if (present_hour == alarm_hour && present_min == alarm_min && present_sec == alarm_sec) {
printf("ALARM");
}
else {
if (present_sec <= 60) {
present_sec++;
if (present_sec == 60) {
present_sec = 0;
present_min++;
}
if (present_min == 60) {
present_min = 0;
present_hour++;
}
if (present_hour == 24) {
present_hour = 0;
}
check(present_hour);
printf(":");
check(present_min);
printf(":");
check(present_sec);
printf("\n");
}
}
}
while (present_min != alarm_min) {
if (present_hour == alarm_hour && present_min == alarm_min && present_sec == alarm_sec) {
printf("ALARM");
}
else {
if (present_sec <= 60) {
present_sec++;
if (present_sec == 60) {
present_sec = 0;
present_min++;
}
check(present_hour);
printf(":");
check(present_min);
printf(":");
check(present_sec);
printf("\n");
}
}
}
while (present_sec != alarm_sec) {
check(present_hour);
printf(":");
check(present_min);
printf(":");
check(present_sec);
printf("\n");
present_sec++;
}
}
int main() {
int present_time, time_for_alarm;
printf("What time is it? (HHMMSS)");
scanf("%d", &present_time);
printf("What time should the alarm go off? (HHMMSS)");
scanf("%d", &time_for_alarm);
count(present_time, time_for_alarm);
printf("ALARM");
return 0;
}
问题就在这里:
while (present_sec != alarm_sec) {
check(present_hour);
printf(":");
check(present_min);
printf(":");
check(present_sec);
printf("\n");
present_sec++;
}
在每隔一个循环中,先前进 present_sec
,然后打印时间。在这个循环中,你在之后前进。当你到达正确的小时和分钟时,你就进入了这个循环,你将永远在这里复制时间。尝试输入 225555 和 225605。您会看到 22:56:00 重复。