首页 > 代码库 > Boolan Homework——Date类

Boolan Homework——Date类

问题描述

为Date类实现如下成员: 
1. 构造器,可以初始化年、月、日。 
2. 大于、小于、等于(> 、< 、==)操作符重载,进行日期比较。 
3. print() 打印出类似 2015-10-1 这样的格式。

然后创建两个全局函数: 
1. 第1个函数 CreatePoints生成10个随机的Date,并以数组形式返回 
2. 第2个函数 Sort 对第1个函数CreatePoints生成的结果,将其按照从小到大进行排序。

最后在main函数中调用CreatePoints,并调用print将结果打印出来。然后调用Sort函数对前面结果处理后,并再次调用print将结果打印出来。

代码实现

date.h头文件

#ifndef _DATE_#define _DATE_#include <iostream>using namespace std;class Date;void Sort(Date* date, const int& n); void CreatePoints(Date* date, const int& n);ostream& operator << (ostream& obj,const Date& date); class Date{    public:        Date (int y = 0, int m = 0, int d = 0) : year(y), month(m), day(d) {}         bool operator == (const Date& date)        {            return year == date.year && month == date.month && day == date.day;        }        bool operator > (const Date& date)        {            if (year != date.year) return year > date.year;            else if (month != date.month) return month > date.month;            else if (day != date.day) return day > date.day;            else return false;        }        bool operator < (const Date& date)        {            if (year != date.year) return year < date.year;            else if (month != date.month) return month < date.month;            else if (day != date.day) return day < date.day;            else return false;        }        void Print()        {            cout << year << ‘-‘ << month << ‘-‘ << day << endl;        }    private:        int year;        int month;        int day;        friend void CreatePoints(Date* date, const int& n);         friend ostream& operator << (ostream& obj,const Date& date);   /*作业没要求,自己重载<<运算符 */ };#endif

  date.cpp实现文件

#include "date.h"#include <iostream>#include <cstdlib>#include <ctime>using namespace std;ostream& operator << (ostream& obj,const Date& date){    return obj << date.year << ‘-‘ << date.month << ‘-‘ << date.day;}void CreatePoints(Date* date, const int& n){    srand(time(NULL));   /*初始化种子*/    /*没有考虑具体日期,如闰年、不同月份有不同天数等*/     for (int i=0; i<n; ++i)    {        date[i].year = rand() % 2015 + 1;        date[i].month = rand() % 12 + 1;        date[i].day = rand() % 30 + 1;    }}/*冒泡排序*/void Sort(Date* date, const int& n){    Date temp;    for (int i=0; i<n; ++i)    {        for (int j=0; j<n-i-1; ++j)        {            if (date[j] > date[j+1])            {                temp = date[j];                date[j] = date[j+1];                date[j+1] = temp;            }        }    }}

  main.cpp主程序

#include <iostream>#include "date.h"#define NUM 10 using namespace std;int main(int argc, char** argv) {       Date *date = new Date[NUM];    CreatePoints(date,NUM);     cout << "before sort:" << endl;    for (int i = 0; i < NUM; i++ )    {        date[i].Print();        /*或cout << date[i] << endl;*/    }    Sort(date,NUM);    cout << "after sort:" << endl;    for (int i = 0; i < NUM; i++ )    {        date[i].Print();        /*或cout << date[i] << endl;*/    }       delete[] date;    return 0;}

 

Boolan Homework——Date类