admin管理员组文章数量:1582016
前言:
可以写出来后发给你的室友或者好朋友,可以增进你们之间的友谊
功能实现:输入关机命令语句,shutdown -s -t 60,电脑就会在60秒之后关机,输入shutdown -a。电脑会撤销关机指令。在c语言中也可以用代码让电脑关机
1.goto语句实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char input[20]={0};//定义一个数组
system("shutdown -s -t 120");//system对应头文件<stdlib.h>,让计算机做出关机指令
again:
printf("你的电脑将会在120秒后关机,如果你不想关机!\n");
printf("请输入:我是弟弟\n");
scanf("%s",input); //读取输入的信息
if(strcmp(input,"我是弟弟")==0)//判断是否和我是弟弟相同 ,strcmp对应头文件<string.h>
{
system("shutdown -a");//撤销关机指令
}
else
{
goto again;//如果他不输入我是弟弟,则跳到again接着执行。
}
return 0;
}
当用户输入了和你设定的想要的信息不一致时,程序不退出,接着执行,提示用户让用户接着输入。同理也可以用while实现。
2.while语句执行
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char input[20] = {0};
system("shutdown -s -t 60");//对应头文件<stdlib.h>
while (1)
{
printf("电脑将在一分钟内关机,如果输入:我是弟弟,就取消关机\n");
scanf("%s", input);
if (strcmp(input,"我是弟弟")==0)//strcmp函数与输入的信息比较
{
system("shutdown -a");//撤销关机命令
break;//跳出while循环
}
}
return 0;
}
恶搞可以,但要注意分寸!
写在最后:
博主大一实力有限,内容仅供参考,欢迎大家在评论区和我交流!
以后会经常发有趣的小游戏,和学习心得,感兴趣的小伙伴可以点个关注支持一下
版权声明:本文标题:C语言实现关机功能(超详解) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1727890567a1136321.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论