博覽網(wǎng)_編程_C++_第一周_Date類的實現(xiàn)

喜歡的朋友可以關(guān)注收藏一下

本文實現(xiàn)了一個date類型,原版要求如下(括號為超出要求附加):為Date類實現(xiàn)如下成員:1. 構(gòu)造器,可以初始化年、月、日。(使用了模板)2. 大于、小于、等于(> 、< 、==)操作符重載,進(jìn)行日期比較。(>=,<=,==)3. print() 打印出類似 2015-10-1 這樣的格式。(print(變長參數(shù)),printA(字符指針,date))然后創(chuàng)建兩個全局函數(shù):1. 第1個函數(shù) CreatePoints生成10個隨機(jī)的Date,并以數(shù)組形式返回;(自定義了隨機(jī)生成器類class Rand_int)2. 第2個函數(shù) Sort 對第1個函數(shù)CreatePoints生成的結(jié)果,將其按照從小到大進(jìn)行排序。 (快速排序算法QuickSort)最后在main函數(shù)中調(diào)用CreatePoints,并調(diào)用print將結(jié)果打印出來。然后調(diào)用Sort函數(shù)對前面結(jié)果處理后,并再次調(diào)用print將結(jié)果打印出來。

在此之前先放一下完整版的代碼

// 本文件名? date.h 我的編程環(huán)境VS2013

/* 本程序的注釋代表僅代表個人理解,不一定完全正確,全是我親自敲上去的,如有錯誤請聯(lián)系我。 */

/* 本程序隨機(jī)數(shù)生成器類和變參數(shù)print()函數(shù)使用了C++11 的標(biāo)準(zhǔn),可能有的編譯器不能編譯通過,我會截圖一下編譯結(jié)果 */

#ifndef __DATE_H__

#define __DATE_H__

#include

#include //c++ 11? 里面不少隨機(jī)數(shù)生成器的引擎和分布只支持c++ 11標(biāo)準(zhǔn),部分編譯器不支持

using namespace std;

template //def template typename is T

class date

{

public:

date(T y = 0, T m = 0, T d = 0) : year(y), month(m), day(d)

{

;

}

T year_fun()? const { return year; }

T month_fun()? const { return month; }

T day_fun()? ? const { return day; }

date& operator = (const date&); //賦值運算符重載函數(shù)

private:

T year, month, day; //def 年,月,日

friend date& __doequ(date*, const date&); //賦值實際運算函數(shù)

friend void CreatePoints(date *buff, const int& n);

};

class Rand_int //等概率整數(shù)的隨機(jī)數(shù)生成器 隨機(jī)生成器由引擎(負(fù)責(zé)生成一組隨機(jī)值或者偽隨機(jī)數(shù))和一種分布(負(fù)責(zé)把引擎產(chǎn)生的值映射到某個數(shù)學(xué)分布上)

{

public:

Rand_int(int low, int high) : dist{ low, high }

{

;

} //構(gòu)造函數(shù)? 初始化隨機(jī)數(shù)范圍

int operator()(){ return dist(re); } //操作符() 重載 得到一個隨機(jī)int

private:

default_random_engine re; //默認(rèn)隨機(jī)引擎

uniform_int_distribution<> dist;//分布:生成的所有整數(shù)概率相等

};

/* 函數(shù)聲明區(qū) */

//inline date printA(const char *a, const date& x);

inline date& __doequ(date* ths, const date& r); //賦值實際運算函數(shù)

/************************************************************************************************************/

/* 對 cout<< date 類型的重載? ? ostream 是 cout的類 */

ostream& operator<<(ostream& os, const date& x)

{

return os << x.year_fun() << '-' << x.month_fun() << '-' << x.day_fun();

}

/* print函數(shù)實現(xiàn)方法一(字符串類型輸出,date類型的輸出) */

inline void

printA(const date& x)

{

cout << x << endl;

}

inline void

printA(const char *a,const date& x)

{

cout << a << x << endl;

}

/**************************************************************/

/* print函數(shù)實現(xiàn)方法二 */

//void printX() {

//

//}

//

//template

//void printX(const T1& firstArg, const Types&... args)

//{

// cout << firstArg << endl;

// printX(args...);

//}

namespace guo{ //把函數(shù)封裝到命名空間guo ,防止里面的函數(shù)、變量、類、對象重復(fù)定義

void print()

{

;

}

template //class 的定義和 typename 類似 都是說這個模板參數(shù)定義的是一個類,而不是變量.class type 和 class... types? ? ? type 和 types 是模式 , ...? 是包擴(kuò)展

void print(const type& x, const types&... next)

{

cout << x << endl;

print(next...);

}

}

/* 日期 賦值(=) 實際運算函數(shù) */

inline date&

__doequ(date* ths, const date& r)

{

ths->year = r.year;

ths->month = r.month;

ths->day = r.day;

return *ths;

}

/* 函數(shù)名 ooerator= 日期賦值(=) */

inline date&

date::operator=(const date& r)

{

return __doequ(this, r);

}

/**************************************************************/

void CreatePoints(date *buff,const int& n)

{

Rand_int rnd_year{ 1, 2100 }, rnd_month{ 1, 12 }, rnd_day_28{ 1, 28 }, rnd_day_29{ 1, 29 }, rnd_day_30{ 1, 30 }, rnd_day_31{ 1, 31 };

for (int i = 0; i < n; i++)

{

buff[i].year = rnd_year();

buff[i].month = rnd_month();

if (buff[i].month == 2) //2月份

{

if (buff[i].year % 400 == 0 || (buff[i].year % 4 == 0 && buff[i].year % 100 != 0)) //是瑞年

{

buff[i].day = rnd_day_29();

}

else? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //不是瑞年

{

buff[i].day = rnd_day_28();

}

}

else if (buff[i].month == 1 || buff[i].month == 3 || buff[i].month == 5 || buff[i].month == 7 || buff[i].month == 8 || buff[i].month == 10 || buff[i].month ==12 ) //31天月份 (else減少判斷次數(shù),在cpu運算速度慢時(如89c51單片機(jī)),如果判斷過多甚至可能卡死現(xiàn)象)

{

buff[i].day = rnd_day_31();

}

else if (buff[i].month == 4 || buff[i].month == 6 || buff[i].month == 9 || buff[i].month == 11) //30天月份 (如果直接else,出錯可能性會大)

{

buff[i].day = rnd_day_30();

}

}

}

/* 函數(shù)名 ooerator==(日期,日期) 判斷兩個日期是否相等? ? 重載 */

inline bool

operator==(const date& x, const date& y)

{

return? x.year_fun() == y.year_fun() && x.month_fun() == y.month_fun() && x.day_fun()==y.day_fun();

}

/* 函數(shù)名 ooerator>(日期,日期) 判斷 x日期 是否大于 y日期? ? 重載 */

bool operator>(const date& x, const date& y)

{

bool feedback; //定義反饋變量

if (x.year_fun() > y.year_fun())

{

feedback = 1;

}

else if (x.year_fun() < y.year_fun())

{

feedback = 0;

}

else if (x.year_fun() == y.year_fun())

{

if (x.month_fun() > y.month_fun())

{

feedback = 1;

}

else if (x.month_fun() < y.month_fun())

{

feedback = 0;

}

else if (x.month_fun() == y.month_fun())

{

if (x.day_fun() > y.day_fun())

{

feedback = 1;

}

else if (x.day_fun() < y.day_fun())

{

feedback = 0;

}

else if (x.day_fun() == y.day_fun())

{

feedback = 0;

}

}

}

return feedback;

}

/* 函數(shù)名 ooerator>=(日期,日期) 判斷 x日期 是否大于等于 y日期? ? 重載 */

bool operator>=(const date& x, const date& y)

{

bool feedback; //定義反饋變量

if (x.year_fun() >= y.year_fun())

{

feedback = 1;

}

else if (x.year_fun() < y.year_fun())

{

feedback = 0;

}

return feedback;

}

/* 函數(shù)名 ooerator<(日期,日期) 判斷 x日期 是否小于 y日期? ? 重載 */

bool operator<(const date& x, const date& y)

{

bool feedback; //定義反饋變量

if (x.year_fun() > y.year_fun())

{

feedback = 0;

}

else if (x.year_fun() < y.year_fun())

{

feedback = 1;

}

else if (x.year_fun() == y.year_fun())

{

if (x.month_fun() > y.month_fun())

{

feedback = 0;

}

else if (x.month_fun() < y.month_fun())

{

feedback = 1;

}

else if (x.month_fun() == y.month_fun())

{

if (x.day_fun() > y.day_fun())

{

feedback = 0;

}

else if (x.day_fun() < y.day_fun())

{

feedback = 1;

}

else if (x.day_fun() == y.day_fun())

{

feedback = 0;

}

}

}

return feedback;

}

/* 函數(shù)名 ooerator<=(日期,日期) 判斷 x日期 是否小于等于 y日期? ? 重載 */

bool operator<=(const date& x, const date& y)

{

bool feedback; //定義反饋變量

if (x.year_fun() > y.year_fun())

{

feedback = 0;

}

else if (x.year_fun() <= y.year_fun())

{

feedback = 1;

}

return feedback;

}

/* 快速排序算法QuickSort */

int partition(date *arr, int low, int high) //快速排序一次劃分算法partition

{

date key;

key = arr[low];

while (low

{

while (low = key) //右側(cè)掃描

{

high--;

}

if (low < high)

{

arr[low++] = arr[high]; //將較小的記錄交換到前面

}

while (low < high && arr[low] <= key) //左側(cè)掃描

{

low++;

}

if (low < high)

{

arr[high--] = arr[low]; //將較大的記錄交換到后面

}

}

arr[low] = key;

return low; //low為軸值記錄的最終位置

}

void sort(date *arr, int start, int end) //快速排序算法QuickSort

{

int pos;

if (start

{

pos = partition(arr, start, end); //一次劃分,pos為軸值得最終位置

sort(arr, start, pos - 1); //遞歸地對左側(cè)子序列進(jìn)行快速排序

sort(arr, pos + 1, end); //遞歸地對右側(cè)子序列進(jìn)行快速排序

}

}

/****************************************************************************************************/

#endif

// 本文件名? date.cpp 我的編程環(huán)境VS2013

/* 本程序的注釋代表僅代表個人理解,不一定完全正確,全是我親自敲上去的,如有錯誤請聯(lián)系我。 */

/* 本程序隨機(jī)數(shù)生成器類和變參數(shù)print()函數(shù)使用了C++11 的標(biāo)準(zhǔn),可能有的編譯器不能編譯通過,我會截圖一下編譯結(jié)果 */

#include

#include"date.h"

#include

#include //c++ 11? 里面不少隨機(jī)數(shù)生成器的引擎和分布只支持c++ 11標(biāo)準(zhǔn),部分編譯器不支持

const int SUM = 10; //C語言中常用的方法是 #define SUM 10? 而這種方法在程序運行過程中可能出現(xiàn)各種錯誤,如不做類型檢查等,所以我采用了宏常量定義const,增加代碼的安全性

using namespace std; //這句話其實就表示了所有的標(biāo)準(zhǔn)庫函數(shù)都在標(biāo)準(zhǔn)命名空間std中進(jìn)行了定義,其作用就在于避免發(fā)生重命名的問題。(本質(zhì)是把std中定義的函數(shù)、變量、類等釋放出來)

//using namespace guo; //不能偷懶用這種全局的命名空間聲明,因為print();在命名空間std和命名空間guo中都有定義。( 因為這個聲明把命名空間中的guo釋放出來以后發(fā)現(xiàn)有兩個print(); 從語法上會產(chǎn)生歧義 )

int main(void)

{

//date d1(2017, 4, 15);

//date d2(2017, 4, 16);

//date d3(2017, 4, 17);

date d1{ 2017, 4, 15 }; //一般情況下{}和()都可以做初始化操作,而前著明確了要做什么(初始化),避免了一些潛在的錯誤,當(dāng)然這里()也是可以的。

date d2{ 2017, 4, 16 };

date d3{ 2017, 4, 17 };

//default_random_engine e; //隨機(jī)數(shù)方法一 默認(rèn)隨機(jī)引擎

//uniform_int_distribution dist(1,10000); //分布:生成的所有整數(shù)概率相等? (范圍1——10000)

//int rand1 = dist(e); ////獲取偽隨機(jī)數(shù)

date ten_rand_date[SUM];

CreatePoints(ten_rand_date,SUM);

cout << "d1: " << d1 << endl;

d1 = date(2015, 10, 1); //測試賦值運算符重載

cout << "測試賦值運算符重載: d1 = date(2015, 10, 1); d1:" << d1 << endl;

printA(d1); //printA函數(shù)? 使用? 方法一(date類型的輸出)

printA("(使用自定義printA) d1:", d1); //printA函數(shù)? 使用? 方法一(字符串類型輸出,date類型的輸出)

guo::print("(guo::使用自定義print):", d1, d2, d3); //print函數(shù)? 使用 方法二 (支持任意類型且參數(shù)變長)? ( 引用命名空間guo中的print()函數(shù),這樣可以和命名空間std中的print();? 起到區(qū)分作用 )

guo::print("隨機(jī)創(chuàng)建的10個日期為:");

for (int i = 0; i < SUM; i++)

{

guo::print(ten_rand_date[i]);

}

sort(ten_rand_date, 0, SUM - 1); //排序函數(shù)? (快速排序算法QuickSort)

guo::print("排序后的10個日期為:");

for (int i = 0; i < SUM; i++)

{

guo::print(ten_rand_date[i]);

}

guo::print("==運算符測試:d1,d2",d1==d2); // 0

guo::print("==運算符測試:d1,d1", d1 == d1);// 1

guo::print(">運算符測試:d1,d2", d1 > d2); // 0

guo::print(">運算符測試:d1,d1", d1 > d1); // 0

guo::print(">運算符測試:d2,d1", d2 > d1); // 1

guo::print("<運算符測試:d1,d2", d1 < d2); // 1

guo::print("<運算符測試:d1,d1", d1 < d1); // 0

guo::print("<運算符測試:d2,d1", d2 < d1); // 0

guo::print(">=運算符測試:d1,d2", d1 >= d2); // 0

guo::print(">=運算符測試:d1,d1", d1 >= d1); // 1

guo::print(">=運算符測試:d2,d1", d2 >= d1); // 1

guo::print("<=運算符測試:d1,d2", d1 <= d2); // 1

guo::print("<=運算符測試:d1,d1", d1 <= d1);? ? // 1

guo::print("<=運算符測試:d2,d1", d2 <= d1); // 0

system("pause");

return 0;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容