ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

[C++]——日期类运算符的重载(针对Date类重载<,>,<=,>=,==,++,<<,>>运算符,并构建排序函数,将时间进行升序排序)

2021-05-15 13:01:42  阅读:201  来源: 互联网

标签:int month 运算符 Date else year 重载 排序 day


文章目录

前言

内容:
针对Date类重载<,>,<=,>=,==,++,<<,>>运算符,并构建排序函数,将时间进行升序排序。

要求:

  1. 掌握类的定义和使用方法,掌握类对象的声明和使用方法。
  2. 掌握对象的初始化和赋值的方法。
  3. 了解成员函数的特性、友元。
  4. 掌握静态成员的使用方法。
  5. 理解和掌握this指针的用法。
  6. 理解和掌握const类型数据的使用。

Date.h

#ifndef DATE_H
#define DATE_H

class Date
{
public:					
	friend ostream& operator<<(ostream& output, Date& d);//运算符重载 
	friend istream& operator>>(istream& input, Date& d);//运算符重载 

	//Date(); 
	Date(int = 1900, int = 1, int = 1);
	Date(const Date &);//拷贝构造函数 
	~Date();//析构函数 

	void  setDate(int, int, int); //对输入日期做有效性验证
	inline int getYear() const;
	inline int getMonth() const;//显示定义内联函数 
	inline int getDay() const;

	void displayDate() const;
	Date* addOneDay();
	bool isLeapYear();

	bool operator==(Date &);//运算符重载 
	bool operator!=(Date &);
	bool operator>(Date &);
	bool operator<(Date &);
	bool operator<=(Date &);
	bool operator>=(Date &);
	Date& operator++(); //前置 
	Date operator++(int);//后置 

	void Sort(Date date[6]);

private:
	int year, month, day;
};
#endif

Date.cpp

#include <iostream>
using namespace std;
#include "Date.h"
//运算符重载函数实现 
ostream& operator<<(ostream& output, Date& d)
{
	output << d.year << "/" << d.month << "/" << d.day;
	return output;
}

istream& operator>>(istream& input, Date& d)
{
	cout << "请输入年份:";
	while (1)
	{
		input >> d.year;
		if (d.year >= 1900)
			break;
		else
			cout << "错误,请从新输入:";
	}
	cout << "请输入月份:";
	while (1)
	{
		input >> d.month;
		if (d.month>0 && d.month<13)
			break;
		else
			cout << "错误,请从新输入:";
	}
	cout << "请输入日:";
	input >> d.day;
	return input;

}

Date::Date(int y, int m, int d){
	setDate(y, m, d);
}

Date::Date(const Date &d){
	year = d.year;
	month = d.month;
	day = d.day;
}

Date::~Date(){
}
void  Date::setDate(int y, int m, int d){
	while (1)//验证年 
	{
		if (y<1900){
			cout << "please input the year(>=1900) again:" << endl;
			cin >> y;
		}
		else{
			year = y;
			break;
		}
	}
	while (1)//验证月 
	{
		if (m<1 || m>12){
			cout << "please input the month(1=<month<=12) again:" << endl;
			cin >> m;
		}
		else{
			month = m;
			break;
		}
	}
	while (1)//验证日期 
	{
		if ((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m == 10) || (m == 12)){
			if (d<1 || d>31){
				cout << "please input the day(1=<day<=31) again:" << endl;
				cin >> d;
			}
			else{
				day = d;
				break;
			}
		}
		if ((m == 4) || (m == 6) || (m == 9) || (m == 11)){
			if (d<1 || d>30){
				cout << "please input the day(1=<day<=30) again:" << endl;
				cin >> d;
			}
			else{
				day = d;
				break;
			}
		}
		if (m == 2){
			if (isLeapYear()){
				if (d<1 || d>29){
					cout << "please input the day(1=<day<=29) again:" << endl;
					cin >> d;
				}
				else{
					day = d;
					break;
				}
			}
			else {
				if (d<1 || d>28){
					cout << "please input the day(1=<day<=29) again:" << endl;
					cin >> d;
				}
				else{
					day = d;
					break;
				}
			}
		}
	}
}
inline int Date::getYear()const {
	return year;
}

inline int Date::getMonth()const{
	return month;
}

inline int Date::getDay()const{
	return day;
}


void Date::displayDate()const{
	cout << getYear() << "/" << getMonth() << "/" << getDay() << endl;
}

Date* Date::addOneDay(){
	switch (month)
	{
	case 2:
		if (isLeapYear()){
			if (day<29)
				day++;
			else{
				day = 1;
				month++;
			}
		}
		else{
			if (day<28)
				day++;
			else{
				day = 1;
				month++;
			}
		}
		break;
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
		if (day>30){
			day = 1;
			month++;
		}
		else
			day++;
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		if (day>29){
			day = 1;
			month++;
		}
		else
			day++;
		break;
	case 12:
		if (day>30){
			day = 1;
			month = 1;
			year++;
		}
		else
			day++;
		break;
	default: cout << "ERROR" << endl;
	}
	return this;
}

bool Date::isLeapYear(){
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		return true;
	else
		return false;
}


bool Date::operator==(Date& d)
{
	return (this->year == d.year) && (this->month == d.month) && (this->day == d.day);
}


bool Date::operator!=(Date& d)
{
	return (this->year != d.year) || (this->month != d.month) || (this->day != d.day);
}

bool Date::operator>(Date& d)
{
	if (year>d.year)
		return true;
	else if (year == d.year)
	{
		if (month>d.month)
			return true;
		else if (month == d.month)
		{
			if (day>d.day)
				return true;
			else
				return false;
		}
	}
	return false;
}

bool Date::operator<(Date& d)
{
	if (year<d.year)
		return true;
	else if (year == d.year)
	{
		if (month<d.month)
			return true;
		else if (month == d.month)
		{
			if (day<d.day)
				return true;
			else
				return false;
		}
	}
	return false;
}


bool Date::operator<=(Date& d)
{
	if (year<d.year)
		return true;
	else if (year == d.year)
	{
		if (month<d.month)
			return true;
		else if (month == d.month)
		{
			if (day <= d.day)
				return true;
			else
				return false;
		}
	}
	return false;
}


bool Date::operator>=(Date& d)
{
	if (year>d.year)
		return true;
	else if (year == d.year)
	{
		if (month>d.month)
			return true;
		else if (month == d.month)
		{
			if (day >= d.day)
				return true;
			else if (day<d.day)
				return false;
		}
	}
	return false;
}


Date& Date::operator++()
{
	addOneDay();
	return *this;
}

Date Date::operator++(int)
{
	Date temp = *this;
	addOneDay();
	//cout<<"测试:"<<temp<<"  "<<*this<<endl;
	return temp;
}

void Date::Sort(Date date[6])
{
	int i, j;
	Date temp(1900, 1, 1);
	for (i = 0; i<6; i++)
	{
		for (j = 0; j<6; j++)
		{
			if (date[j]<date[j + 1])
			{
				temp = date[j];
				date[j] = date[j + 1];
				date[j + 1] = temp;
			}
		}
	}
}


main函数

#include <iostream>
using namespace std;
#include "Date.h"

void Sort(Date date1, Date date2, Date date3, Date date4, Date date5, Date date6)
{
	int i, j;
	Date date[6] = { date1, date2, date3, date4, date5, date6 };
	Date temp;
	for (i = 0; i<6; i++)
	{
		for (j = 0; j<6 - i; j++)
		{

			if (date[j]<date[j + 1])
			{
				temp = date[j];
				date[j] = date[j + 1];
				date[j + 1] = temp;
			}
		}
		cout << date[i] << " ";
	}
}

int main() {
	Date date1(2020, 4, 10);
	cout << "date1:" << date1 << endl;

	Date *date2 = date1.addOneDay();
	cout << "date2:" << *date2 << endl;

	Date date3(2000, 1, 9);
	cout << "date3:" << date3 << endl;

	Date date4(2000, 3, 1);
	cout << "date4:" << date4 << endl;

	Date date5(1999, 12, 31);
	cout << "date5:" << date5 << endl;

	Date date6(1900, 12, 1);
	cout << "date6:" << date6 << endl;

	if (date1 == date3)
		cout << endl << "两日期相等(重载==运算符)" << endl;
	else
		cout << "两日期不相等(重载==运算符)" << endl;

	if (date4 != date3)
		cout << "两日期不相等(重载!=运算符)" << endl;
	else
		cout << "两日期相等(重载!=运算符)" << endl;

	if (date4 >= date3)
		cout << date4 << ">=" << date3 << "(重载>=运算符)" << endl;
	else
		cout << date4 << "<" << date3 << "(重载>=运算符)" << endl;

	if (date6 <= date3)
		cout << date6 << "<=" << date3 << "(重载<=运算符)" << endl;
	else
		cout << date6 << ">" << date3 << "(重载<=运算符)" << endl;

	if (date3<*date2)
		cout << date3 << "<" << *date2 << "(重载<运算符)" << endl;
	else
		cout << date3 << ">" << *date2 << "(重载<运算符)" << endl;

	if (date5>date6)
		cout << date5 << ">" << date6 << "(重载>运算符)" << endl;
	else
		cout << date5 << "<" << date6 << "(重载>运算符)" << endl;

	date5++;
	cout << date5 << "(重载后置++运算符)" << endl;

	++date5;
	cout << date5 << "(重载前置++运算符)" << endl;

	cout << "日期排序:";
	Sort(date1, *date2, date3, date4, date5, date6);

	return 0;
}

运行结果

在这里插入图片描述

标签:int,month,运算符,Date,else,year,重载,排序,day
来源: https://blog.csdn.net/qq_46486243/article/details/116843077

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有