【game——关机程序】
程序运行后,会在1分钟内关机,用户需要输入:lalala
,才能停止电脑关机。
电脑内有操作命令符,可以实现关机:
最后按一下回车:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char input[10] = { 0 };
system("shutdown -s -t 60");
again:
printf("请注意你的电脑将在60秒内自动关机,如果输入:lalala,就取消关机\n");
scanf("%s", input);
if (strcmp(input, "lalala") == 0)
{
system("shutdown -a");
}
else //如果用户输入不对,这里我们重复上面的内容,让用户再输入
{
goto again;
}
return 0;
}
注意该代码的几点:
1、system
是一个库函数,可以用来执行系统命令。
2、使用system
函数需要包含头文件stdlib.h
。
3、用户输入的字符串与你规定的字符串比较,需要用到strcmp
。
4、strcmp
函数的头文件是string.h
。
5、如果不是很清楚goto
语句,我们也可以使用循环,结果都是一样的:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char input[10] = { 0 };
system("shutdown -s -t 60");
while (1)
{
printf("请注意你的电脑将在60秒内自动关机,如果输入:lalala,就取消关机\n");
scanf("%s", input);
if (strcmp(input, "lalala") == 0)
{
system("shutdown -a");
break;
}
}
return 0;
}
如果你们想将自己写的这个程序发布给其他人:
要注意下面两个地方的修改:
改完后再生成解决方案:
这个就是可以发布的,可以发布给你想发的人。