溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

類實現(xiàn)一個簡單的日期計算器

發(fā)布時間:2020-07-04 16:32:59 來源:網(wǎng)絡 閱讀:343 作者:稻草陽光L 欄目:開發(fā)技術

  作為一個程序員,對于時間的概念已經(jīng)退化到了三歲小孩水平,常常會醉心于寫一個程序忘記了時間,一個下午,一天,甚至一個星期就過去了。對于一個剛入程序員大門的我來說,時光真的是匆匆溜走,所以經(jīng)常會百度一個日期計數(shù)器,算今天到那些特別的日子還有多少天。用多了后就覺得現(xiàn)在儲備的編程知識可以去實現(xiàn)一個簡單的日期計算器了。所以就寫了這篇博客給大家分享一下。

  首先,得設計這個日期類,一個日期類應該具有私有數(shù)據(jù)成員應該有年Year,月month,日day。在這我們就不精確到時分秒了。

#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year, int month, int day);
	~Date();
	Date(const Date& a);
	void ShowDate();
	bool operator==(const Date& d);
	bool operator<(const Date& d);
	bool operator<=(const Date& d);
	bool operator>(const Date& d);
	bool operator>=(const Date& d);


	Date operator+ (int day);
	Date& operator+= (int day);
	Date operator- (int day);
	Date& operator-= (int day);
	Date& operator++();
	Date operator++(int);
	Date& operator--();
	Date operator--(int);
	int operator-(const Date& d);
	friend ostream* operator<<(ostream* out, const Date &d);

private:
	int GetMonthDay(int year, int month);
	bool IsLeapYear(int year);
private:
	int _year;
	int _month;
	int _day;
};

  可以看到,一個日期類應該具有的功能大致上都有了。對于GetMonthDay()和IsLeapYear()是類內部的函數(shù),只允許在類內調用,不想讓類外的對象使用,所以設為了私有成員函數(shù)。

 其實大部分的成員函數(shù)實現(xiàn)起來較簡單,在此我就只挑出重要且易錯的函數(shù)討論一下。

(一)構造函數(shù)

  一個類的構造函數(shù)很重要,要是構造函數(shù)沒有寫好,那后果不堪設想。

Date(int year, int month, int day)
	{
		if (year < 1900
			|| month>12 || month < 1
			|| day<1
			|| day>GetMonthDay(year, month))
		{
			cout << "初始化錯誤,日期重置為:1900-1-1" << endl;
			_year = 1900;
			_month = 1;
			_day = 1;
		}
		else
		{
			_year = year;
			_month = month;
			_day = day;
		}
	}

  設計一個程序要設計的讓使用者使用起來良好,符合大眾認知。國際的標準公歷都是從1900-1-1開始計時的。所以初始化時要處理一下不能小于199-1-1.

(二)GetMonthDay()和IsLeapYear()

  要使一個程序的可讀性和健壯性提高,我們要盡可能少寫重復的代碼。我們把經(jīng)常要調用的代碼封裝成一個函數(shù),修改程序時也特別方便。

   此日期類中GetMonthDay()和IsLeapYear()函數(shù)是一個經(jīng)常調用函數(shù),因為要得到一年的天數(shù)就必須先判斷是平年還是閏年,然后得到每個月的天數(shù),然后相加。所以這兩個私有函數(shù)配合起來可以得到某一年的天數(shù)

bool Date::IsLeapYear(int year)
{
	if (year % 4 == 0 && year % 100 != 0
		|| year % 400 == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int Date::GetMonthDay(int year, int month)
{
	int MonthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day = MonthArray[month];
	if (month == 2 && IsLeapYear(year))
	{
		day += 1;
	}
	return day;
}

  這里GetMonthDay()使用了一種簡單的方法來確定某年某月的天數(shù),就是利用一個數(shù)組來存每個月的天數(shù),數(shù)組的下標就表示月份,所以數(shù)組大小為13,下表為0的不用,沒有0月。。。。然后就判斷是平年還是閏年,如果是閏年只要把多的那一天加上就行了。

 (三)>,<,>=,<= 都可以歸為一類

    只要實現(xiàn)了<其他三個都可以用<去實現(xiàn)。

bool Date::operator<(const Date& d)
{
	if (this->_year>d._year)
		return false;
	else if (this->_year < d._year)
		return true;
	else
	{
		if (this->_month > d._month)
			return false;
		else if (this->_month < d._month)
			return true;
		else
		{
			if (this->_day > d._day||this->_day==d._day)
				return false;
			else
				return true;

		}
	}
}

(四)+,-,+=,-=歸為一類

  實現(xiàn)+=和-=,+和-可以根據(jù)他們實現(xiàn)

Date& Date::operator+= (int day)
{
	if (day<0)
	{
		return (*this -= (-day));
	}
	else
	{
		this->_day += day;
		while (this->_day > GetMonthDay(this->_year, this->_month))
		{
			this->_day -= GetMonthDay(this->_year, this->_month);
			if (this->_month < 12)
			{
				this->_month++;
			}
			else
			{
				this->_year++;
				this->_month %= 12;
			}
		}
		return *this;
	}
}
Date& Date::operator-= (int day)
{
	if (day < 0)
	{
		return (*this += (-day));
	}
	this->_day -= day;
	while (this->_day < 1)
	{
		this->_month --;
		if (this->_month < 1)
		{
			this->_year--;
			this->_month += 12;
			if (this->_year < 1)
			{
				exit(1);
			}
		}
		this->_day += GetMonthDay(this->_year, this->_month);
	}
	return *this;
}

  其實實現(xiàn)起來很簡單的,希望朋友們可以自己去實現(xiàn)一個,提高編程技巧,注重細節(jié)。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI