头文件采用1. <stdlib.h> 2. <string.h>
注意:system的头文件为stdlib.h
关机程序流程
程序运作起来后,倒计时60秒后关机,如果输入:我是猪,就取消关机。
使用命令行关机
shutdowm -s -t 60 //注意中间必须带有空格! s代表关机 t代表时间 60代表60秒
shutdown -a //a代表停止关机
源代码展示
本代码采用了goto语句
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char arr[20] = {0}; system("shutdown -s -t 60"); again: printf("请注意,你的电脑在1分钟内关机,如果输入:我是猪,就取消关机\n"); scanf("%s", arr); if (strcmp(arr,"我是猪") == 0) { system("shutdown -a"); } else { goto again; } return 0; }
用while语句的形式
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char arr[20] = {0}; system("shutdown -s -t 60"); while(1) { printf("请注意,你的电脑在1分钟内关机,如果输入:我是猪,就取消关机\n"); scanf("%s", arr); if (strcmp(arr,"我是猪") == 0) { system("shutdown -a"); break; } } return 0; }