#include"stdio.h"
#include"stdlib.h"
#include"string.h"
void main()
{
char str[20]={0};
system("shutdown -s -t 120");
again:
printf("电脑将在一分钟后关机,如果输入csh好帅,就停止关机\n请输入>");
scanf("%s",str);
if (strcmp(str, "csh好帅") == 0)
{
system("shutdown -a");
}
else
{
goto again;
}
}
goto语句适合用于跳出二层及以上循环,如果滥用,会导致程序员逻辑混乱。最好平时还是使用while、for循环这些。
这个程序用到了system()函数,用来控制我们计算机系统的,我们可以按住window键+R,输入cmd打开命令行,输入命令shutdown -s -t 60 回车,计算机就会提示在1分钟后关机,此时我们可以手动输入shutdown -a 回车,阻止计算机关机。
system()用到的库函数是"stdlib.h"
如何判断关机?我这里用到的是字符串比较strcmp()函数,当strcmp==0时,两个字符串相等。
strcmp()库函数是"string.h"