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---------------------------------------------

截图如下:

还等什么?动手吧 ~  ^_^

 

 

转载于:https://www.cnblogs.com/cppprogrammer/p/3425823.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值