C++小时钟(包含C++类继承, C++ IO, 定时器)

#include <windows.h>
#include <winbase.h>   


#include <iomanip>
#include <iostream>
#include <string>
using namespace std;


#define Event_ID 1024


int g_iTime = 1000;
//光标定位
HANDLE   hStdout;   
//光标位置   
COORD     cursorPos;


void initTimer();
void printhelp();
void start();


class Time
{
public:
	Time(int hour1,int minute1,int second1);
	void print();
	void adv(int advhour,int advminute,int advsecond);
	void Increment();
	void reset(){hour=0;minute=0;second=0;}
	
	
protected:
	int hour;
	int minute;
	int second;
};


Time* g_time = 0;


class Date
{
public:
	Date(int _year, int _month, int _day);
	void print();
	void Increment();
	void reset(){ year = 2012; month = 12; day = 26; }
protected:
	int year;
	int month;
	int day;
};


Date* g_date = 0;


class DateTime: public Time, public Date
{
public:
	DateTime(int _year, int _month, int _day, int _hour,int _minute,int _second);
	void Increment();
	void print();
private:
	string country;
	int diff;
};


DateTime* g_DateTime = 0;




class DateUtil{
public:
	int NowToDay(int year,int month ,int day);// 从某一天到 1800 年1 月1 日 有多少天 
	bool InputDayIsTrue( int year , int month , int day );  // 判断输入的日期是否合法
	int FindDayWeekend( int year , int month ,int day );    //查找某一天是星期几
	int DisplayMonth( int year , int month );//显示某一年的某一月的日历
	int DisplayYear( int year );//显示某一年的日历
private :
	int SpaceSize;                      // 输出长度 setw()大小
	bool IsLeapYear(int year){         // 判断是否是闰年
		return ( ( 0 == year % 4 && 0 != year %100 ) || ( 0 == year % 400 ) );
	}
public:
	int GetMonthDayNumber(int year , int month){   //返回 某一年某一月天数
		int daynumber ;
		if(   2 == month  ) {  //二月单独处理
			if( IsLeapYear( year ) )
				daynumber = 29 ;
			else
				daynumber = 28 ;
		}else{
			if( ( month < 8  && 1 == month % 2) || ( month >= 8 && 0 == month % 2 ) )  //  八月前奇数月为31 天 八月后 偶数为 31 天
				daynumber = 31 ;
			if( ( month < 8  && 0 == month % 2) || ( month >= 8 && 1 == month % 2 ) )  //  八月前偶数月 30 天  八月后 奇数月30 天
				daynumber = 30 ;
		}
		return daynumber;
	}
};


DateUtil g_dateUtil;


class Timer
{
public:
	Timer();
	void CreateTimerThread(int* pi);
	static DWORD CALLBACK TimeThread(PVOID pvoid);
	static void CALLBACK TimeProc(HWND hWnd,UINT uMsg,UINT idEvent,DWORD dwTime);
	static int count;
};


Timer* g_timer = 0;


int main()
{
	cout<<"请输入年 月 日 时 分 秒"<<endl;
	int year,month,day;
	cin >> year >> month >> day ;
	// g_date = new Date(year, month, day);
	
	
	int hour, minute, second;
	cin >> hour >> minute >> second ;
	// g_time = new Time(hour, minute, second);
	
	
	g_DateTime = new DateTime(year, month, day, hour, minute, second);
	system("cls");
	// 标准输出句柄   
	hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
	
	
	initTimer();
	
	
	// g_time->print();
	// g_date->print();
	g_DateTime->print();
	
	
	printhelp();
	
	
	int press = 0;
	while(true)
	{
		cin>>press;
		switch(press)
		{
		case 1:
			start();
			break;
		case 2:
		case 3:
			//   释放句柄   
			CloseHandle(hStdout);   
			exit(0);
			break;
		}
	}
	return 0;
}
//
void initTimer()
{
	g_timer = new Timer;
}


void printhelp()
{
	cout<<endl<<endl;
	cout<<"\t************************"<<endl;
	cout<<"\t小        时          钟"<<endl;
	cout<<"\tPress:                  "<<endl;
	cout<<"\t    1.start              "<<endl;
	cout<<"\t    2.pause              "<<endl;
	cout<<"\t    3.stop               "<<endl;
	cout<<"\t************************"<<endl;
}


void start()
{
	int* pi = &g_iTime;
	g_timer->CreateTimerThread(pi);
}
//


Time::Time(int hour1,int minute1,int second1)
{
	this->hour=hour1;this->second=second1;this->minute=minute1;
}


void Time::print()
{
	cursorPos.X   =  15;   
	cursorPos.Y   =   0;  
	SetConsoleCursorPosition(   hStdout,   cursorPos   );
	cout<<setfill('0')<<setw(2)<<this->hour<<":";
	cout<<setfill('0')<<setw(2)<<this->minute<<":";
	cout<<setfill('0')<<setw(2)<<this->second<<endl;
}


void Time::adv(int advhour,int advminute,int advsecond)
{
	if ((this->second+advsecond)>=60)
	{
		this->second=this->second+advsecond-60;
		this->minute++;
	}
	else this->second=this->second+advsecond;
	if (this->minute+advminute>=60)
	{
		this->minute=this->minute+advminute-60;
		this->hour++;
	}
	else this->minute=this->minute+advminute;
	
	
	this->hour=(this->hour+advhour)%24;
}


void Time::Increment()
{
	this->adv(0, 0, 1);
}




int DateUtil :: NowToDay(int year,int month ,int day){
	int DiDay , i ;
	for( i = 1800 , DiDay = 0 ; i < year ; i++)  // 闰年 单独+ 366 天
		if( IsLeapYear( i ) )
			DiDay += 366 ;
		else
			DiDay += 365 ;
		for( i = 1 ; i < month ; i++ )
			DiDay += GetMonthDayNumber( year , i );
		
		
		DiDay += day - 1 ;
		
		
		return DiDay;
}


bool DateUtil :: InputDayIsTrue( int year , int month , int day ){
	int TureDayNumber ;
	if( year < 1800  ||  month > 13 || day > 31 ){    // 如果输入的日期小于 1800 年 大于 13 月 大于31 天 返回 假
		return false;
	}else{
		TureDayNumber = GetMonthDayNumber( year, month ); // 如果输入的day  比该月的实际 day 大 返回错误
		if( day > TureDayNumber  )
			return false ;
	}
	return true;
}
int DateUtil ::DisplayMonth(int year,int month ){
	int  Excur ,  i , TotalDay  ; 
	SpaceSize = 4 ;
	Excur = ( NowToDay(year,month,1 ) + 3 ) % 7 ;  // 首先计算 这个月的 1 日 是星期几 然后在这个基础上自加天数
	cout << year << "年 " << month << "月" << endl;
	
	
	cout << setw( SpaceSize ) <<"日" << setw( SpaceSize ) <<"一" << setw( SpaceSize ) <<"二" ;
	cout << setw( SpaceSize ) <<"三" << setw( SpaceSize ) <<"四" << setw( SpaceSize ) <<"五" ;
	cout << setw( SpaceSize ) <<"六" << endl;
	
	
	for( i = 1 ; i <= Excur ; i++)
		cout<< setw( SpaceSize ) << " "  ;    //首先输出 多余的空格
	TotalDay = GetMonthDayNumber( year , month );
	
	
	for( i = 1 ; i <= TotalDay ; i++){   // 然后自加,当然长度大于七的时候换行
		cout << setw( SpaceSize ) << i ;
		if( 0 == ( i + Excur ) % 7 )     //换行条件
			cout<< endl;
	} 
	cout << endl ;
	return 0;
}
int DateUtil::FindDayWeekend(int year , int month ,int day ){   //查找某一天 是星期几
	int TotalDay ;
	char Weekend[ 7 ][ 4 ]={{"日"},{"一"},{"二"},{"三"},{"四"},{"五"},{"六"} } ;
	TotalDay = NowToDay(year,month,day) ;  
	cout << year <<"年 "<<month<<"月 "<<day<<"日 是星期" << Weekend[( TotalDay + 3 ) % 7 ]<<endl;
	return 0;
}
int DateUtil::DisplayYear ( int year ){
	int    i , j, k , n , month1 ,month2 ,month3 ;  // 按照 一个季度输出 日历 month1 month2 month3 是 该月应该有天数
	int  Excur1 , Excur2 , Excur3 ;//  i , j , k 分别表示 三个月的 天数值  Excur1,Excur2,Excur3 表示偏移量
	int  sea; 
	char Months[12][10]={{"一月"},{"二月"},{"三月"},{"四月"},{"五月"},{"六月"},{"七月"},{"八月"},{"九月"},{"十月"},{"十一月"},{"十二月"}};
	SpaceSize = 3 ;
	cout<< year << " 年 " << endl;
	for( sea = 1 ; sea <= 12 ; sea += 3 ){             // 分四个季度显示
		Excur1 = ( NowToDay(year,sea   , 1 ) + 3 ) % 7 ;   // 分别计算 3 个月的偏移量
		Excur2 = ( NowToDay(year,sea+1 , 1 ) + 3 ) % 7 ;
		Excur3 = ( NowToDay(year,sea+2 , 1 ) + 3 ) % 7 ;
		month1 = GetMonthDayNumber( year ,sea );         // 分别计算 3个月的 天数
		month2 = GetMonthDayNumber( year ,sea + 1 );
		month3 = GetMonthDayNumber( year ,sea + 2 );     //显示三个月的 星期
		cout << Months[ sea - 1 ] << setw( 21 ) <<" " <<Months[ sea ] << setw( 20 ) <<" " <<Months[ sea + 1 ] << endl;
		cout << setw( SpaceSize ) <<"日" << setw( SpaceSize ) <<"一" << setw( SpaceSize ) <<"二" ;
		cout << setw( SpaceSize ) <<"三" << setw( SpaceSize ) <<"四" << setw( SpaceSize ) <<"五" ;
		cout << setw( SpaceSize ) <<"六" << setw( SpaceSize ) <<" ";
		
		
		cout << setw( SpaceSize ) <<"日" << setw( SpaceSize ) <<"一" << setw( SpaceSize ) <<"二" ;
		cout << setw( SpaceSize ) <<"三" << setw( SpaceSize ) <<"四" << setw( SpaceSize ) <<"五" ;
		cout << setw( SpaceSize ) <<"六" << setw( SpaceSize ) <<" ";
		
		
		cout << setw( SpaceSize ) <<"日" << setw( SpaceSize ) <<"一" << setw( SpaceSize ) <<"二" ;
		cout << setw( SpaceSize ) <<"三" << setw( SpaceSize ) <<"四" << setw( SpaceSize ) <<"五" ;
		cout << setw( SpaceSize ) <<"六" << endl;
		
		
		for( n = 1 ; n <= Excur1 ; n++)      // 第一行 单独处理  首先输出 多余的空格 然后在输出天数  
			cout<< setw( SpaceSize ) << " "  ;  // i ,j , k 单独保存当前的日期
		for( n  ; n <= 7 ; n++)
			cout<< setw( SpaceSize ) << n - Excur1 ;
		cout<< setw( SpaceSize ) << " "  ;
		i =  n - Excur1 ;
		
		
		for( n = 1 ; n <= Excur2 ; n++)
			cout<< setw( SpaceSize ) << " "  ;
		for( n  ; n <= 7 ; n++)
			cout<< setw( SpaceSize ) << n - Excur2 ;
		cout<< setw( SpaceSize ) << " "  ;
		j = n - Excur2 ;
		
		
		for( n = 1 ; n <= Excur3 ; n++)
			cout<< setw( SpaceSize ) << " "  ;
		for( n  ; n <= 7 ; n++)
			cout<< setw( SpaceSize ) << n - Excur3 ;
		cout<< endl ;
		k = n - Excur3 ;
		
		
		while (  i <= month1 || j <= month2 || k <= month3){  // 当三个月的日期都大于 应有的天数后  循环结束
			for( n = i  ; i < n + 7; i++)                    // 接下来的 7 天输出一次。但是 大于天数的部分输出空格
				if( i <= month1 )
					cout<< setw( SpaceSize )<< i ;
				else
					cout<< setw( SpaceSize )<<" " ;
				cout<< setw( SpaceSize )<<" " ;
				
				
				for( n = j  ; j < n + 7; j++)
					if( j <= month2 )
						cout<< setw( SpaceSize )<< j ;
					else
						cout<< setw( SpaceSize )<<" " ;
					cout<< setw( SpaceSize )<<" " ;
					
					
					for( n = k  ; k < n + 7; k++)
						if( k <= month3 )
							cout<< setw( SpaceSize )<< k ;
						else
							cout<< setw( SpaceSize )<<" " ;
						cout<< endl; 
		}
		cout<< endl;
	}
	cout << endl;
	return 0;
}






Date::Date(int _year, int _month, int _day): year(_year), month(_month), day(_day)
{
	
	
}


void Date::print()
{
	cursorPos.X   =   0;   
	cursorPos.Y   =   0;  
	SetConsoleCursorPosition(   hStdout,   cursorPos   );
	cout<<setfill('0')<<setw(4)<<this->year<<"年";
	cout<<setfill('0')<<setw(2)<<this->month<<"月";
	cout<<setfill('0')<<setw(2)<<this->day<<"日"<<endl;
}


void Date::Increment()
{
	int max_day = g_dateUtil.GetMonthDayNumber(year, month);
	if(day == max_day)
	{
		day = 1;
		if(month == 12)
		{
			month = 1;
			++year;
		}
		else
		{
			++month;
		}
	}
	else
	{
		++day;
	}
}




DateTime::DateTime(int _year, int _month, int _day, int _hour,int _minute,int _second)
:Date(_year, _month, _day), Time(_hour, _minute, _second)
{
	country = "Green";
	diff = 0;
}


void DateTime::Increment()
{
	if(hour == 23)
	{
		Time::Increment();
		if(hour == 0)
		{
			Date::Increment();
		}
	}
	else
	{
		Time::Increment();
	}
}


void DateTime::print()
{
	Time::print();
	Date::print();
	cursorPos.X   =   0;   
	cursorPos.Y   =   1;  
	SetConsoleCursorPosition(   hStdout,   cursorPos   );
	cout<<"国家名:"<<country<<"\t与格林威治时间的差:"<<diff<<"h"<<endl;
	
	
}


int Timer::count = 0;


Timer::Timer()
{
}


void Timer::CreateTimerThread(int* pi)
{
	HANDLE hand = CreateThread(NULL,0,Timer::TimeThread,pi,0,NULL);
}


DWORD CALLBACK Timer::TimeThread(PVOID pvoid)
{
	int* pi = (int*)pvoid;
	int itm = *pi;
	MSG msg;
	PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
	UINT timeid = SetTimer(NULL, Event_ID, itm,Timer::TimeProc);
	BOOL bRet;
	while ((bRet = GetMessage(&msg,NULL,0,0))!=0)
	{
		if (bRet == -1)
		{
			cout<<"Error:the thread will quit,error id is "<<GetLastError()<<endl;
			break;
		}
		else
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	KillTimer(NULL,timeid);
	cout<<"thread end here"<<endl;
	return 0;
}

void CALLBACK Timer::TimeProc(HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
	// g_time->Increment();
	// g_time->print();
	// g_date->print();
	g_DateTime->Increment();
	g_DateTime->print();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值