如何用C语言去写一段关机程序?
有的初学者觉得C语言编程太枯燥了,想要写个程序来太难了,甚至有的人在学习了C之后就觉得C语言只能去写一些黑白框里的简单程序,其实不是的,C语言是一种面向过程的开发语言,他可以去做电脑硬件和软件系统的开发,小编也是个C语言的小白,但是凭借着对C语言的热爱,小编想能否用C语言开发出一个关于开关机的小程序呢?如图:
于是,小编开始上网查询资料,经过了一天的奋战,终于完成了一个让自己满意的作品。
首先选择开发环境,小编用的是Dev C++ 5.10,如图:
然后,开始编写代码:
一、定义头文件:
#include<stdio.h>
#include<stdlib.h>
说明:因为我们要用到库函数system来实现系统功能而库函数system在头文件stdlib.h中,所以,要定义头文件#include<stdilib.h>
二、定义main函数:
#include<stdio.h>
#include<stdlib.h>
int main(void)
三、实现操作提示:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
printf("******************************************\n");
printf("| |\n");
printf("| 请输入要操作的序号 |\n");
printf("| 1、关机 2、重启 3、退出 |\n");
printf("| |\n");
printf("******************************************\n");
}
四、功能区代码的编写:
int OFF;
printf("请输入序号:");
scanf("%d",&OFF);
if(OFF==1)
{
system("shutdown -s -t 5 -f -c '您的机器已被外星人入侵,计算机即将在5s内关机!!!'");
}
else if(OFF==2)
{
system("shutdown -r -t 4 -f -c '您的机器已被外星人入侵,计算机即将在4s内重启!!!'");
}
else
{
system("exit");
}
最后的代码为:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main(void)
{
printf("*****************************************\n");
printf("| |\n");
printf("| 请输入要操作的序号 |\n");
printf("| |\n");
printf("| 1.关机 2.重启 3.退出 |\n");
printf("| |\n");
printf("*****************************************\n");
int OFF;
printf("请输入序号:");
scanf("%d",&OFF);
if(OFF==1)
{
system("shutdown -s -t 5 -f -c '您的机器已被外星人入侵,计算机即将在5s内关机!!!'");
}
else if(OFF==2)
{
system("shutdown -r -t 4 -f -c '您的机器已被外星人入侵,计算机即将在4s内重启!!!'");
}
else
{
system("exit");
}
return 0;
}
实现效果: