1.shutdown关机命令
在学习编写关机程序之前,我们要知道基于window系统,利用cmd命令窗口可以执行关机命令。
shutdown -s :关机
shutdown -a :可以取消关机
shutdown -s -t 可以设置多少秒后关机
2.while循环实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char input[20] = { 0 };
system("shutdown -s -t 60");
while (1)
{
printf("请注意,你的电脑将在一分钟之后关机,如果输入:我是猪方可取消关机\n请输入>:");
scanf("%s", &input);
if (strcmp(input, "我是猪") == 0)
{
system("shutdown -a");
break;
}
}
return 0;
}
3.goto语句实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
char input[20] = { 0 };
system("shutdown -s -t 60");
again:
printf("请注意,你的电脑将在1分钟内关机,如果输入:我是猪,就取消关机\n请输入>:");
scanf("%s", input);
if (strcmp(input, "我是猪") == 0)
{
system("shutdown -a");
}
else
{
goto again;
}
return 0;
}
4.while和go共同实现
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
system("shutdown -s -t 60");
while (1)
{
printf("你的电脑将在60秒后关机,请输入我是猪,取消关机\n");
char input[20];
scanf("%s", &input);
if (strcmp(input, "我是猪") == 0)
{
printf("哈哈,我骗你,在输入一次我是猪,我就帮你取消关机\n");
a:
scanf("%s", &input);
if (strcmp(input, "我是猪") == 0)
{
printf("那我就帮你取消吧");
system("shutdown -a");
printf("你的电脑已取消关机。\n");
break;
}
else
{
printf("在给你一次机会好好想想\n");
goto a;
}
}
else
printf("快点输我是猪啦,不然真的会关机的\n");
}
return 0;
}