Coding In C++, Day05

// 操作符重载练习一
//
// 日期运算。
// 实现日期类,支持如下运算:
// +/+=:增加指定的天数;
// -/-=:减去指定的天数;
// -   :两日期相差天数。
// >>  :接受形如2014 1 14格式输入;
// <<  :以形如2014-1-14的格式输出;
#include <iostream>
using namespace std;
class Date {
public:
	Date (int nYear = 0, int nMonth = 0,
		int nDay = 0) :
		m_nYear (nYear), m_nMonth (nMonth),
		m_nDay (nDay) {}
	Date operator+ (int nDays) const {
		return Days2Date (Date2Days () + nDays);
	}
	Date& operator+= (int nDays) {
		return *this = *this + nDays;
	}
	Date operator- (int nDays) const {
		return Days2Date (Date2Days () - nDays);
	}
	Date& operator-= (int nDays) {
		return *this = *this - nDays;
	}
	int operator- (const Date& date) const {
		return Date2Days () - date.Date2Days ();
	}
	friend istream& operator>> (istream& is,
		Date& date) {
		return is >> date.m_nYear >> date.m_nMonth
			>> date.m_nDay;
	}
	friend ostream& operator<< (ostream& os,
		const Date& date) {
		return os << date.m_nYear << "-"
			<< date.m_nMonth << "-" << date.m_nDay;
	}
private:
	int Date2Days (void) const {
		int nDays = (m_nYear - 1) * 365;
		for (int nYear = 1; nYear < m_nYear;
			nYear++)
			if (IsLeap (nYear))
				nDays++;
		for (int nMonth = 0; nMonth < m_nMonth - 1;
			nMonth++)
			if (IsLeap (m_nYear))
				nDays += s_nDaysOfMonth[1][nMonth];
			else
				nDays += s_nDaysOfMonth[0][nMonth];
		nDays += m_nDay;
		return nDays;
	}
	Date Days2Date (int nDays) const {
		int nYear = 1;
		for (;;) {
			if (IsLeap (nYear)) {
				if (nDays <= 366)
					break;
				nDays -= 366;
			}
			else {
				if (nDays <= 365)
					break;
				nDays -= 365;
			}
			nYear++;
		}
		int nMonth = 1;
		bool bLeap = IsLeap (nYear);
		for (;;) {
			if (bLeap) {
				if (nDays <=
					s_nDaysOfMonth[1][nMonth-1])
					break;
				nDays-=s_nDaysOfMonth[1][nMonth-1];
			}
			else {
				if (nDays <=
					s_nDaysOfMonth[0][nMonth-1])
					break;
				nDays-=s_nDaysOfMonth[0][nMonth-1];
			}
			nMonth++;
		}
		return Date (nYear, nMonth, nDays);
	}
	bool IsLeap (int nYear) const {
		return (nYear % 4 == 0 && nYear % 100 != 0)
		   || nYear % 400 == 0;
	}
	int m_nYear;
	int m_nMonth;
	int m_nDay;
	static int s_nDaysOfMonth[][12];
};
int Date::s_nDaysOfMonth[][12] = {
	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31},
	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30,31}
};
int main (void) {
	cout << "Enter a date: ";
	Date d1;
	cin >> d1;
	cout << "Enter the number of days: ";
	int nDays;
	cin >> nDays;
	Date d2 = d1 + nDays;
	cout << d1 << " + " << nDays << " = " << d2
		<< endl;
	Date d3 (d1);
	d3 += nDays;
	cout << d1 << " += " << nDays << " -> " << d3
		<< endl;
	cout << "Enter a date: ";
	cin >> d1;
	cout << "Enter the number of days: ";
	cin >> nDays;
	d2 = d1 - nDays;
	cout << d1 << " - " << nDays << " = " << d2
		<< endl;
	d3 = d1;
	d3 -= nDays;
	cout << d1 << " -= " << nDays << " -> " << d3
		<< endl;
	cout << "Enter a date: ";
	cin >> d1;
	cout << "Enter another date: ";
	cin >> d2;
	cout << "The days between those dates is "
		<< d2 - d1 << endl;
	return 0;
}
//
//
//操作符重载练习二
//
//
#include <iomanip>
#include <iostream>
using namespace std;
class M33 {
public:
	M33 (void) 	{
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				m_a[i][j] = 0;
	}
	M33 (int a[][3]) {
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				m_a[i][j] = a[i][j];
	}
	const M33 operator+ (const M33& m) const {
		int a[3][3];
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				a[i][j] = m_a[i][j] + m.m_a[i][j];
		return a;
	}
	const M33 operator- (const M33& m) const {
		int a[3][3];
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				a[i][j] = m_a[i][j] - m.m_a[i][j];
		return a;
	}
	const M33 operator* (const M33& m) const {
		int a[3][3] = {0};
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				for (int k = 0; k < 3; k++)
					a[i][j]+=m_a[i][k]*m.m_a[k][j];
		return a;
	}
	M33& operator+= (const M33& m) {
		return *this = *this + m;
	}
	M33& operator-= (const M33& m) {
		return *this = *this - m;
	}
	M33& operator*= (const M33& m) {
		return *this = *this * m;
	}
	const M33 operator- (void) const {
		return M33 () - *this;
	}
	M33& operator++ (void) {
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				m_a[i][j]++;
		return *this;
	}
	M33& operator-- (void) {
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				m_a[i][j]--;
		return *this;
	}
	const M33 operator++ (int) {
		M33 m = *this;
		++(*this);
		return m;
	}
	const M33 operator-- (int) {
		M33 m = *this;
		--(*this);
		return m;
	}
	friend ostream& operator<< (ostream& os,
		const M33& m) {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++)
				os << setw (4) << m.m_a[i][j];
			os << endl;
		}
		return os;
	}
private:
	int m_a[3][3];
};
int main (void) {
	int a1[3][3] = {1,2,3,4,5,6,7,8,9};
	M33 m1 (a1);
	int a2[3][3] = {9,8,7,6,5,4,3,2,1};
	M33 m2 (a2);
	cout << m1 + m2 << endl;
	cout << m1 - m2 << endl;
	cout << m1 * m2 << endl;
	m1 += m2;
	cout << m1 << endl;
	m1 -= m2;
	cout << m1 << endl;
	m1 *= m2;
	cout << m1 << endl;
	cout << -m1 << endl;
	cout << ++m2 << endl;
	cout << m2 << endl;
	cout << --m2 << endl;
	cout << m2 << endl;
	cout << m2++ << endl;
	cout << m2 << endl;
	cout << m2-- << endl;
	cout << m2 << endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值