閱讀704 返回首頁    go 阿裏雲 go 技術社區[雲棲]


當前時間的下一秒

#include <iostream>
#include <cassert>
using namespace std;

class CTime
{
private:
	int year;
	int month;
	int day;
	int hour;
	int minute;
	int second;
public:

	//Judging the input datas whether is validate.
	void validate() const;

	//input datas to the CTime's data members
	void input();

	//count the current time's next second time
	void nextSecond();

	//count the current time's next 100 days' time 
	void next100days();

	//output datas
	void output() const;

	//carry bit for minute if minute is bigger than sixty
	void minuteAdvance();

	//carry bit for second if second is bigger than sixty
	void secondAdvance();

	//carry bit for hour if hour is bigger than twenty-four
	void hourAdvance();

	//carry bit for month if month can be added one 
	bool dayAdvance();

	//carry bit for year if year can be added one
	void monthAdvance();

	//bool isLearYear() const;
};


///////////////////////////////////////
void CTime::secondAdvance()
{
	if (second == 60)
	{
		second = 0;
        minute += 1;
	}	
}
///////////////////////////////////////
void CTime::minuteAdvance()
{
	if (minute == 60)
	{
		hour += 1;
		minute = 0;
	}
}
////////////////////////////////////////
void CTime::hourAdvance()
{
	if (hour == 24)
	{
		day += 1;
		hour = 0;
	}
}
//////////////////////////////////////////
bool CTime::dayAdvance()
{
	switch (month)
	{
	case 1: case 3: case 5: case 7:
	case 8: case 10: case 12:
		if (day > 31)
		{
			day -= 31;
			month += 1;
			return true;
		}
	case 4: case 6: case 9: case 11:
		if (day > 30)
		{
			day -= 30;
			month +=1;
			return true;
		}
	case 2:
		if ((year % 4 == 0 && year % 100) || (year % 400 == 0))
		{
			if (day > 29)
			{
				day -= 29;
				month += 1;
				return true;
			}
		}
		else 
		{
			if (day > 28)
			{
				day -= 28;
				month += 1;
				return true;
			}
		}
	}
	return false;
}
/////////////////////////////////////////
void CTime::monthAdvance()
{
	if (month >= 12)
	{
		month -= 12;
		year += 1;
	}
}
/////////////////////////////////////////
void CTime::output() const
{
	cout << year << "-" << month << "-" << day << ((hour >= 10) ? " " :  " 0") 
		 << hour << ":" << ((minute >= 10) ? "" :  "0")  << minute << ":" 
		 << ((second >= 10) ? "" :  "0")  << second << endl;
}


void CTime::validate() const
{
	assert(year >= 1);
	assert(month >= 1 && month <= 12);
	assert(day >= 1 && day <= 31);
	assert(hour >= 0 && hour <= 23);
	assert(minute >= 0 && minute <= 59);
	assert(second >= 0 && second <= 59);
}

void CTime::input()
{
	cout << "year month day hour minute second:" << endl;
	cin >> year >> month >> day >> hour >> minute >> second;

	validate();
}

void CTime::nextSecond()
{
	second += 1;
	secondAdvance(); //handle second
    minuteAdvance();//
    hourAdvance();
	dayAdvance();
	monthAdvance();
}

void CTime::next100days()
{
	day += 100;
	while (dayAdvance())
	{
		monthAdvance();
	}	
}

int main()
{	
	CTime time;

	time.input();
	cout << endl;
    CTime temp(time);
///////////////////////////////////////////////
	cout << "next second is :" << endl;
	time.nextSecond();
    time.output();
    cout << endl;

	CTime newTime(temp);
///////////////////////////////////////////////
	newTime.next100days();
	cout << "next 100 days is :" << endl;
	newTime.output();
	cout << endl;
	return 0;
}

最後更新:2017-04-02 15:15:31

  上一篇:go 傳微軟將斥資10億美元推廣Windows 8
  下一篇:go android 懸浮窗口的拖動