日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++實現關機功能詳細代碼_C 語言

作者:愛德蘇 ? 更新時間: 2022-03-26 編程語言

前言:

可以寫出來后發給你的室友或者好朋友,可以增進你們之間的友誼

功能實現:

輸入關機命令語句,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;
}	

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

總結

原文鏈接:https://blog.csdn.net/weixin_64131583/article/details/122408774

欄目分類
最近更新