用c语言的写一段电脑定时关机的代码
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h>
int main() { int hour, minute, second, shutdown_time; time_t now; struct tm *tm_info;
/* 获取当前时间 */
time(&now);
tm_info = localtime(&now);
/* 输入关机时间 */
printf("请输入关机时间(小时 分钟):");
scanf("%d %d", &hour, &minute);
/* 计算关机时间距离当前时间的秒数 */
shutdown_time = ((hour - tm_info->tm_hour) * 60 + (minute - tm_info->tm_min)) * 60 - tm_info->tm_sec;
/* 输出倒计时 */
printf("倒计时 %d 秒\n", shutdown_time);
/* 等待关机时间 */
sleep(shutdown_time);
/* 关闭电脑 */
system("shutdown -h now");
return 0;
原文地址: https://www.cveoy.top/t/topic/dnHh 著作权归作者所有。请勿转载和采集!