引言
点击键盘上windows键,搜索框里输入cmd 出现命令提示符打开
输入如下内容:
(注意shutdown空格-s空格-t空格60)
shutdown是用来关机的名字
-s是设置关机
-t是设置倒计时(图中第一行的意思是让电脑60s后关机)
-a是取消关机
关机程序的实现
#include<stdio.h>
#include<stdlib.h>//使用system需要包含的头文件
#include<string.h>//使用strcmp需要包含的头文件
int main()
{
char input[20] = { 0 };//字符数组 用来存放字符串
system("shutdown -s -t 60");//system是一个函数,这个函数是用来执行命令的
again:
printf("注意,你的电脑将在一分钟内关机,如果输入:我是猪,就取消关机\n");
scanf("%s", input);//不需要取地址,因为数组名本来就是地址
//判断:如果两个字符串相等则返回0
//strcmp-string compare
if (strcmp(input, "我是猪") == 0)//两个字符串在比较时不能用==判断
{
system("shutdown -a");//取消关机
printf("已取消关机\n");
}
else
{
goto again;
}
return 0;
}
//用while循环优化
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char input[20] = { 0 };//字符数组 用来存放字符串
system("shutdown -s -t 60");//system是一个函数,这个函数是用来执行命令的
while (1)
{
printf("注意,你的电脑将在一分钟内关机,如果输入:我是猪,就取消关机\n");
scanf("%s", input);//不需要取地址,因为数组名本来就是地址
//判断:如果两个字符串相等则返回0
//strcmp-string compare
if (strcmp(input, "我是猪") == 0)//两个字符串在比较时不能用==判断
{
system("shutdown -a");//取消关机
printf("已取消关机\n");
break;//相等时跳出循环
}
}
return 0;
}