c++实现日期类

作为c++新手小白,在国庆头2天发狂,玩?漏漏漏~搞作业啊!!!栓Q啊xdm!

(谢谢~其实晕倒在wc了)

作业:根据给出的类中函数实现函数体功能

(呜呜呜呜呜呜)

        PART 1:头文件和命名空间

#include<iostream>
#include<fstream>
#include<cassert>
using namespace std;

        PART 2:Date类

class Date {
public:
	Date(int yy = 1900, int mm = 1, int dd = 1);    //构造函数
	virtual ~Date();                                //析构函数
	
 //得到年月日
	int Getyear() { return year; }
	int Getmonth() { return month; }
	int Getday() { return day; }
	
//设置日期
	void setDate();
	void setDate(int yy,int mm,int dd);
	int DaysofMonth(int year, int month) const;    //返回某月的天数
	bool IsLeapyear(int year) const                //判断是否为平年
	{
		return year % 400 ? (year % 100 ?(year%4? (false) : (true)) : (false)):true;
	}
	Date Changeofdays(int days);                    //改变日期
	int Getmonthday(int year, int month);           //某年某月有多少天
	bool Isvalid();                                 //日期是否合法
	int Betweendays(const Date& d) const;           //日期间隔
	virtual void showdate();                        //显示日期
	string ItoA(int kk);                            //转换为周几
	
//日期加减days个天数单目
	Date operator+=(int days);                      
	Date operator-=(int days);                       
    Date operator+(int days);
	Date operator-(int days);
	Date operator++();
	Date operator++(int);
	Date operator--();
	Date operator--(int);
	
//重载双目运算符
	friend long operator-(const Date& d1, const Date& d2);
	
//日期大小比较
	friend bool operator>(const Date& d1, const Date& d2);
	friend bool operator<(const Date& d1, const Date& d2);
	friend bool operator==(const Date& d1, const Date& d2);
    friend bool operator!=(const Date& d1, const Date& d2);
	friend bool operator>=(const Date& d1, const Date& d2);
	friend bool operator<=(const Date& d1, const Date& d2);
	
//输入输出日期
	friend ostream& operator<<(ostream& out, const Date& d);
	friend fstream& operator>>(fstream& in,  Date& d);
	friend ofstream& operator<<(ofstream& myout, Date& d);
private:
	int year, month, day;
};

        PART 2:Date函数体

Date(int yy=1900,int mm=1,int dd=1);//带参数的构造函数

Date ::Date(int yy,int mm,int dd):year(yy),month(mm),day(dd)
{ }

virtual ~Date;        //析构函数

virtual Date::~Date()
{ }

 void setDate();                                //无参数的构造函数

 void setDate(int yy,int mm,int dd);  //有参数的构造函数

void Date::setDate(){}

void Date::setDate(int yy, int mm, int dd) {
	year = yy;
	month = mm;
	day = dd;
}

 int DaysofMonth(int year,int month) const;        //返回某个月的天数

int Date::DaysofMonth(int yy, int mm) const {
	int d = 0;
	switch (mm)
	{case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		d = 31;
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		d = 30;
		break;
	case 2:
		d = 28+IsLeapyear(yy);
		break;
	}

	return d;
	
}

Date Changeofdays(int days);                //增加天数后的日期

int Getmonthday(int year,int month);          //某年某月多少天

//随意增加天数后的日期
Date Date::Changeofdays(int days) {
	int yeartemp = year;
	int monthtemp = month;
	int daytemp = day;
	if (days > 0) {
		daytemp += days;
		while (daytemp > DaysofMonth(yeartemp, monthtemp)) {
			daytemp -= DaysofMonth(yeartemp, monthtemp);
			monthtemp++;
			if (monthtemp > 12) {
				yeartemp++;
				monthtemp = 1;
			}
		}
	}
	else {//days为负数
		daytemp += days;
		while (daytemp<1)
		{
			monthtemp--;
			if (monthtemp < 1) {
				yeartemp--;
				monthtemp = 12;
			}
			daytemp += DaysofMonth(yeartemp, monthtemp);
		}
	}
	return Date(yeartemp, monthtemp, daytemp);
}
//输出某年的某月有多少天
int Date::Getmonthday(int year, int month) {
	int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && IsLeapyear(year))
	{
		++arr[2];
	}
	return arr[month];
}

bool Isvalid();                        //判断日期是否合法

virtual void showdate();         //显示日期

bool Date::Isvalid() 
{
return (year > 0 && month > 0 && month < 12 && day>0 && day <= Getmonthday(year, month));
}

virtual void Date:: showdate() {
	cout << year << "年" << month << "月" << day << "日" << endl;
}

int Betweendays(const Date& d) const; //日期间隔

//计算2个日期之间差多少天
int Date::Betweendays(const Date& d) const {
	const int day_of_month[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
	int years = year - d.year;
	int months = day_of_month[month] - day_of_month[d.month];
	int days = day - d.day;

	int alldays = years * 365 + years / 4 - years / 100 + years / 400 + months + days;
	return alldays;
}

string ItoA(int kk);        //转换为周几

//日期减多少天后的周几
string Date::ItoA(int kk) {
	Date start(1);//假设1为开始
	kk = day - kk;
	string str[] = { "周一","周二","周三","周四","周五","周六","周日" };
	return str[kk % 7];
}

日期加减

Date operator+=(int days);

 Date operator-=(int days);

//+=
Date Date::operator+=(int days) {
	if (days == 0)
		return *this;
	else {
		*this = this->Changeofdays(days);
		return *this;
	}
}
//-=
Date Date::operator-=(int days) {
	if (days == 0)
		return *this;
	else {
		*this = this->Changeofdays(-days);
		return *this;
	}
}

Date operator+(int days);

 Date operator-(int days);

//+
Date Date::operator+(int days) {
	Date temp(*this);
	for (int i = 0; i < days; i++)
	{
		temp.operator++();
	}
	return temp;
}
//-
Date Date::operator-(int days) {
	Date temp(*this);
	for (int i = 0; i < days; i++)
	{
		temp.operator--();
	}
	return temp;
}

Date operator++();

 Date operator++(int);

//前置++
Date Date::operator++() 
{
	*this = this->Changeofdays(1);
	return *this;
}
//后置++
Date Date::operator++(int) 
{
	Date dtemp(* this);
	++(*this);
	return dtemp;
}

Date operator--();

 Date operator--(int);

//前置--
Date Date::operator--() {
	*this = this->Changeofdays(-1);
	return *this;
}
//后置--
Date Date::operator--(int) 
{
	Date dtemp(*this);
	--(*this);
	return dtemp;
}

重载双目运算符

friend bool operator>(const Date& d1,const Date& d2);

friend bool operator<(const Date& d1,const Date& d2);

//是否>
bool operator>(const Date& d1, const Date& d2) {
	if (d1.year > d2.year) return true;
	else if (d1.year < d2.year) return false;
		else {
		if (d1.month > d2.month)
			return true;
		else if (d1.month < d2.month)
			return false;
		else {
			if (d1.day > d2.day)
				return true;
			else if (d1.day < d2.day)
				return false;
			else return false;
		}
	}
}
//是否<
bool operator<(const Date& d1, const Date& d2) {
	if (d1.year < d2.year) return true;
	else if (d1.year > d2.year) return false;
	else {
		if (d1.month < d2.month)
			return true;
		else if (d1.month > d2.month)
			return false;
		else {
			if (d1.day < d2.day)
				return true;
			else if (d1.day > d2.day)
				return false;
			else return false;
		}
	}
}

friend bool operator==(const Date& d1,const Date& d2);

friend bool operator!=(const Date& d1,const Date& d2);

bool operator==(const Date& d1, const Date& d2) {
	if (d1.year == d2.year && d1.month == d2.month && d1.day == d2.day)
		return true;
	else return false;
}
//是否!=
bool operator!=(const Date& d1, const Date& d2) {
	if (d1.year != d2.year || d1.month != d2.month || d1.day != d2.day)
		return true;
	else return false;
}

friend bool operator>=(const Date& d1,const Date& d2);

friend bool operator<=(const Date& d1,const Date& d2);

//是否<=
bool operator<=(const Date& d1, const Date& d2) {
	if (d1.year < d2.year) return true;
	else if (d1.year > d2.year) return false;
	else {
		if (d1.month < d2.month)
			return true;
		else if (d1.month > d2.month)
			return false;
		else {
			if (d1.day < d2.day)
				return true;
			else if (d1.day > d2.day)
				return false;
			else return true;
		}
	}
}
//是否>=
bool operator>=(const Date& d1, const Date& d2) {
	if (d1.year > d2.year) return true;
	else if (d1.year < d2.year) return false;
	else {
		if (d1.month > d2.month)
			return true;
		else if (d1.month < d2.month)
			return false;
		else {
			if (d1.day > d2.day)
				return true;
			else if (d1.day < d2.day)
				return false;
			else return true;
		}
	}
}

输入输出日期 

friend ostream& operator<<(ostream& out, const Date& d);

 friend fstream& operator>>(fstream& in,  Date& d);
 friend ofstream& operator<<(ofstream& myout, Date& d);

//针对文件读取的流
ostream& operator<<(ostream& out, const Date& d) {
	out << d.year << "/" << d.month << "/" << d.day;
	return out;
}
//针对文件写入的流
fstream& operator>>(fstream& in,  Date& d) {
	in >> d.year;
	in >> d.month;
	in>> d.day;
	return in;
}
//针对文件读的流
ofstream& operator<<(ofstream& myout, Date& d) {
	myout << d.year << "-" << d.month << "-" << d.day;
	return myout;
}

PART 3:main函数体现其功能

int main() {
	Date mydate,c,c1;
	cout << "构造函数Date(int yy = 1900, int mm = 1, int dd = 1):" << endl;
	mydate.showdate();
	mydate.~Date();
	cout << endl;

	cout <<"获取mydate年份" << mydate.Getyear() << endl;
	cout <<"获取mydate月份"<< mydate.Getmonth() << endl;
	cout << "获取mydate日子" << mydate.Getday() << endl;
	cout << endl;

    mydate.setDate();
	cout << "没有参数的setDate()" <<mydate<< endl;
	
    mydate.setDate(200,1,1);
	cout << "有参数的setDate(2000,1,1)重置mydate时间:" <<mydate<< endl;

	cout << "显示2001年12月的天数(调用DaysofMonth):" << endl;
	cout<<mydate.DaysofMonth(2001,12)<<endl;
	cout << endl;

    c=mydate.Changeofdays(22);
	cout << "显示2001年12月共增加的天数(1+22)(调用Changeofdays):" <<c<< endl;

	cout << "显示2002年1月的天数,调用Getmonthday:" << endl;
	cout<<mydate.Getmonthday(2002, 1)<<endl;
	cout << endl;

	cout << "调用Isvalid判断年月日是否有效,输出1,有效,输出0,无效" << endl;
	cout<<mydate.Isvalid()<<endl;
	cout << endl;
	
	cout << "原来mydate的日期:" <<mydate<< endl;
	
    mydate.operator+=(20);
	cout << "调用operator+=(20)后的日期:" <<mydate<< endl;
    
    mydate.operator-=(20);
	cout << "调用operator-=(20)后的日期:" << mydate<<endl;

    ++mydate;
	cout << "调用operator++()后的日期(即++mydate):" <<mydate<< endl;
	mydate++;
	cout << "调用operator++(int)后的日期(即mydate++):" <<mydate<< endl;
    
    --mydate;
	cout << "调用operator--()后的日期(即--mydate):" <<mydate<< endl;
    mydate--;
	cout << "调用operator--(int)后的日期(即mydate--):" <<mydate<< endl;

	cout << "myclock的日期:" <<mydate<< endl;
    
    c1=(mydate+ 45);
	cout << "调用operator+后的日期(即mydate45天之后的日期):" <<c1<< endl;
    c1 = (mydate - 45);
	cout << "调用operator-后的日期(即mydate45天之前的日期):" <<c1<< endl;

	cout << "调用long operator-对mydate和c1:" << endl;
	cout << "mydate:" << mydate<<endl;
	cout << "c1:" << c1<<endl;
	cout << endl;
	cout<<"mydate - c1"<<":"<<mydate - c1 << endl;
	cout << endl;

	cout << "c1的日期:" <<c1<< endl;
	cout << "c1调用ItoA后减3天后得到的星期数:" << c1.ItoA(3)<< endl;
	cout << endl;


	cout << "调用mydate.Betweendays(c1):" << mydate.Betweendays(c1) << endl;
	cout << endl;


	cout << "mydate和c1比较大小" << endl;
	cout << "mydate的日期:" <<mydate<< endl;
	cout << "c1的日期:" << c1<<endl;
	cout << endl;

	cout << "输出1,则是,输出0,则否" << endl;

	cout << "mydate > c1():" <<(mydate > c1) << endl;
	cout << endl;

	cout << "mydate < c1:" <<(mydate < c1) << endl;
	cout << endl;

	cout << "mydate == c1:" <<(mydate == c1) << endl;
	cout << endl;

	cout << "mydate >= c1:" <<(mydate >= c1) << endl;
	cout << endl;

	cout << "mydate <= c1:" <<(mydate <= c1) << endl;
	cout << endl;

	cout << "mydate != c1:" <<(mydate != c1) << endl;
	cout << endl;

	cout << "调用 ostream& " << endl;
	Date& c2 = mydate;
	cout << c2<<endl;
	cout << endl;

	cout << "调用 fstream" << endl;
	Date& c3=c1;
	cout << c3 << endl;
	cout << endl;

	cout << "调用ofstream" << endl;
	ofstream myout("ofs.txt", ios::in|ios::out);
	myout << c1;
}

如有雷同,纯属巧合,或私。

c++小白菜欢迎批评指正,ofstream 是本白菜最讨厌的一个函数,main可以自己调格式,本白菜显示屏上会很乱(呜呜呜呜)

不喜勿喷。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值