C++关机代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++关机代码

爱德苏   2022-05-23 我要评论

前言:

可以写出来后发给你的室友或者好朋友,可以增进你们之间的友谊

功能实现:

输入关机命令语句,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;
}	

恶搞可以,但要注意分寸!

总结

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们