C++《日期》实现

C++《日期》实现

头文件

在该文件中是为了声明函数和定义类成员

using namespace std;
class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);//友元
	friend istream& operator>>(istream& cin, Date& d);//友元<这是为了把定义在类外面的函数,能够访问到类中的成员>
public:
	Date(int year = 1990, int month = 1, int days = 1);
	void print();
	int Getdaymonth(int year,int month)//日期获取
	{
		int getday[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		{
			return 29;
		}
		else
			return getday[month];
	}


	bool cheakdate();

    //d1+=/+
	Date& operator+=(int days);
	Date operator+(int days);


	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;

	//d1-= 和 - 
	Date& operator-=(int days);
	Date operator-(int days);

	//d++,++d
	Date& operator++();
	Date& operator++(int x);


	//d--和--d
	Date& operator--();
	Date operator--(int);


	//d1-d2(俩日期相减)
	int operator-(const Date& d)const;
private:
	int _year;
	int _month;
	int _days;
};



ostream& operator<<(ostream& out,const Date& d);//流输出
istream& operator>>(istream& cin, Date& d);//流提取

实现文件

这里是对头文件外部函数中的成员函数的逐一实现:

#include"标头.h"
bool Date::cheakdate()
{
	if (_month < 0 || _month>12)
	{
		return false;
	}
	else
	{
		return true;
	}
}

Date::Date(int year,int month,int days)
{
	_year = year;
	_month = month;
	_days = days;
	if (!cheakdate())
	{
		cout << "非法日期" << endl;
	}
}
void Date::print()
{
	cout << _year << "-" << _month << "-" << _days << endl;
}
Date& Date::operator+=(int days)
{
	if (days < 0)
	{
		return *this -= -days;
	}
	_days += days;
	while (_days > Getdaymonth(_year, _month))
	{
		_days -= Getdaymonth(_year, _month);
		_month++;
		
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
Date Date::operator+(int day) 
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}



bool Date:: operator<(const Date& d)const
{
	if (_year < d._year)
	{
	return true;
	}
	else if (_year == d._year)
	{
		if (_month < d._month)
		{
			return true;
		}
		else if (_month == d._month)
		{
			return _days < d._days;
		}
	}
	return false;
}
bool Date::operator>=(const Date& d)const
{
	return !(*this < d);
}
bool Date::operator==(const Date& d)const
{
	return _year == d._year && _month == d._month && _days == d._days;
}
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 days)
{
	if (days < 0)
	{
		return *this += -days;
	}
	_days -= days;
	while (_days<= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_days += Getdaymonth(_year, _month);

	}
	return *this;
}
Date Date::operator-(int days)
{
	Date tmp = *this;
	tmp -= days;
	return tmp;
}
Date& Date:: operator++()
{
	return *this += 1;
}

Date& Date:: operator++(int x)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

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

int Date::operator-(const Date& d)const
{
	Date max = *this;
	Date min(d);
	int flag = 1;
	int count = 0;
	if (*this <d)
	{
		max = d;
		min = *this;
		flag = -1;
    }
	while (min != max)
	{
		count++;
		min++;
	}
	return count * flag;

}



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


istream& operator>>(istream& cin, Date& d)
{
	while (1)
	{
		cout << "请依次输入数据>:";
		cin >> d._year >> d._month >> d._days;
		if (!d.cheakdate())
		{
			cout << "输入日期非法:";
			d.print();
			cout << "请重新输入:";
		}
		else
		{
			break;
		}
	}
		return cin;
}``


相关推荐

  1. C++《日期实现

    2024-07-13 21:08:01       20 阅读
  2. C++日期类的实现

    2024-07-13 21:08:01       32 阅读
  3. c++日期类的实现

    2024-07-13 21:08:01       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 21:08:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 21:08:01       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 21:08:01       58 阅读
  4. Python语言-面向对象

    2024-07-13 21:08:01       69 阅读

热门阅读

  1. 151. Reverse Words in a String

    2024-07-13 21:08:01       19 阅读
  2. 力扣--20. 有效的括号

    2024-07-13 21:08:01       17 阅读
  3. RC-u3 跑团机器人

    2024-07-13 21:08:01       16 阅读
  4. 设计模式的七项原则

    2024-07-13 21:08:01       21 阅读
  5. 力扣2381.字母移位II

    2024-07-13 21:08:01       20 阅读
  6. Transformer模型:WordEmbedding实现

    2024-07-13 21:08:01       16 阅读
  7. stm32高级定时器

    2024-07-13 21:08:01       16 阅读
  8. Jupyter Notebook 使用教程

    2024-07-13 21:08:01       20 阅读