前提知识
system() 函数
头文件:stdlib.h
函数原型:
int system(const char* command);
函数功能:
在系统中,在控制台直接调用命令,参数字符串command为命令内容
函数返回值:
命令执行成功返回0,失败返回-1
strcmp() 函数
头文件:string.h
函数原型:
int strcmp(const char* str1,const char* str2);
函数功能:
根据两个字符串的ASCII值,一个字符一个字符的比较两个字符串的大小
函数返回值:
字符串1的值大于字符串2,则返回大于0的数字
字符串1的值等于字符串2,则返回0
字符串1的值小于字符串2,则返回小于0的数字
cmd命令
电脑关机 设置 时间为60s后
shutdown -s -t 60
取消关机
shutdown -a
实现代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
char input[20] = { 0 };
system("shutdown -s -t 60");
again:
printf("电脑将在60s后关机...\n输入:\"爸爸\"。即可取消\n");
printf("请输入->");
scanf("%s", input);
if (strcmp(input, "爸爸") == 0) {
system("shutdown -a");
}
else {
goto again;
}
return 0;
}