栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

学完类和对象,我写了一个日期类

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学完类和对象,我写了一个日期类

●六个人主页:你帅你先说.
●欢迎点赞关注收藏
●既选择了远方,便只顾风雨兼程。
●蘭欢迎大家有问题随时私信我!
●類版权:本文由[你帅你先说.]原创,CSDN首发,侵权必究。

吝为您导航吝

裡1.设计思路2.Date类框架3.类成员函数实现

裡1.设计思路

本次我们设计的日期类大致要实现日期大小的判断、日期的加减、日期的输入输出、日期对应星期几。这将把我们前面类和对象中所学的知识点运用起来,让我们对类和对象有一个更深的理解。

2.Date类框架

Date.h

#pragma once
#include
using namespace std;
class Date
{
	// 友元函数
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	Date(int year = 1, int month = 1, int day = 1);
	void Print() const;
	int GetMonthDay(int year, int month) const;
	bool operator>(const Date& d) const;
	bool operator<(const Date& d)const;
	bool operator>=(const Date& d)const;
	bool operator<=(const Date& d)const;
	bool operator==(const Date& d)const;
	bool operator!=(const Date& d)const;
	Date& operator+=(int day);
	Date operator+(int day)const;
	Date& operator-=(int day);
	Date operator-(int day)const;
	Date& operator++();
	Date operator++(int);
	Date& operator--();
	Date operator--(int);
	int operator-(const Date& d) const;
	void PrintWeekDay() const;
private:
	int _year;
	int _month;
	int _day;
};
3.类成员函数实现

这些实现放在Date.cpp文件里
首先要实现的接口就是构造函数

void Date::Print() const
{
	cout << _year << "-" << _month << "-" << _day << endl;
}
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;

	if (!(_year >= 0
		&& (month > 0 && month < 13)
		&& (day > 0 && day <= GetMonthDay(year, month))))
	{
		cout << "非法日期->";
		Print();
	}
}

接下来我们要实现Date类中最核心的一个接口:GetMonthDay,这个接口当你传入年和月时,它会返回对应的天数,这里就涉及到平年和闰年的问题了。C语言阶段我们学过判断闰年,有一句口诀:四年一闰,百年不闰,四百年再闰,用代码来实现就是
year % 4 == 0 && year % 100 != 0 || year % 400 == 0

int Date::GetMonthDay(int year, int month) const
{
	static int monthDayArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day = monthDayArray[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		day += 1;
	}

	return day;
}

完成了这个接口的实现,接下来的接口就轻松了。
重载>运算符

bool Date::operator>(const Date& d)const
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

重载==运算符

bool Date::operator==(const Date& d)const
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

漏重载<运算符

bool Date::operator<(const Date& d)  const
{
	return !(*this >= d);
}

重载>=运算符

bool Date::operator>=(const Date& d)  const
{
	return *this > d || *this == d;
}

重载<=运算符

bool Date::operator<=(const Date& d)  const
{
	return  !(*this > d);
}

重载!=运算符

bool Date::operator!=(const Date& d)  const
{
	return  !(*this == d);
}

錄重载+=运算符

Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= -day;
	}

	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;

		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}

	return *this;
}

重载+运算符

Date Date::operator+(int day)  const
{
	Date ret(*this);
	//ret.operator+=(day);
	ret += day;

	return ret;
}

樓重载-=运算符

Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}

	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}

		_day += GetMonthDay(_year, _month);
	}

	return *this;
}

重载-运算符

//计算减掉几天后的日期
Date Date::operator-(int day)  const
{
	Date ret(*this);
	ret -= day;

	return ret;
}
//计算日期差
int Date::operator-(const Date& d)  const
{
	Date max = *this;
	Date min = d;
	int flag = 1;

	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}

	int count = 0;
	while (min != max)
	{
		++min;
		++count;
	}

	return count * flag;
}

重载前置++运算符

Date& Date::operator++()
{
	*this += 1;

	return *this;
}

重载后置++运算符

Date Date::operator++(int)
{
	Date ret(*this);
	*this += 1;

	return ret;
}

重载前置--运算符

Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

婢重载后置--运算符

Date Date::operator--(int)
{
	Date ret(*this);
	*this -= 1;
	return ret;
}

输出星期几

void Date::PrintWeekDay() const
{
	const char* arr[] = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天" };
	//Date start(1900, 1, 1);
	//int count = *this - start;
	int count = *this - Date(1900, 1, 1);

	cout << arr[count % 7] << endl;
}

徭重载<<运算符

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "/" << d._month << "/" << d._day << endl;
	return out;
}

重载>>运算符

istream& operator>>(istream& in,Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

Date.h和Date.cpp文件里的内容实现完后,整个日期类就完成了。接下来就可以在Test.cpp文件里调用运行了。觉得太简陋了,也可以在Test.cpp里写个菜单。

喜欢这篇文章的可以给个一键三连 点赞关注收藏

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737772.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号