- #include<iostream>
- #include<cstdlib>
- #include<string>
- #include<sstream>
- #include<time.h>
- #define ESC 27
- using namespace std;
- struct localTime{ //建立时间结构体为获取系统时间做准备
- int localHour;
- int localMinutes;
- int localSecond;
- };
- struct typeTime{
- int hour;
- int minutes;
- int second;
- };
- int getConmand(int &Conmand)//获取要关机的分钟
- {
- cout<<"请输入你想关机的时间:(时间为分钟)"<<endl;
- cin>>Conmand;
- return Conmand;
- }
- string changeNumber(int& Conmand)//将int类型的时间转化为String类型
- {
- stringstream ss;
- ss<<Conmand;
- return ss.str();
- }
- void getTime(typeTime&typetime)//输入要关机的具体时间
- {
- cout<<"请输入你要关机的时间(用空格间隔)!"<<endl;
- cout<<"小时:";cin>>typetime.hour;
- cout<<endl<<"分钟:";cin>>typetime.minutes;
- cout<<endl<<"秒:";cin>>typetime.second;
- }
- tm *getLocalTime(){ //获取系统时间
- time_t t;
- t=time(0);
- tm *time;
- time=localtime(&t);
- return time;
- }
- void outputTimeNow(localTime&localtime)//显示现在电脑的准确时间
- {
- cout<<"现在的时间是:";
- cout<<"小时:"<<localtime.localHour<<" 分钟:"<<localtime.localMinutes<<" 秒:"<<localtime.localSecond<<endl;
- }
- typeTime getMinutes(localTime& localtime,typeTime&typetime)//转化时间差
- {
- typetime.hour=typetime.hour-localtime.localHour;
- typetime.minutes=typetime.minutes-localtime.localMinutes;
- typetime.second=typetime.second-localtime.localSecond;
- return typetime;
- }
- int getSec(typeTime&typetime)//验证时间的准确性,并得到准确的时间
- {
- while(typetime.hour<0)
- {
- typetime.hour=24-typetime.hour;
- }
- if(typetime.minutes<0)
- {
- typetime.hour-=1;
- typetime.minutes=60-typetime.minutes;
- }
- if(typetime.second<0)
- {
- typetime.minutes-=1;
- typetime.second=60-typetime.second;
- }
- return typetime.hour*3600+typetime.minutes*60+typetime.second;
- }
- int main()
- {
- cout<<"*********************************************************"<<endl;
- cout<<"* *"<<endl;
- cout<<"* 胡萌同学欢迎使用电脑自动关机软件>_< *"<<endl;
- cout<<"* 你可以设置在几分钟后关闭电脑或者什么时间关闭电脑 *"<<endl;
- cout<<"* (早点睡觉,记着!) *"<<endl;
- cout<<"*********************************************************"<<endl;
- char select;
- cout<<" A.在几分钟后关闭电脑"<<endl;
- cout<<" B.在具体时间关闭电脑 "<<endl;
- cout<<" C.立即关机 "<<endl;
- cin>>select;
- if(select=='A'||select=='a') //选择A所执行的代码
- {
- int Conmand;
- getConmand(Conmand);
- Conmand*=60;
- string conmand=changeNumber(Conmand);
- string struction="shutdown -s -t "+conmand;//得到cmd命令
- char *str=const_cast<char*>(struction.c_str()); //将string命令转化为char*命令
- system(str);
- cout<<"关机命令已经设置完毕"<<endl;
- }
- if(select=='b'||select=='B')
- {
- int Conmand;
- tm *time; //获取系统时间为下步骤作比较
- time=getLocalTime();
- localTime localtime;
- localtime.localHour=time->tm_hour;
- localtime.localMinutes=time->tm_min;
- localtime.localSecond=time->tm_sec;
- outputTimeNow(localtime);
- typeTime typetime;
- getTime(typetime);//从用户获取要关机的时间
- typetime=getMinutes(localtime,typetime);//获取时间差
- Conmand=getSec(typetime);
- string conmand=changeNumber(Conmand);
- string struction="shutdown -s -t "+conmand;//得到cmd命令
- char *str2=const_cast<char*>(struction.c_str()); //将string命令转化为char*命令
- system(str2);
- cout<<"关机命令已经设置完毕"<<endl;
- }
- if(select=='C'||select=='c')
- {
- system("shutdown -s -t 0");
- cout<<"关机命令已经设置完毕"<<endl;
- }
- cout<<"关机命令已经设置完成"<<endl;
- cout<<"退出请按ESC,取消刚才的操作请按Q"<<endl;
- cin>>select;
- if(select=='Q'||select=='q')
- system("shutdown -a ");
- if(select==ESC)
- exit(0);
- cin.get();
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1310201513802.html
来源: http://www.codesnippet.cn/detail/1310201513802.html