1. 程式人生 > >C++程式設計語言練習 10.2 一個簡單的date類

C++程式設計語言練習 10.2 一個簡單的date類

程式碼如下:

標頭檔案Date.h

#pragma once
#include <string>
#include<sstream>
using std::string;
using std::stringstream;

class Date
{
public:
	enum Month{ jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec };
	
	// constructed function
	Date();
	Date(int d,Month m,int y);
	Date(const Date&d);
	Date& operator=(const Date&d);

	// get the date
	int get_day()const;
	Month get_month()const;
	int get_year()const;
	string string_rep() const;
	void char_rep(char s[])const;

	// relation calc
	bool operator==(const Date &d)const;
	bool operator!=(const Date &d)const;
	bool operator>(const Date &d)const;
	bool operator<(const Date &d)const;
	bool operator<=(const Date &d)const;
	bool operator>=(const Date &d)const;

	// increase
	Date &operator++(int);
	Date &operator++();
	Date &operator--(int);
	Date &operator--();

	// assignment
	Date& operator+=( int n);
	Date& operator-=( int n);

	Date& operator+( int n);
	Date& operator-( int n);

protected:
	static void set_default(int d, Month m, int y);
	void IncDay();
	bool IsEndOfMonth()const;
	void IncMonth();

	void DecMonth();
	void DecDay();
	int  GetDayOfMonth()const;
	// exception class
	class Bad_date{};
private:
	int day_,  year_;
	Month month_;
	static Date default_date;

};

bool IsLeapYear(int y);
實現date.cpp
#include "stdafx.h"
#include "Date.h"

#include <iostream>
using namespace std;



Date Date::default_date(1, jan, 1970);
void Date::set_default(int d, Month m, int y)
{
	default_date = Date(d, m, y);
}

// constucted function ///////////////////////////////
Date::Date()
{
	*this = default_date;
}

Date::Date(int d,Month m,int y)
{
	if (d == 0) d = default_date.get_day();
	if (m == 0) m = default_date.get_month();
	if (y == 0) y = default_date.get_year();
	
	int max;
	switch (m)
	{
	case feb:
		max = 29 + IsLeapYear(y);
		break;
	case apr:case jun:case sep:case nov:
		max = 30;
		break;
	case jan:case may:case jul:case aug:case oct:case dec:
		max = 31;
		break;
	default:
		throw Bad_date();
	}

	if (d < 1 || max < d)throw Bad_date();

	day_ = d;
	month_ = m;
	year_ = y;
}
// 複製建構函式
Date::Date(const Date&d)
{
	day_ = d.get_day();
	month_ = d.get_month();
	year_ = d.get_year();
}
// 賦值建構函式
Date& Date::operator=(const Date&d)
{
	if (*this != d)
	{
		year_ = d.get_year();
		month_ = d.get_month();
		day_ = d.get_day();
	}
	return *this;
}
// get the date /////////////////////////////////
int Date::get_day() const
{
	return day_;
}
Date::Month Date::get_month() const
{
	return month_;
}
int Date::get_year() const
{
	return year_;
}
string Date::string_rep() const
{
	string s,st;
	stringstream ss;
	ss << this->year_;
	ss>>st;
	s += st;
	s += '/';

	ss.clear();
	ss << this->month_;
	ss >> st;
	s += st;
	s += '/';

	ss.clear();
	ss << this->day_;
	ss >> st;
	s += st;
	return s;
}

void Date::char_rep(char s[]) const
{
	string sdate = string_rep();
	strcpy_s(s, sdate.length(), sdate.c_str());
}

int Date::GetDayOfMonth() const
{
	int day = 0;

	switch (this->month_)
	{
	case jan: day = 31; break;
	case feb: 	
		if (month_ == feb && IsLeapYear(year_))
			day = 29;
		else
			day = 28;
		break;
	case mar: day = 31; break;
	case apr: day = 30; break;
	case may: day = 31; break;
	case jun: day = 30; break;
	case jul: day = 31; break;
	case aug: day = 31; break;
	case sep: day = 30; break;
	case oct: day = 31; break;
	case nov: day = 30; break;
	case dec: day = 31; break;
	default:
		throw Bad_date();
		break;
	}

	return day;
}


// relation calc   ////////////////////////////////////
bool Date::operator==(const Date &d) const
{
	return year_ == d.year_ &&
		month_ == d.month_ &&
		day_ == d.day_;
}
bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}

bool Date::operator>(const Date& d) const
{
	if (year_ > d.year_)
	{
		return true;
	}
	if (year_ == d.year_)
	{
		if (month_ > d.month_)
		{
			return true;
		}
		if (month_ == d.month_)
		{
			if (day_ > d.day_)
				return true;
		}
	}
	return false;
}

bool Date::operator>=(const Date&d)const
{
	return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d) const
{
	return !(*this>=d);
}
bool Date::operator<=(const Date& d) const
{
	return !(*this>d);
}

// increase calc //////////////////////////////////
bool Date::IsEndOfMonth() const
{
	if (month_ == feb && IsLeapYear(year_))
		return day_ == 29;
	else		
		return day_ == GetDayOfMonth();
}

void Date::IncMonth()
{
	switch (this->month_)
	{
	case jan:
		this->month_ = feb;
		break;
	case feb:
		this->month_ = mar;
		break;
	case mar:
		this->month_ = apr;
		break;
	case apr:
		this->month_ = may;
		break;
	case may:
		this->month_ = jun;
		break;
	case jun:
		this->month_ = jul;
		break;
	case jul:
		this->month_ = aug;
		break;
	case aug:
		this->month_ = sep;
		break;
	case sep:
		this->month_ = oct;
		break;
	case oct:
		this->month_ = nov;
		break;
	case nov:
		this->month_ = dec;
		break;
	case dec:
		this->month_ = jan;
		break;
	default:
		throw Bad_date();
		break;
	}
}

void Date::IncDay()
{
	if (IsEndOfMonth())
	{
		if (month_ == dec) // 如果是12月最後一天,則加1年
		{
			day_ = 1; month_ = jan; year_++;
		}
		else // 如果是本月最後一天,則加1月
		{
			day_ = 1; IncMonth();
		}
	}
	else
		day_++;
}

void Date::DecMonth()
{
	switch (this->month_)
	{
	case feb:
		this->month_ = jan;
		break;
	case mar:
		this->month_ = feb;
		break;
	case apr:
		this->month_ = mar;
		break;
	case may:
		this->month_ = apr;
		break;
	case jun:
		this->month_ = may;
		break;
	case jul:
		this->month_ = jun;
		break;
	case aug:
		this->month_ = jul;
		break;
	case sep:
		this->month_ = aug;
		break;
	case oct:
		this->month_ = sep;
		break;
	case nov:
		this->month_ = oct;
		break;
	case dec:
		this->month_ = nov;
		break;
	case jan:
		this->month_ = dec;
		break;
	default:
		throw Bad_date();
		break;
	}
}

void Date::DecDay()    // dec one day
{
	if (day_ == 1)
	{
		if (month_ == jan) // 如果是1月,則變為12月,並且減1年
		{
			month_ = dec; year_--; day_ = GetDayOfMonth();
		}
		else // 否則減1月
		{
			DecMonth(); day_ = GetDayOfMonth();
		}
	}
	else
		day_--;
}




Date& Date::operator++(int)
{
	IncDay();
	return *this;
}
Date& Date::operator++()
{
	IncDay();
	return *this;
}

Date& Date::operator--(int)
{
	DecDay();
	return *this;
}

Date& Date::operator--()
{
	DecDay();
	return *this;
}


Date& Date::operator+=( int n)
{
	for (int i = 0; i < n; i++)
		IncDay();
	return *this;
}

Date& Date::operator-=( int n)
{
	for (int i = n; i > 0; i--)
		DecDay();
	return *this;
}

Date& Date::operator+( int n)
{
	for (int i = 0; i < n; i++)
		IncDay();
	return *this;
}
Date& Date::operator-( int n)
{
	for (int i = n; i < 0; i--)
		DecDay();
	return *this;
}



// frend fun  //////////////////////////////////////
bool IsLeapYear(int y) // y == 0 ??? 
{
	if (y % 400 == 0 || y % 100 != 0 && y % 4 == 0)
	{
		return true;
	}
	return false;
}


相關推薦

C++程式設計語言練習 10.2 一個簡單date

程式碼如下: 標頭檔案Date.h #pragma once #include <string> #include<sstream> using std::string; using std::stringstream; class Date {

C程式設計語言(第2版)簡單讀書筆記

最近重新看了C語言聖經,查漏補缺,記了簡單的筆記,全部來自原書,共9866字,記錄一下。  騰訊文件地址:https://docs.qq.com/doc/DUmt5VU5Tem1LQUxx 第一章 導言   c語言中一個通用的例項:在允許使用某種型別變數值的任何場合,都可以使用該型別的更復雜的

C++程式設計語言練習4.6 區域性名字和外部名字的長度限制

書中程式碼,利用模板副檔名字長度#include <iostream> #include <typeinfo> template<typename T,typename U> struct Doublify{}; template&l

c程式設計語言 2-4 2-5

第五彈! 第五彈!第五彈! #include <stdio.h> #define max 1000 void InputSpring(char s[]); //void mySqueeze(char s1[],char s2[]); void any(char s

C++程式設計語言》7.10_9 簡單C++加密程式

/*-------------------------------------------------- 寫一個加密程式, 它從cin讀入, 並將編碼後的字元序列寫到cout. 你 可以採用如下的簡單

C程式設計語言練習 3-1

練習 3-1 在上面有關折半查詢的例子中,while 迴圈語句內共執行了兩次測試,其實 只要一次就足夠(代價是將更多的測試在迴圈外執行)。重寫該函式,使得在迴圈內部只執行 一次測試。比較兩種版本函式的執行時間。 原文程式碼有一個問題,下面是改過的原文

C語言回撥函式一個簡單的例子

原文地址:http://blog.csdn.net/zgrjkflmkyc/article/details/9198519 回撥函式在linux核心或是微控制器上應用得太多,由此也可以大致判斷,一個初學者和有個有經驗的區別。我轉寫在這兒,希望更多的人能夠看到,一起進步!!

C語言練習篇-2交換兩個變數的值

練習:交換兩個變數的值。 方法一:中間變數 #include <stdio.h> int main() { int a=10; int b=4; printf("

c 程式設計語言 第二版 練習題 5-2

double getfloat(double *pn) { if (pn == NULL) return -1; int c, sign, flag=1; while (isspace(c = getchar())); if (!isdigit(c) &

C程式設計語言練習3-5

問題描述   練習 3-5 編寫函式 itob(n, s, b),將整數n轉換為以b為底的數,並將轉換結果以字元的形式儲存到字串s中。例如,itob(n, s, 16)把整數n格式化成十六進位制整數儲存在s中。   Write the function itob(n,s,b) that c

Part10 泛型程序設計與C++標準模板庫 10.2叠代器

main inf 數據 序列 3.3 距離 結果 示例 res 叠代器是算法和容器的橋梁   叠代器用作訪問容器中的元素   算法不直接操作容器中的數據,而是通過叠代器間接操作算法和容器獨立   增加新的算法,無需影響容器的實現   增加新的容器,原有的算法也能適用 輸

Win8 Metro(C#)數字圖像處理--2.56簡單統計法圖像二值化

public 分割 ola 0.11 orm http statistic weight segment 原文:Win8 Metro(C#)數字圖像處理--2.56簡單統計法圖像二值化

scrapy框架系列 (2) 一個簡單案例

com 必須 res 逗號 大致 繼承 中文 append .sh 學習目標 創建一個Scrapy項目 定義提取的結構化數據(Item) 編寫爬取網站的 Spider 並提取出結構化數據(Item) 編寫 Item Pipelines 來存儲提取到的Item(即結構化數據

C++程式設計2018年10月考試設計題程式碼參考

設計一個圓類Circle和一個桌子類Table,類Circle包含私有資料成員半徑radius和求圓面積的成員函式getarea(),類Table包含私有資料成員高度height和成員函式getheight(),另設計一個圓桌類Roundtable是類Circle和類Table兩個類的派生,私有資料

C++程式設計練習程式碼筆記

從文字檔案old.txt讀取字元,將其中的數字字元‘0’、‘1’、‘2’、‘3’、‘4’、‘5’、‘6’、‘7’、‘8’、‘9’ 分別用英文字母字元‘Z’、‘Y’、‘X’、‘W’、‘V’、‘U’、‘T’、‘S’、‘R’、‘Q’替換,其餘字元不變,結果寫入文字檔案new.txt,並分別將兩個

C程式設計語言(六)UNIX系統介面

系統介面和標準的C庫,是不同的兩個範疇。 標準C庫,各個平臺的C編譯器都應該支援,具有跨平臺的性質。 系統介面則是系統相關的,UNIX的系統介面,在Win上就肯定不能用。 那麼兩者是不是還有聯絡呢,其實是的。標準C庫,跟系統相關的部分,不也是通過系統呼叫/系統介面實現的麼。所以系

tomcat學習(2) 一個簡單的servlet容器

servlet容器 2.1 javax.servlet.Servlet介面     servlet程式設計需要使用到java.servlet和javax.servlet.http兩個包下的介面和類,在所有的類和介面中,java.servlet.servlet介面是最重要的,所

C++程式設計語言 計算器原始碼

首先是課本最早出現的原始碼,可以執行 #include "pch.h" #include <iostream> #include<string> #include<cctype> #include<map> #include&

C++程式設計語言》學習筆記1-容器

容器 容器名 資料結構 vector<T> 可變大小向量 list<T> 雙向連結串列 forward_list<T> 單向連結串列 deque<T> 雙端佇列 set<T&g

C程式設計筆記2018.10.15

構成C語言程式的基本單位是() 一個C程式的執行是從____開始,到____結束。 判斷 C程式中,main函式必須位於程式的最前面? C程式中大、小寫字母是有區別的? C程式的每行只能寫一條語句? C程式中,若一條語句較長,也不能分寫在下一行上? C程式中,