// 操作符重载练习一
//
// 日期运算。
// 实现日期类,支持如下运算:
// +/+=:增加指定的天数;
// -/-=:减去指定的天数;
// - :两日期相差天数。
// >> :接受形如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;
}