简单的关机程序(C语言)

Windows 系统自带一个名为的程序,可以用于关机操作(位置在Windows下),一般情况下Windows系统的关机都可以通过调用程序 来实现的,同时该程序也可以用于终止正在计划中的关机操作。

while循环实现:

#include <stdio.h>
#include <stdlib.h>
int main()
{
	char input[10] = { 0 };
	system("shutdown -s -t 60");
	while (1)
	{
		printf("电脑将在1分钟内关机,如果输入:我是猪,就取消关机!
请输入:>");
		scanf("%s", input);
		if (0 == strcmp(input, "我是猪"))
		{
			system("shutdown -a");
			break;
		}
	}
	return 0;
}

goto语句实现:

#include<stdio.h>
#include<stdlib.h>

int main()
{
	char input[10] = { 0 };
	system("shutdown -s -t 60");
again:
	printf("电脑将在1分钟内关机,如果输入:我是猪,就取消关机!
请输入:>");
	scanf("%s", input);
    if (0 == strcmp(input, "我是猪"))
	{
		system("shutdown -a");
	}
	else
	{
		goto again;
	}
	return 0;
}
经验分享 程序员 微信小程序 职场和发展