c++面向对象高级编程 作业1

发布时间:2019-06-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了c++面向对象高级编程 作业1脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

问题描述

为Date类实现如下成员:

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

然后创建两个全局函数

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

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

c++实现


//定义Date头文件
#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED
#include <iostream>
using namespace std;
class Date;
void Sort(Date* date, const int& n);
void CreatePoints(Date* date, const int& n);
class Date
{
public:
    Date(int Year=0,int Month=0,int Day=0) :year(Year), month(Month) ,day(Day){}
    bool operator >  (const Date& d2)
{
    if(year>d2.year||(year==d2.year&&month>d2.month||(year==year&&month==d2.month&&day>d2.day)))
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool operator < (const Date d2)
{
    if(year<d2.year||(year==d2.year&&month<d2.month)||(year==d2.year&&month==d2.month&&day<d2.day))
    {
        return true;
    }
    else
    {
        return false;
    }
}
 bool operator ==(const Date& d2)
{
    if(year==d2.year&&month==d2.month&&day==d2.day)
        return true;
    return false;
}
 bool isleagl(Date& date)
{
    if((date.getmonth()==4||date.getmonth()==6||date.getmonth()==9||date.getmonth()==11)&&date.getday()>30)
    {
       return false;
    }
    else if(date.getmonth()==2)
    {
        if((date.getyear()%4==0&&date.getyear()%100!=0)||date.getyear()%400==0)
        {
            if(date.getday()>29)
            {
                return false;
            }
        }
        else if(date.getday()>28)
        {
            return false;
        }
    }
    return true;
}
    int getyear() const {return year;}
    int getmonth()const {return month;}
    int getday() const {return day;}
    void print()
    {
        cout<<year<<"-"<<month<<"-"<<day<<endl;
    }
    friend void CreatePoints(Date* date,const int& n);
    friend void Sort(Date* date, const int& n);
private:
    int year;
    int month;
    int day;
};
#endif // DATE_H_INCLUDED
#include<stdlib.h>
using namespace std;
//日期合法性检查
bool isleagl(Date& date)
{
    if((date.getmonth()==4||date.getmonth()==6||date.getmonth()==9||date.getmonth()==11)&&date.getday()>30)
    {
       return false;
    }
    else if(date.getmonth()==2)
    {
        if((date.getyear()%4==0&&date.getyear()%100!=0)||date.getyear()%400==0)
        {
            if(date.getday()>29)
            {
                return false;
            }
        }
        else if(date.getday()>28)
        {
            return false;
        }
    }
    return true;
}
//创建日期并初始化
void CreatePoints(Date *date,const int&n)
{
    for(int i=0;i<n;)
    {
        int Year=rand()%3000+1;
        int Month=rand()%12+1;
        int Day=rand()%31+1;
        date[i]=Date(Year,Month,Day);
        if(isleagl(date[i]))
        {
            ++i;
        }
    }
}
//日期排序
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;
            }
        }
    }
}
int main()
{
    Date *date=new Date[10];
    CreatePoints(date,10);
    cout<<"CreatData:"<<endl;
    for (int i = 0; i < 10; i++ )
    {
        date[i].print();
    }
    Sort(date,10);
    cout << "after sort:" << endl;
    for (int i = 0; i < 10; i++ )
    {
        date[i].print();
    }
 
    delete[] date;
    return 0;
}

运行结果:

c++面向对象高级编程 作业1

脚本宝典总结

以上是脚本宝典为你收集整理的c++面向对象高级编程 作业1全部内容,希望文章能够帮你解决c++面向对象高级编程 作业1所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。