C++实现 小巧蜂鸣报时器Beeper
上班工作忙,小白有时候会忘记时间。
自制了个蜂鸣器类,实现定时报时。从此,再也不必担心会忘记吃饭时间了。哈哈。
大家可根据自己的需要进行修改,小伙伴们,尽情发挥你们的创意吧~
--------------------------Beeper.h---------------------------------------------
#pragma once
#include <Windows.h>
#pragma region 时间类
class Time
{
public:
int hour;
int minute;
int second;
};
#pragma endregion
#pragma region 蜂鸣器类
class Beeper
{
public:
void clearScreen(void);
void showAuthorMsg(void);
void showTime(void);
void showTips(void);
void sleep(int ms = 1000); // default 1000 ms
void beep(void);
Beeper(void);
~Beeper(void);
private:
void toBeep(bool isLBeep,int bCount = 0);
void showCurrentTime(void);
void showStartedTime(void);
private:
int beepCount;
bool isLongBeep;
Time t;
SYSTEMTIME curTime;
};
#pragma endregion
--------------------------Beeper.cpp---------------------------------------------
#include "Beeper.h"
#include "windows.h"
#include <iostream>
#include <iomanip> // For std::setw(n)
#pragma region 构造与析构
Beeper::Beeper(void)
{
//std::cout << "Beeper(void) called." << std::endl;
beepCount = 0;
isLongBeep = false;
t.second = 0 ;
t.minute = 0 ;
t.hour = 0 ;
}
Beeper::~Beeper(void)
{
//std::cout << "~Beeper(void) called." << std::endl;
}
#pragma endregion
#pragma region 私有函数
void Beeper::toBeep(bool isLBeep,int bCount)
{
isLongBeep = isLBeep;
beepCount = bCount;
if(isLongBeep)
{
if(curTime.wMinute/30 >= 1)
{
for(int i = 0;i < 2;i++)
{
Beep(5000,500);
}
}
else
{
Beep(5000,1000);
}
}
else
{
while ( beepCount-- )
{
std::cout << "Minute Beeping(" << bCount - beepCount << ")..." << std::endl;
Beep(5000,200);
}
std::cout << "The Beeper has beeped " << bCount << " times successfully." << std::endl;
}
}
void Beeper::showCurrentTime(void)
{
GetLocalTime(&curTime);
std::cout << "The Current Time is "
<< std::setw(4) << std::setfill('0') << curTime.wYear << "-"
<< std::setw(2) << std::setfill('0') << curTime.wMonth << "-"
<< std::setw(2) << std::setfill('0') << curTime.wDay << " "
<< std::setw(2) << std::setfill('0') << curTime.wHour << ":"
<< std::setw(2) << std::setfill('0') << curTime.wMinute << ":"
<< std::setw(2) << std::setfill('0') << curTime.wSecond << " "
<< std::endl;
}
void Beeper::showStartedTime(void)
{
int seconds = ::GetTickCount() / 1000;
t.second = seconds % 60;
t.minute = ((seconds - t.second) / 60)%60;
t.hour = (seconds - t.minute * 60 - t.second) / (60 * 60);
std::cout << "The Computer has Started "
<< std::setw(2) << std::setfill('0') << t.hour << ":"
<< std::setw(2) << std::setfill('0') << t.minute << ":"
<< std::setw(2) << std::setfill('0') << t.second << "."
<< std::endl << std::endl;
}
#pragma endregion
#pragma region 公有函数
void Beeper::clearScreen(void)
{
system("cls");
}
void Beeper::showAuthorMsg(void)
{
std::cout << "--------------------------------" << std::endl;
std::cout << " Time Tools " << std::endl;
std::cout << " CopyRight (C) 2013 EndlessRain " << std::endl;
std::cout << " Nov 15,2013 " << std::endl;
std::cout << "--------------------------------" << std::endl << std::endl;
}
void Beeper::showTime(void)
{
this->showCurrentTime();
this->showStartedTime();
}
void Beeper::showTips(void)
{
std::cout << "-------------------Tips----------------------" << std::endl;
if(curTime.wHour == 17)
{
std::cout << " It is Time for Dinner... " << std::endl;
}
if(curTime.wHour == 12)
{
std::cout << " It is Time for Lunch... " << std::endl;
}
std::cout << "---------------------------------------------" << std::endl;
}
void Beeper::sleep(int ms)
{
Sleep(ms); // default sleep 1000 ms
}
void Beeper::beep(void)
{
if( curTime.wSecond == 0 )
{
if(curTime.wMinute%5 == 0)
{
if( curTime.wMinute/30 >= 1)
{
toBeep(true); // isLongBeep. So,one parameter only.
toBeep(false,(curTime.wMinute%30)/5);
}
else
{
if(curTime.wMinute%30 == 0)
{
toBeep(true);
}
else
{
toBeep(false,curTime.wMinute/5);
}
}
}
}
}
#pragma endregion
--------------------------main.cpp---------------------------------------------
#include "Beeper.h"
#include <windows.h>
#include <iostream>
void beeperBeep(void)
{
Beeper* beeper = new Beeper();
while(true)
{
beeper->clearScreen();
beeper->showAuthorMsg();
beeper->showTime(); // current time && PC started time
beeper->showTips();
beeper->beep();
beeper->sleep(); // default sleep 1000 ms
}
delete beeper;
}
int main(int args,char* argv[])
{
beeperBeep();
system("pause"); // pause for debug
return 0;
}
----------------------------- End---------------------------------------------
截图如下:
还等什么?动手吧 ~ ^_^