解析: 运行此程序,必须输入正确才会取消关机,如果直接关闭程序或者输入错误,电脑到了一分钟会直接关机,必须输入正确的指令才会取消关机。
使用goto语句实现电脑关机程序
代码:
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char input[20] = { 0 }; //shutdown -s -t 60 关机命令60秒 system("shutdown -s -t 60"); /*system执行系统命令的*/ again: printf("请注意!!!电脑将在1分钟之内关机,如果输入:取消关机 ,则电脑才会取消关机。\n请输入>:"); scanf("%s", input); if (strcmp(input,"取消关机")==0) /*strcmp() 比较2个字符串是否相等*/ { system("shutdown -a"); //取消关机 printf("已取消关机!\n"); } else { goto again; } return 0; }
使用循环语句实现电脑关机程序
代码:
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char input[20] = { 0 }; system("shutdown -s -t 60"); while (1) { printf("请注意!!!电脑将在1分钟之内关机,如果输入:取消关机 ,则电脑将会取消关机。\n"); printf("请输入>:"); scanf("%s", input); if (strcmp(input, "取消关机") == 0) { system("shutdown -a"); printf("已取消关机!\n"); break; } } return 0; }