C语言-Windows定时关机小程序

整理文件发现以前写的定时关机小程序(Windows下)

1-效果

 

2-程序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	//界面
	printf("-------自动关机小程序-------\n");
	printf("您想在几分钟后关机?请输入正整数:");
	//定义变量
	unsigned long long second;
	unsigned int minutes;
	char charsecond[100] = {};
	char arr[100] = {};
	char cmd[20] = "shutdown -s -t ";
	char input[20];
	scanf("%d", &minutes); //读取分钟数
	second = 60 * minutes; //计算秒数
//	printf("%d\n", second);
	itoa(second, charsecond, 10); //将数字转化为字符串
	strcpy(arr, strcat(cmd, charsecond));
	system(arr);//执行关机命令
//	printf("%s%s", arr, "\n");
	printf("系统将于%d分后关机,输入“stop”可取消关机\n", minutes);

	while (1) {
		scanf("%s", input);
		if (0 == strcmp(input, "stop")) {
			system("shutdown -a");
			printf("关机操作已经被取消\n");
			printf("5秒后程序自动关闭,倒计时:\n");
			int i;
			for (i = 5; i > 0; i--) {
				printf("%d ", i);
				sleep(1);
			}
			break;
		} else {
			printf("关机继续\n");

		}

	}
	return 0;
}

 

posted @   尚方咸鱼  阅读(258)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示