Time.h
-----------------------------------------------------------------------------------------------------------------------------
#pragma once
#include <windows.h>
#include <iostream>
#include <iomanip>
using namespace std;
class Time
{
private:
int hour;
int min;
int sec;
bool timeflag;//作为时间的制式 12 24
SYSTEMTIME clocktime;//设置闹钟时间 按f12可以看一下这个结构体,
public:
Time(void);
~Time(void);
void SetTime(SYSTEMTIME);//设置时间,获取系统的时间,用SYSTEMTIME传入时间参数,把这个时间转换为定义hour,min,sec
void ShowTime();// 显示时间
void SetClock(int, int);//设置闹铃,没必要考虑秒针,只考虑 时 分就行了
bool IsClock();//是否响铃
};
Time.cpp
------------------------------------------------------------------------------------------------------------
#include "Time.h"
Time::Time(void)
{
hour = 0;
min = 0;
sec = 0;
timeflag = false;
//clocktime = { 0 };
memset(&clocktime,0x00,sizeof(clocktime));
}
Time::~Time(void)
{
}
void Time::SetTime(SYSTEMTIME time)
{
if(time.wHour > 12 && timeflag == false)
{
time.wHour%=12; //或者time.wHour-=12
}
hour = time.wHour;
min = time.wMinute;
sec = time.wSecond;
}
void Time::ShowTime()
{
system("cls");//清屏,刷新控制台
cout << setw(2) <<setfill('0') <<hour; //见<iomanip>文件
cout <<":" << setw(2) <<setfill('0') <<min;
cout << ":"<< setw(2) <<setfill('0') <<sec;
}
void Time::SetClock(int h, int m)
{
clocktime.wHour=h;
clocktime.wMinute=m;
clocktime.wSecond=0;
}
bool Time::IsClock()
{
if (hour==clocktime.wHour && min==clocktime.wMinute && sec==clocktime.wSecond)
{
return true;
}
return false;
}
main.cpp
---------------------------------------------------------------------------------------------------------------------
#include "Time.h"
#include <conio.h>//用 法:int kbhit(void);包含头文件: include <conio.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
using namespace std;
int main()
{
Time t1; //定义个对象
SYSTEMTIME time;
int h; int m;
while(!_kbhit())
{
GetLocalTime(&time);
t1.SetTime(time);
t1.ShowTime(); //构造函数, t1已经初始化了,
if(t1.IsClock())
{
PlaySound(L"com.wav", NULL, SND_FILENAME|SND_ASYNC);
}
if (_kbhit())
{
cin >> h;
cin >> m;
t1.SetClock(h,m);
}
}
return 0;
}