C语言:定时关机

写一个定时关机的小程序,可以立即关闭计算机,也可以一段时间后关闭计算机。

system()命令

代码实现:

 
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. int main()
  6. {
  7. char cmd[20]="shutdown -s -t ";
  8. char t[5]="0";
  9. int c;
  10.  
  11. system("title C语言关机程序"); //设置cmd窗口标题
  12. system("mode con cols=48 lines=25"); //窗口宽度高度
  13. system("color f0"); //可以写成 red 调出颜色组
  14. system("date /T");
  15. system("TIME /T");
  16.  
  17. printf("----------- C语言关机程序 -----------\n");
  18. printf("1.实现10分钟内的定时关闭计算机\n");
  19. printf("2.立即关闭计算机\n");
  20. printf("3.注销计算机\n");
  21. printf("0.退出系统\n");
  22. printf("-------------------------------------\n");
  23.  
  24. scanf("%d",&c);
  25. switch(c) {
  26. case 1:
  27. printf("您想在多少秒后自动关闭计算机?(0~600)\n");
  28. scanf("%s",t);
  29. system(strcat(cmd,t));
  30. break;
  31. case 2:
  32. system("shutdown -p");
  33. break;
  34. case 3:
  35. system("shutdown -l");
  36. break;
  37. case 0:
  38. break;
  39. default:
  40. printf("Error!\n");
  41. }
  42. system("pause");
  43. return 0;
  44. }

这个程序虽然实用价值不大,但是可以让我们了解 system() 函数。

在Windows下,system() 函数可以执行 dos 命令;在 Unix/Linux 中,可以执行Shell。

该程序得在Windows运行上,程序中对dos界面的设置和关机功能都是通过dos命令实现的。

猜你喜欢

转载自blog.csdn.net/weixin_44015669/article/details/92674259