C語言編寫簡單的定時關(guān)機程序
更新時間:2016年02月16日 11:53:51 投稿:hebedich
本文給大家分享的是一則C語言編寫的簡單的定時關(guān)機程序,可以設(shè)置0-600秒倒計時,有需要的小伙伴可以參考下。
寫一個定時關(guān)機的小程序,可以立即關(guān)閉計算機,也可以一段時間后關(guān)閉計算機。
這里主要考察system()命令。
代碼實現(xiàn):
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char cmd[20]="shutdown -s -t ";
char t[5]="0";
int c;
system("title C語言關(guān)機程序"); //設(shè)置cmd窗口標題
system("mode con cols=48 lines=25"); //窗口寬度高度
system("color f0"); //可以寫成 red 調(diào)出顏色組
system("date /T");
system("TIME /T");
printf("----------- C語言關(guān)機程序 -----------\n");
printf("1.實現(xiàn)10分鐘內(nèi)的定時關(guān)閉計算機\n");
printf("2.立即關(guān)閉計算機\n");
printf("3.注銷計算機\n");
printf("0.退出系統(tǒng)\n");
printf("-------------------------------------\n");
scanf("%d",&c);
switch(c) {
case 1:
printf("您想在多少秒后自動關(guān)閉計算機?(0~600)\n");
scanf("%s",t);
system(strcat(cmd,t));
break;
case 2:
system("shutdown -p");
break;
case 3:
system("shutdown -l");
break;
case 0:
break;
default:
printf("Error!\n");
}
system("pause");
return 0;
}
這個程序雖然實用價值不大,但是可以讓我們了解 system() 函數(shù)。
在Windows下,system() 函數(shù)可以執(zhí)行 dos 命令;在 Unix/Linux 中,可以執(zhí)行Shell。
請在Windows下運行上面的程序。程序中對dos界面的設(shè)置和關(guān)機功能都是通過dos命令實現(xiàn)的。
相關(guān)文章
C++/STL實現(xiàn)判斷平面內(nèi)兩條線段的位置關(guān)系代碼示例
這篇文章主要介紹了C++/STL實現(xiàn)判斷平面內(nèi)兩條線段的位置關(guān)系代碼示例,具有一定參考價值,需要的朋友可以了解下。2017-11-11

