谁能用C++编写一个程序,能够使电脑定时自动关机
- 提问者网友:愿为果
- 2021-04-30 18:17
- 五星知识达人网友:深街酒徒
- 2021-04-30 19:18
源代码:
#include <cstdlib>
#include <iostream>
#include <Conio.h>
using namespace std;
int main(int argc, char *argv[])
{ system("sc config Schedule start= auto");//将所需服务设为自动启动
system("sc start Schedule");//启动Scheduler服务,要实现定时关机需开启此服务
system("cls"); //清屏
string shutdown_time;//定义存储时间的变量
cout<<"请输入自动关机的时间:(格式:HH:MM,例如想在23点15分关机则输入 23:15"<<endl;
cin>>shutdown_time;//输入关机时间
string cmd="at ";
cmd+=shutdown_time;
cmd+=" shoutdown -s";//获得完整的关机命令字符串
system(cmd.c_str());//因为system函数只能接受字符串常量,所以此处要用c_str函数获得string的C风格字符串常量
system("cls");
cout<<"计算机将在"<<shutdown_time<<"关机"<<endl;
cout<<"输入 n 取消刚才的设置,输入其他任意键保留设置并自动退出程序"<<endl;
if(getch()=='n')system("at /delete /y");//如果用户输入n 删除at列表
return EXIT_SUCCESS;
}
至于怎么生成可执行文件,如果你学过编程应该知道的。复制以上代码,打开任意编译工具(IDE)(windows平台一般是vc,鄙人没用过vc所以无法给出详细的步骤,但是IDE操作方式都差不多的)
新建工程,粘贴源代码,编译,在你保存工程的目录里就有exe执行文件了。
代码已给出详细的注释。
另外这不是一个很严谨的程序,只是演示了实现方法,严谨的代码应该对用户输入的字符串做出检测,如果输入格式错误或无效字符,应作出提示或更改。还有本程序在用户输入n以后会删除所有的at列表(并不仅限于用本程序添加的列表)。呵呵,所以使用的时候要注意这点。
希望对你有用。
- 1楼网友:十鸦
- 2021-04-30 23:11
1> h:\vc2008安装目录\vc\include\ostream(785): 或 “std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(909): 或 “std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(916): 或 “std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(923): 或 “std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(930): 或 “std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(170): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(176): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(183): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(190): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(210): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(243): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(263): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 int)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(288): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned int)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(308): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(328): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned long)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(349): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(369): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(390): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(410): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(430): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(450): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> h:\vc2008安装目录\vc\include\ostream(470): 或 “std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)” 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ] 1> 试图匹配参数列表“(std::basic_ostream<_Elem,_Traits>, std::string)”时 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ]
有错啊!!!!!
- 2楼网友:低音帝王
- 2021-04-30 21:44
自己就可以写:
main(){
system("shutdown -s -t 100");
}
以前我写过。现在机子上没有编程工具,。
- 3楼网友:忘川信使
- 2021-04-30 20:26