c 语言闹钟源码,小程序源代码:给自己设计一个番茄闹钟,文末有代码
时间: 2024-01-21 16:04:55 浏览: 34
以下是一个简单的 C 语言闹钟源码,可以设置闹钟时间并进行倒计时,当倒计时结束时会提示用户:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
int main()
{
int hours, minutes, seconds, total_seconds;
printf("Enter the hours: ");
scanf("%d", &hours);
printf("Enter the minutes: ");
scanf("%d", &minutes);
printf("Enter the seconds: ");
scanf("%d", &seconds);
total_seconds = hours * 3600 + minutes * 60 + seconds;
while (total_seconds > 0)
{
printf("Time left: %02d:%02d:%02d\n", total_seconds / 3600, (total_seconds % 3600) / 60, total_seconds % 60);
Sleep(1000); // 暂停一秒钟
total_seconds--;
}
printf("Time's up!\a\n"); // 发出警报声
return 0;
}
```
该程序首先要求用户输入小时、分钟和秒数,然后将其转换为总秒数。接着,程序会不断地输出剩余时间,直到计时结束。在每次循环中,程序暂停一秒钟,然后将总秒数减少一秒钟。最后,程序会提示用户时间到了,并发出警报声。
注意:该程序使用了 Windows API 的 Sleep 函数,因此只能在 Windows 系统上运行。如果要在其他操作系统上运行,需要使用相应的延时函数替代 Sleep 函数。