喜歡的朋友可以關(guān)注收藏一下
本文實(shí)現(xiàn)了一個(gè)date類型,原版要求如下(括號(hào)為超出要求附加):為Date類實(shí)現(xiàn)如下成員:1. 構(gòu)造器劲绪,可以初始化年鞍陨、月、日阻塑。(使用了模板)2. 大于蓝撇、小于、等于(> 陈莽、< 渤昌、==)操作符重載,進(jìn)行日期比較走搁。(>=,<=,==)3. print() 打印出類似 2015-10-1 這樣的格式独柑。(print(變長參數(shù)),printA(字符指針私植,date))然后創(chuàng)建兩個(gè)全局函數(shù):1. 第1個(gè)函數(shù) CreatePoints生成10個(gè)隨機(jī)的Date忌栅,并以數(shù)組形式返回;(自定義了隨機(jī)生成器類class Rand_int)2. 第2個(gè)函數(shù) Sort 對第1個(gè)函數(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
/* 本程序的注釋代表僅代表個(gè)人理解清女,不一定完全正確钱烟,全是我親自敲上去的,如有錯(cuò)誤請聯(lián)系我嫡丙。 */
/* 本程序隨機(jī)數(shù)生成器類和變參數(shù)print()函數(shù)使用了C++11 的標(biāo)準(zhǔn)拴袭,可能有的編譯器不能編譯通過,我會(huì)截圖一下編譯結(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&); //賦值運(yùn)算符重載函數(shù)
private:
T year, month, day; //def 年拥刻,月,日
friend date& __doequ(date*, const date&); //賦值實(shí)際運(yùn)算函數(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)生的值映射到某個(gè)數(shù)學(xué)分布上)
{
public:
Rand_int(int low, int high) : dist{ low, high }
{
;
} //構(gòu)造函數(shù)? 初始化隨機(jī)數(shù)范圍
int operator()(){ return dist(re); } //操作符() 重載 得到一個(gè)隨機(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í)際運(yùn)算函數(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ù)實(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ù)實(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 類似 都是說這個(gè)模板參數(shù)定義的是一個(gè)類蒸眠,而不是變量.class type 和 class... types? ? ? type 和 types 是模式 , ...? 是包擴(kuò)展
void print(const type& x, const types&... next)
{
cout << x << endl;
print(next...);
}
}
/* 日期 賦值(=) 實(shí)際運(yùn)算函數(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運(yùn)算速度慢時(shí)(如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,出錯(cuò)可能性會(huì)大)
{
buff[i].day = rnd_day_30();
}
}
}
/* 函數(shù)名 ooerator==(日期,日期) 判斷兩個(gè)日期是否相等? ? 重載 */
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; //定義反饋?zhàn)兞?/p>
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; //定義反饋?zhàn)兞?/p>
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; //定義反饋?zhàn)兞?/p>
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; //定義反饋?zhàn)兞?/p>
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
/* 本程序的注釋代表僅代表個(gè)人理解,不一定完全正確激况,全是我親自敲上去的作彤,如有錯(cuò)誤請聯(lián)系我。 */
/* 本程序隨機(jī)數(shù)生成器類和變參數(shù)print()函數(shù)使用了C++11 的標(biāo)準(zhǔn)誉碴,可能有的編譯器不能編譯通過宦棺,我會(huì)截圖一下編譯結(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? 而這種方法在程序運(yùn)行過程中可能出現(xiàn)各種錯(cuò)誤黔帕,如不做類型檢查等代咸,所以我采用了宏常量定義const,增加代碼的安全性
using namespace std; //這句話其實(shí)就表示了所有的標(biāo)準(zhǔn)庫函數(shù)都在標(biāo)準(zhǔn)命名空間std中進(jìn)行了定義,其作用就在于避免發(fā)生重命名的問題。(本質(zhì)是把std中定義的函數(shù)成黄、變量呐芥、類等釋放出來)
//using namespace guo; //不能偷懶用這種全局的命名空間聲明,因?yàn)閜rint();在命名空間std和命名空間guo中都有定義逻杖。( 因?yàn)檫@個(gè)聲明把命名空間中的guo釋放出來以后發(fā)現(xiàn)有兩個(gè)print(); 從語法上會(huì)產(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 }; //一般情況下{}和()都可以做初始化操作,而前著明確了要做什么(初始化)思瘟,避免了一些潛在的錯(cuò)誤荸百,當(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); //測試賦值運(yùn)算符重載
cout << "測試賦值運(yùn)算符重載: 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個(gè)日期為:");
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個(gè)日期為:");
for (int i = 0; i < SUM; i++)
{
guo::print(ten_rand_date[i]);
}
guo::print("==運(yùn)算符測試:d1,d2",d1==d2); // 0
guo::print("==運(yùn)算符測試:d1,d1", d1 == d1);// 1
guo::print(">運(yùn)算符測試:d1,d2", d1 > d2); // 0
guo::print(">運(yùn)算符測試:d1,d1", d1 > d1); // 0
guo::print(">運(yùn)算符測試:d2,d1", d2 > d1); // 1
guo::print("<運(yùn)算符測試:d1,d2", d1 < d2); // 1
guo::print("<運(yùn)算符測試:d1,d1", d1 < d1); // 0
guo::print("<運(yùn)算符測試:d2,d1", d2 < d1); // 0
guo::print(">=運(yùn)算符測試:d1,d2", d1 >= d2); // 0
guo::print(">=運(yùn)算符測試:d1,d1", d1 >= d1); // 1
guo::print(">=運(yùn)算符測試:d2,d1", d2 >= d1); // 1
guo::print("<=運(yùn)算符測試:d1,d2", d1 <= d2); // 1
guo::print("<=運(yùn)算符測試:d1,d1", d1 <= d1);? ? // 1
guo::print("<=運(yùn)算符測試:d2,d1", d2 <= d1); // 0
system("pause");
return 0;
}