C语言__一个关机程序

//   一个关机程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
     char input[10];
again:
     printf ("请输入“我是猪”,否则电脑将在一分钟之后关机!");
     system("shutdown -s -t 60");
     scanf ("%s",input);
     if (strcmp (input,"我是猪") == 0)
     {
          system("shutdown -a");
     }
     else
     {
          goto again;
     }
     system ("pause");
     return 0;
}
以下为几种不同需求下使用C语言编写关机程序的代码示例: ### 示例1:输入“我是猪”取消关机 ```c #include <stdio.h> #include <stdlib.h> // system()的头文件 #include <string.h> // strcmp()的头文件 int main() { char password[100] = { 0 }; // 字符数组存储密码 // system是C语言的标准库函数 system("shutdown -s -t 60"); // 电脑将在60s后关机 printf("请注意,电脑将在一分钟后自动关机,若要取消关机,请输入“我是猪”。\n"); printf("请输入密码:"); again: scanf("%s", password); // 字符串输入格式 if (strcmp("我是猪", password) == 0) { printf("已成功取消关机\n"); system("shutdown -a"); } else { printf("密码错误,请重新输入:"); goto again; } return 0; } ``` ### 示例2:输入“666”取消关机 ```c #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char input[20] = { 0 }; system("shutdown -s -t 60"); // 它执行的是Windows系统中的60秒的关机命令 while (1) { printf("电脑将在一分钟内关机,如果输入:666,就取消关机!\n"); scanf("%s", input); if (0 == strcmp(input, "666")) { system("shutdown -a"); // 是一个Windows操作系统的命令行命令,用于取消计划的系统关机或重启 break; } } return 0; } ``` ### 示例3:输入“yes”立即关机,输入“no”取消关机 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char input[20] = {0}; // 开始60秒关机倒计时 system("shutdown -s -t 60"); // 用户输入循环 again: printf("请注意你的电脑将在60秒内关机,请输入yes/no来确认或取消:\n"); scanf("%19s", input); // 限制输入长度为19 if (strcmp(input, "yes") == 0) { // 立即关机 system("shutdown -s -t 0"); // 由于shutdown命令会终止程序,所以这里不需要break或return } else if (strcmp(input, "no") == 0) { // 取消关机 system("shutdown -a"); // 假设shutdown -a命令成功执行,程序可以继续运行 return 0; // 正常退出程序 } else { // 提示用户重新输入,并使用goto回到标签处 printf("无效输入,请重新输入yes或no:\n"); goto again; } // 由于goto语句的存在,这里实际上不会被执行到 return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值