C++ 时钟 创建心得 (修改,加入了闹钟,输入合法性判断等)

本文介绍了一个用C++实现的简单时钟类的设计过程,包括如何获取系统时间、显示时钟、更新时间以及设置闹钟功能。文章还讨论了如何确保时间数据的有效性和准确性。

今天突发奇想想写个C++的时钟类,本以为是小菜一碟,但却遇到了好多麻烦,看来动手总比想象的难啊。因此做下简单记录

首先要确定的就是时钟类的数据和成员函数,数据很简单了,就是3个整型的数据,用来代表时分秒,成员函数当然包括显示,时钟更新等,为了简单只写了显示、更新和设置时间,当然可以加入更多的闹钟神马之类的。 这次改进在成员函数中 加入了闹钟函数。同时加入了自己的构造函数和析构函数,将时钟的默认时间和系统时间相等。这个办法是通过包含<window.h>文件实现的,定义了系统的时间变量 类sys, 使用函数GetLocalTime来获得系统时间,并将小时、分钟和秒分别赋给Clock类。


这里面主要就是判断数据合法性及什么时候应该更新时间,其中sincrease其实更类似于start,用来时钟开始工作。在显示的时候,采用了格式输出,当不足两位时,补零显示,符合习惯,更新时,每次都覆盖更新。这样就完成了一个简单的时钟类的定义声明,若想要简单的使用它,则在主函数中加入相应代码即可。大致就是这样了,有时间了可以加入更多功能,闹钟,定点报时等功能。

 其中increase函数用来更新闹钟时间,其他两个函数如期名字定义。类的声明完成后,定义其成员函数就顺其自然了。这次改进判断了设置时钟的输入否合法,如果不合法则报错退出,另外在更新秒的时候采用了sleep 函数,使之更加精确,同时通过使电脑蜂鸣来代替闹钟响铃。

主函数的改进方面,提供了用户是否设置时间和闹钟的提示,并根据相应的提示做出动作。


<pre code_snippet_id="145533" snippet_file_name="blog_20140428_2_477161" name="code" class="cpp">///声明clock类
#include<iostream>
#include<Windows.h>  //获取系统当前时间的库函数
#ifndef CLOOCKDIS_000   //避免重复定义 
#define CLOOCKDIS_000
class MyClock 
{
private:            //时钟应有时针 分针 秒针 
	int hour;
	int min;
	int sec;
public:
	
	MyClock()       //构造函数 
	{
		SYSTEMTIME sys;      //定义系统时钟变量
        GetLocalTime( &sys );  //获取系统时间 
		hour=sys.wHour;       //设置时钟为当前时钟
		min=sys.wMinute;
		sec=sys.wSecond;
	//	std::cout<<"construuctor used"<<std::endl;
	}
	
	void settime(int h,int m,int s);   //设置时钟函数
    void hincrease();                  //小时增加函数 
	void mincrease();                  //分钟增加函数 
	void sincrease();                  //秒增加函数 
	void showclock();                 // 显示时钟
	void alarm(int h,int m, int s);   //时钟闹钟


	~MyClock()                       //析构函数
	{
	//	std::cout<<"deconstruuctor used"<<std::endl;
	};


};


#endif


<pre code_snippet_id="145533" snippet_file_name="blog_20140428_3_3094882" name="code" class="cpp">// 定义时钟类的具体变量  

#include<iostream>
#include"clockdisplay.h"  // 包含类的定义的头文件
#include<iomanip>
using namespace std;

void MyClock::settime(int h,int m,int s)    //设置时钟的时间
{


	hour=h;
	if( hour<0||hour>24) 
	{
		cerr<<"wrong hour ,please set the hour between 0 and 24"<<endl;
		exit(1);
	}

	min=m;
	if( min<0||min>60) 
	{
		cerr<<"wrong hour ,please set the miniute between 0 and 60"<<endl;
		exit(2);
	}
	sec=s;
	if( sec<0||sec>60)
	{
		cerr<<"wrong hour ,please set the second between 0 and 60"<<endl;
		exit(3);
	}
};
void MyClock::hincrease()     // 小时增加函数 采用24小时制 
{

	hour++;
	if(hour==24)   //如果小时到了24 则记为零时
		hour=0;
};
void MyClock::mincrease()
{
	min++;       //  如果分钟增加到了60,则小时加1,分钟置零
	if(min==60)
	{
		hincrease();
		min=0;
	}

};

void MyClock::sincrease()  //  如果秒增加到了60,则分钟加1,秒置零
{
	Sleep(1000);  //程序睡眠1000ms,时钟增加1s.
	sec++;
	if(sec==60)
	{
		mincrease();
		sec=0;
	}

};


void MyClock::alarm(int h, int m, int s)    //设置闹钟并响铃
{
	if(hour==h&&min==m&&sec==s)     // 判断MyClock的时间是否与传入闹钟的时间相等,如果相等则蜂鸣
	{
		for(int i=0;i<60;i++)
			cout<<"\a";        //蜂鸣“\a”
	}
}


void MyClock::showclock()     //设置时钟显示格式 小时、分钟和秒都以两位显示,不够两位是使用格式控制的setfill补零,每次显示是覆盖上次(\r) 实现。
{

	cout<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<min<<":"<<setw(2)<<setfill('0')<<sec<<"\r";

};


/// 时钟程序,获取系统的当前时间 并且显示, 同时提供了时钟的
/// 设置和响铃闹钟的功能

//// 作者:QHL

/// 时间 2014.4.28

#include<iostream>
#include<iomanip>
#include"clockdisplay.h"


using namespace std;

int main()
{
   char timestate; //判断是否要重设时间
   char alarmstate; //判断是否要重设闹钟

   int sh=0;  // 记录用户输入的时间
   int sm=0;
   int ss=0;

   int ah=0;  // 记录用户输入的时间
   int am=0;
   int as=0;
   
  
   MyClock mclk;
   //mclk.showclock();
   //cout<<endl;
   //是否重设时间
   cerr<<"do you want to reset the time? Y or N"<<endl;
   cin>>timestate;  //读入状态
   if(timestate=='Y'||timestate=='y')     //如果重设时间,则设置用户时间  
    
   {
	   cerr<<"input the time you want: hour, minute and second"<<endl;
	   cin>>sh>>sm>>ss;
	   mclk.settime(sh,sm,ss);
   }
  
   //是否重设闹钟
   cerr<<"do you want to set the alarm? Y or N"<<endl;
   cin>>alarmstate;

   if(alarmstate=='Y'||alarmstate=='y')     //如果重设时间,则设置用户时间  
    
   {
	   cerr<<"input the alarmtime you want: hour, minute and second"<<endl;
	   cin>>ah>>am>>as;
	   cerr<<"the alarmtime you want is  "<<setw(2)<<setfill('0')<<ah<<":"<<setw(2)<<setfill('0')<<am<<":"<<setw(2)<<setfill('0')<<as<<endl;
   }

    mclk.showclock();

   while(true)
   {  
	 
	   
	   mclk.sincrease();
	   mclk.showclock();
	  if(alarmstate=='Y'||alarmstate=='y')  //如果设置闹钟则响
<span style="white-space:pre">	</span>        mclk.alarm(ah,am,as);
   }
   return 0;
}


<pre code_snippet_id="145533" snippet_file_name="blog_20140107_2_2327737" name="code" class="html">









                
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值