c++如何编写一个关机程序
然后保存什么格式??可以“打开直接运行”的文件,怎样保存? 展开
编写代码:
#include <iostream>
using namespace std;
int main()
{
system("shutdown -s -f -t 0");
return 0;
}
保存按Ctrl+F9,目录下就会出现一个.exe文件。
扩展资料:
还可以定时关机
#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语言关机程序"); //设置cmd窗口标题
system("mode con cols=48 lines=25"); //窗口宽度高度
system("color f0"); //可以写成 red 调出颜色组
system("date /T");
system("TIME /T");
printf("----------- C语言关机程序 -----------\n");
printf("1.实现10分钟内的定时关闭计算机\n");
printf("2.立即关闭计算机\n");
printf("3.注销计算机\n");
printf("0.退出系统\n");
printf("-------------------------------------\n");
scanf("%d",&c);
switch(c) {
case 1:
printf("您想在多少秒后自动关闭计算机?(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;
}
#include <string>
using namespace std;
void print()
{
system( "CLS" );
system( "title C++ 关机程序" );//设置cmd窗口标题
system( "mode con cols=52 lines=17" );//窗口宽度高度
system( "color 8f" );
cout << " ╔═══════════════════════╗\n"
<< " ║ C++ 关机程序 ║\n"
<< " ║ 请输入: ║\n"
<< " ║ 1.实现定时关闭计算机 ║\n"
<< " ║ 2.立即关闭计算机 ║\n"
<< " ║ 3.注销计算机 ║\n"
<< " ║ 4.重新启动计算机 ║\n"
<< " ║ 5.取消关机或注销操作(留意右下角提示) ║\n"
<< " ║ 0.退出系统(当然直接关闭是一样的) ║\n"
<< " ║ ║\n"
<< " ║ ║\n"
<< " ╚═══════════════════════╝\n";
}
int main()
{
string c;
char cmds[ 25 ] = "shutdown -s -t 000000000";
print();
cin >> c;
while( c != "0" )
{
while( c == "1" )
{
int num;
cout << "Please enter the time: " << endl;
cin >> num;
for( int i = 0; i < 9; i++ )
{
cmds[ 23 - i ] = static_cast< char >( num % 10 + 48 );
num = num / 10;
}
system( cmds );
break;
}
while( c == "2" )
{
system( "shutdown -p" );
break;
}
while( c == "3" )
{
system( "shutdown -l" );
break;
}
while( c == "4" )
{
system( "shutdown -r -t 0" );
break;
}
while( c == "5" )
{
system( "shutdown -a" );
break;
}
print();
cin >> c;
}
return 0;
}
保存之后又个文件夹叫debug
里面有可执行的exe文件。
#include <iostream>
using namespace std;
int main()
{
system("shutdown -s -f -t 0");
return 0;
}
然后保存,按Ctrl+F9,目录下就会出现一个.exe文件
#include<windows.h>
using namespace std;
int main()
{
system("shutdown -s -f -t 0");
return 0;
}
ctrl+s保存,编译运行后会在保存的目录下创建一个.exe文件
{ int hour, min, sec; cout << "请输入一个定时 :" << endl; cout << "定时时:" << endl; cin >> hour; cout << "定时分:" << endl; cin >> min; cout << "定时秒:" << endl; cin >> sec;
time_t now; struct tm * nowTime;
time(&now);
nowTime = localtime(&now);
nowTime->tm_hour = hour;
nowTime->tm_min = min;
nowTime->tm_sec = sec;
double setTime = mktime(nowTime); double waitTime = setTime - now; cout <<"距当前时间" << (int)waitTime / 3600 << "小时:" << ((int)waitTime % 3600) / 60 <<"分:" << ((int)waitTime % 3600) % 60 << "秒后关机" <<endl;
sleep(waitTime);
system("shutdown -s -t 0"); return 0;
}