这个闹钟可以在定时提醒你时间到了,采用的是windows系统的任务计划系统。
[以下为老版本,更新请见下↓]
代码如下:
#include "iostream"
using namespace std;
int leng(int number)
{
int flag=0;
while(number)
{
number/=10;
flag++;
}
return flag;
}
int main()
{
int hour;
int minute;
cout << "设定小时:";
cin >> hour;
cout << endl << "设定分钟:";
cin >> minute;
if((hour<=24 && hour>=0) && (minute>=0 && minute<=60))
{
char temp_hour[3],temp_minute[3];
if (leng(minute)<2)
{
sprintf(temp_minute,"0%d",minute);
}
else
{
itoa(minute,temp_minute,10);
}
if (leng(hour)<2)
{
sprintf(temp_hour,"0%d",hour);
}
else
{
itoa(hour,temp_hour,10);
}
system("del clock.bat");
system("echo schtasks /Delete /tn clock_alarm >> clock.bat");
char temp[300];
sprintf(temp,"echo schtasks /Create /tn clock_alarm /tr \"D:\\Works\\C++\\clock.exe\" /sc once /st %s:%s >>clock.bat",temp_hour,temp_minute);
system(temp);
system("echo pause >> clock.bat");
system("echo exit >> clock.bat");
system("start clock.bat");
}
return 0;
}
补上clock.exe的代码:
#include <iostream>
using namespace std;
int main()
{
cout << "Time's up!";
return 0;
}
[新版本]
CLOCK_ALARM.CPP
#include <iostream>
#include <fstream>
#include <fstream>
using namespace std;
int leng(int number)
{
int flag=0;
while(number)
{
number/=10;
flag++;
}
return flag;
}
int main()
{
int hour;
int minute;
a:int choice;
cout << "请输入选项:1)定时闹钟 2)计时闹钟" << endl;
cin >> choice;
if (choice > 2 && choice < 1)
{
cin.clear();
cin.sync();
goto a;
}
switch (choice)
{
case 1: cout << "设定小时:";
cin >> hour;
cout << endl << "设定分钟:";
cin >> minute;
break;
case 2: int minute_plus;
cout << "设定计时分钟:";
cin >> minute_plus;
system("erase /q C:\\time.txt");
system("time /t >> c:\\time.txt");
char timenow[5];
ifstream infile;
infile.open("c://time.txt");
if(infile.is_open())
{
for(int i = 0;infile.good() && !infile.eof();i++)
{
infile >> timenow[i];
}
infile.close();
}
char* address[4];
address[0] = &timenow[0];
address[1] = &timenow[1];
address[2] = &timenow[3];
address[3] = &timenow[4];
hour = atoi(address[0]);
minute = atoi(address[2]);
minute = minute_plus + minute;
while (minute >= 60)
{
++hour;
minute = minute - 60;
}
break;
}
if((hour<=24 && hour>=0) && (minute>=0 && minute<=60))
{
char temp_hour[3],temp_minute[3];
if (leng(minute)<2)
{
sprintf(temp_minute,"0%d",minute);
}
else
{
_itoa_s(minute,temp_minute,10);
}
if (leng(hour)<2)
{
sprintf(temp_hour,"0%d",hour);
}
else
{
_itoa_s(hour,temp_hour,10);
}
system("del clock.bat");
system("echo schtasks /Delete /tn clock_alarm >> clock.bat");
char temp[300];
sprintf(temp,"echo schtasks /Create /tn clock_alarm /tr \"D:\\Works\\C++\\clock.exe\" /sc once /st %s:%s >>clock.bat",temp_hour,temp_minute);
system(temp);
system("echo pause >> clock.bat");
system("echo exit >> clock.bat");
system("start clock.bat");
}
return 0;
}
clock.cpp
#include <windows.h>
#include <mmsystem.h>
#include <iostream>
#pragma comment(lib,"winmm.lib")
using namespace std;
int main()
{
PlaySound(TEXT("D:\\Works\\C++\\clock\\7557.wav"), NULL, SND_FILENAME | SND_ASYNC);
cout << "Time's up!" << endl;
system("pause");
return 0;
}