如何用C语言编写一个简单的关机小程序?
- 内容介绍
- 文章标签
- 相关推荐
本文共计572个文字,预计阅读时间需要3分钟。
本例展示了如何使用C语言实现一个简单的关机小程序,包括立即关机和定时关机功能。以下是一段参考代码:
c#include #include #include
void shutdownNow() { system(shutdown /s /t 0);}
void scheduleShutdown(int seconds) { time_t now; time_t shutdownTime; struct tm *now_tm;
time(&now); now_tm=localtime(&now); shutdownTime=now + seconds;
system(shutdown /s /t 0);}
int main() { int choice; int seconds;
printf(选择关机方式:\n); printf(1. 立即关机\n); printf(2. 定时关机\n); printf(请输入选项(1或2):); scanf(%d, &choice);
if (choice==1) { shutdownNow(); } else if (choice==2) { printf(请输入关机前的秒数:); scanf(%d, &seconds); scheduleShutdown(seconds); } else { printf(无效的选项。\n); }
return 0;}
本文实例为大家分享了C语言实现关机小程序,立即关机和定时关机的具体代码,供大家参考,具体内容如下
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<windows.h> int main() { printf(" ===================\n");//菜单界面 printf(" 1.立刻关机\n"); printf(" 2.定时关机\n"); printf(" 3.退出程序\n"); printf(" ===================\n"); int choice;//选项1.2.3 AGAME: scanf("%d",&choice);//输入选项 switch (choice) { case 1: system("shutdown -s");//立刻关机 break; case 2: system("shutdown -i");//打开定时关机设置界面 break; case 3: printf("正在退出程序...");//2s后退出程序 Sleep(2000); return 0; default: printf("输入错误,请重新输入!\n");//输入错误,警告 goto AGAME;//跳回开始,重新输入 } system("pause"); return 0; }
之前小编遇到的一个有趣题目,大家都来了解一下
题目要求:用C语言编写一个关机程序
程序分析
系统提示一分钟之后关机,如果输入“我爱你”就注销关机,否则将跳到关机程序,继续提示,如果一分钟之内输入不对,则关机。
主要程序
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char input[20]; system ("shutdown -s -t 60"); again: printf ("你的电脑将在60秒之后关机,如果输入我是猪,就取消关机。\n"); scanf ("%s",input); if (strcmp(input,"我是猪") == 0 ) { system("shutdown -a"); } else { goto again; } return 0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计572个文字,预计阅读时间需要3分钟。
本例展示了如何使用C语言实现一个简单的关机小程序,包括立即关机和定时关机功能。以下是一段参考代码:
c#include #include #include
void shutdownNow() { system(shutdown /s /t 0);}
void scheduleShutdown(int seconds) { time_t now; time_t shutdownTime; struct tm *now_tm;
time(&now); now_tm=localtime(&now); shutdownTime=now + seconds;
system(shutdown /s /t 0);}
int main() { int choice; int seconds;
printf(选择关机方式:\n); printf(1. 立即关机\n); printf(2. 定时关机\n); printf(请输入选项(1或2):); scanf(%d, &choice);
if (choice==1) { shutdownNow(); } else if (choice==2) { printf(请输入关机前的秒数:); scanf(%d, &seconds); scheduleShutdown(seconds); } else { printf(无效的选项。\n); }
return 0;}
本文实例为大家分享了C语言实现关机小程序,立即关机和定时关机的具体代码,供大家参考,具体内容如下
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<windows.h> int main() { printf(" ===================\n");//菜单界面 printf(" 1.立刻关机\n"); printf(" 2.定时关机\n"); printf(" 3.退出程序\n"); printf(" ===================\n"); int choice;//选项1.2.3 AGAME: scanf("%d",&choice);//输入选项 switch (choice) { case 1: system("shutdown -s");//立刻关机 break; case 2: system("shutdown -i");//打开定时关机设置界面 break; case 3: printf("正在退出程序...");//2s后退出程序 Sleep(2000); return 0; default: printf("输入错误,请重新输入!\n");//输入错误,警告 goto AGAME;//跳回开始,重新输入 } system("pause"); return 0; }
之前小编遇到的一个有趣题目,大家都来了解一下
题目要求:用C语言编写一个关机程序
程序分析
系统提示一分钟之后关机,如果输入“我爱你”就注销关机,否则将跳到关机程序,继续提示,如果一分钟之内输入不对,则关机。
主要程序
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char input[20]; system ("shutdown -s -t 60"); again: printf ("你的电脑将在60秒之后关机,如果输入我是猪,就取消关机。\n"); scanf ("%s",input); if (strcmp(input,"我是猪") == 0 ) { system("shutdown -a"); } else { goto again; } return 0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

