C++ 模拟日期类的实现

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class Date 
{
	//友元函数
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
private:
	int _year;
	int _month;
	int _day;
public:
	int GetMonthDay(int year, int month)
	{
		static int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

		// 是2月且是闰年返回29
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
		{
			return 29;
		}
		return monthDays[month];
	}
	Date (int year = 1, int month = 1, int day = 1)
	{
		if (year > 0 && (month > 0 && month < 13) && (day > 0 && day <= GetMonthDay(year, month)))
		{
			_year = year;
			_month = month;
			_day = day;
			//cout << "Date" << endl;
		}
		else
			cout << "非法日期" << endl;
	}
	// <  ==  >   <=   >=  !=
	inline bool operator<(const Date& d) const  //const 是给 this 指针加的
	{
		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;

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

	}
	inline bool operator>(const Date& d) const
	{
		return !(*this < d || *this == d);
	}
	 bool operator<=(const Date& d) const
	{
		return !(*this > d);
	}
	bool operator>=(const Date& d) const
	{
		return !(*this < d);
	}
	bool operator!=(const Date& d) const
	{
		return !(*this == d);
	}
	
	//-=  +=   +   -   前置--  后置--  前置++  后置++
	//d1 += 100  d1也会改变
	Date& operator+=(int day) 
	{
		if (day >= 0)
			_day += day;
		else
		{
			//如果输入的day是负数,就去调 operator-=
			return *this -= -day;   //记得是-day,不然会造成无限循环调用
		}
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month); //这里注意,先变日期在变月份
			++_month;
			if (_month > 12)
			{
				++_year;
				_month = 1;
			}
		}
		return *this;
	}
	//d1 -= 100  d1也会改变
	Date& operator-=(int day)
	{
		if (day >= 0)
			_day -= day;
		else
		{
			//如果_day 是负数就去调 operator-=
			return *this += -day;  //记得是-day,不然会造成无限循环调用
		}
		while (_day < 1)
		{
			_day += GetMonthDay(_year, _month); //注意点同上
			--_month;
			if (_month < 1)
			{
				--_year;
				_month = 12;
			}
		}
		return *this;
	}
	//d1 + 100  d1不会改变
	Date operator+(int day) const
	{
		Date ret = *this;
		ret += day;
		return ret;   //这里返回的是临时变量,出了作用域会自动销毁,所以不用 Date&
	}
	//d1 - 100  d1不会改变
	Date operator-(int day) const 
	{
		Date ret = *this;
		ret -= day;
		return ret;
	}
	//前置++
	Date& operator++()
	{
		return *this += 1;
	}
	//后置++,为了构成重载所以加了一个int
	Date operator++(int)
	{
		Date tmp = *this;
		*this += 1;
		return tmp;
	}
	//前置--
	Date& operator--()
	{
		return *this -= 1;
	}
	//后置--
	Date operator--(int)
	{
		Date tmp = *this;
		*this -= 1;
		return tmp;
	}

	// & 取地址
	const Date* operator&() const  //参数为 const Date* this
	{
		return this;  //返回的也是const Date* ,如果函数名不加const,返回值类型就变成了 Date* ,不符合
	}
	void print() //const
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	int operator- (const Date & d)  
	{
		int flag = 1;
		Date max = *this;
		Date min = d;
		if (max < min)
		{
			max = d;
			min = *this;
			flag = -1;
		}
		int n = 0;
		while (max != min)
		{
			++min;
			++n;
		}
		return n * flag;
	}
};
//重载 cout 
ostream& operator<<(ostream& out,const Date& d)
{
	out << d._year << "-" << d._month << "-" << d._day << endl;
	return out;
}
//重载 cin
istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
int main()
{
	Date d1(2020, 4, 5);
	Date d2(2020, 4, 20);
	cin >> d1 >> d2;
	cout << d1 << d2;
	//fun(d1);
	/*Date d1(2020, 4, 15);
	d1 += 300;
	int i = 0;
	++i;   i是整形变量,++不会去调Date类型的++,因为类型不匹配
	++d1;*/
	/*d1.print();*/
	/*Date d2 = d1 + -100;*/	
	/*Date d2 =  d1--;
	d2.print();*/
	/*Date d1(2020, 4, 15);
	Date d2(2021, 4, 15);
	cout <<d2 - d1 << endl;*/
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
程序清单 按以下步骤向视图类(CClockView)添加下列数据成员及成员函数。 (1) 添加表示年、月、日、时、分、秒的变量。 int year; int month; int day; int hour; int minute; int second; (2) 添加秒表的计数变量。 int watch; (3) 添加时钟的画笔及画刷变量。 CPen m_HouPen, m_MinPen, m_SecPen; // 各种针的画笔 CBrush m_MarkBrush; // 表盘标记的画刷 (4) 添加时钟控制变量。 CPoint m_Center; // 表的中心 double m_Radius; // 表的半径 CPoint m_Hour [2], m_OldHour [2]; // 时针当前及前一次位置 CPoint m_Minute [2], m_OldMin [2]; // 分针当前及前一次位置 CPoint m_Second [2], m_OldSec [2]; // 秒针当前及前一次位置 (5) 添加秒表的两个按钮位置变量。 CRect m_WatchStart; CRect m_WatchStop; (6) 添加两个函数,计算时钟各指针位置。 void SetClock (int hour, int minute, int second); CPoint GetPoint (int nLenth, int nValue); (7) 在视图类构造函数中增加初始化语句: CClockView::~CClockView() { //设定时间 year=2010; month=11; day=22; hour=0; minute=0; second=0; //设定画笔画刷 m_HouPen.CreatePen(PS_SOLID,5,RGB(255,0,0));//时针画笔 m_MinPen.CreatePen(PS_SOLID,3,RGB(0,0,250));//分针画笔 m_SecPen.CreatePen(PS_SOLID,1,RGB(0,0,0));//秒针画笔 m_MarkBrush.CreateSolidBrush(RGB(250,250,0)); //设定表芯位置 m_Center.x=222; m_Center.y=222; //设定时钟半径 m_Radius=222; //计算指针位置 SetClock(hour,minute,second); //设定秒表计数器及按钮位置 watch=0; m_WatchStart=CRect(480,310,560,340);//启动按钮 m_WatchStop=CRect(590,310,670,340);//停止按钮 } 编写指针位置函数SetClock和GETpOINT。 首先在ClockView.cpp文件头部下添加下面两行代码,以便进行数学计算。 #define PI 3.14159265258 #include"math.h" 然后添加下列代码: //计算个指针位置的函数 void CClockView::SetClock(int hour,int minute,int second) { hour=hour*5; hour=hour+minute/12; //保存时针原位置 m_OldHour[0]=m_Hour[0]; m_OldHour[1]=m_Hour[1];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值