C++—日期类的实现+运算符重载

Date.h

#pragma once
#include<iostream>
using namespace std;

class Date
{
public:

	//获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		static int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };     //建立12个月天数的数组

		//判断是否是闰年的二月份
		if (month == 2&& ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)))
		{
			return 29;
		}
		else
		{
			return MonthDay[month];
		}
	}

	//全缺省构造函数
	Date(int year=1997,int month=10,int day=20)
	{
		_year = year;
		_month = month;
		_day = day;

		//检查输入的日期是否合法
		if (!(year >= 1 && year <= 12) && (month >= 1 && month <= 12) 
			&& (day >= 1 && day <= GetMonthDay(year, month)))
		{
			cout << "非法日期" << endl;
		}
	}


	析构函数
	//~Date();

	//拷贝构造函数
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	//赋值运算符重载
	Date& operator=(const Date& d)
	{
		if (*this != d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	//==运算符重载
	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;


private:
	int _year;
	int _month;
	int _day;
};

Date.cpp

#include "Date.h"

bool Date::operator==(const Date& d) const 
{
	return _year == d._year && _month==d._month && _day == d._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;
	}

	return false;
}

//>=运算符重载
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);
}

//!运算符重载
bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}

//日期+=天数
Date& Date::operator+=(int day) 
{
	if (day < 0)
	{
		return *this -= abs(day);
	}

	_day += day;
	while(_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
	}
	if (_month == 13)
	{
		++_year;
		_month = 1;
	}
}

//日期+天数
Date Date::operator+(int day) const
{
	Date ret(*this);     //拷贝构造
	ret+= day;
	return ret;
}

//日期-=天数
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += abs(day);
	}

	day -= _day;
	while (day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 0;
		}
		
		_day += GetMonthDay(_year, _month);
	}
}

//日期-天数
Date Date::operator-(int day) const
{
	Date ret(*this);
	ret -= day;
	return ret;
}

//前置
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

//后置
Date Date::operator++(int)
{
	Date ret(*this);
	ret += 1;
	return ret;
}

//前置
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
//后置
Date Date::operator--(int)
{
	Date ret(*this);
	ret -= 1;
	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 n = 0;
	while (max == min)
	{
		min++;
		++n;
	}

	return n * flag;
}

main.cpp

#include "Date.h"

int main()
{
	Date d1;
	Date d2(d1);

	d2.Print();

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C++中的高精度运算符重载是指对整数进行大数运算时,通过重载运算符来实现对大数的加减乘除等操作。一般情况下,C++内置的整数类型(如int、long等)有一定的位数限制,无法处理超过其表示范围的大数。而通过运算符重载,我们可以自定义一个类来表示大数,并对其进行各种运算操作。 以下是一个简单的示例,展示了如何实现C++中的高精度运算符重载: ```cpp #include <iostream> #include <vector> using namespace std; class BigInteger { private: vector<int> digits; // 用vector存储大数的每一位 public: BigInteger() {} BigInteger(int num) { while (num > 0) { digits.push_back(num % 10); num /= 10; } } BigInteger operator+(const BigInteger& other) const { BigInteger result; int carry = 0; int i = 0; while (i < digits.size() || i < other.digits.size() || carry != 0) { int sum = carry; if (i < digits.size()) { sum += digits[i]; } if (i < other.digits.size()) { sum += other.digits[i]; } result.digits.push_back(sum % 10); carry = sum / 10; i++; } return result; } friend ostream& operator<<(ostream& os, const BigInteger& num) { for (int i = num.digits.size() - 1; i >= 0; i--) { os << num.digits[i]; } return os; } }; int main() { BigInteger a(123456789); BigInteger b(987654321); BigInteger c = a + b; cout << "a + b = " << c << endl; return 0; } ``` 在上述示例中,我们定义了一个名为BigInteger的类,用于表示大数。通过重载加法运算符`+`,我们可以实现对两个BigInteger对象的相加操作。同时,我们还重载了输出流运算符`<<`,以便能够直接输出BigInteger对象的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值