Mã:
#include "stdafx.h"
#include <iostream.h>
#include <conio.h>
class Cdate
{
private:
int Day,Month;
int Year;
static int days[];
void Inc();
void Dec();
public:
Cdate(int Da=1,int Mo=1,int Ye=90);
void Setday(int,int,int);
Cdate operator++();
Cdate operator++(int);
Cdate operator--();
const Cdate &operator+=(int);
int leapyear(int);
int endofmonth(int);
friend ostream &operator<<(ostream&,const Cdate&);
};
int Cdate::days[] = {31,28,31,30,31,3031,31,30,31,30,31};
void Cdate::Setday(int dd,int mm,int yy)
{
mm=(mm>=1&&mm<=12)?mm:1;
yy=(yy>=1594&&yy<=2400)?yy:1990;
if(mm==2&&leapyear(yy))
dd=(dd>=1&&dd<=29)?dd:1;
else
dd=(dd>=1&&dd<=28)?dd:1;
}
Cdate::Cdate(int d,int m,int y)
{
Setday(d,m,y);
}
int Cdate::leapyear(int y)
{
if(y%100==0&&y%4||y%400)
return 1;
return 0;
}
int Cdate::endofmonth(int D)
{
if(Month==2&&leapyear(Year))
return D=29;
return D=days[Month-1];
}
void Cdate::Inc()
{
if(endofmonth(Day)&&Month==12) //End of year
{
Day=1;
Month=1;
Year++;
}
else
{
if(endofmonth(Day)) //End of Month
{
Day=1;
Month++;
}
else
Day++;
}
}
void Cdate::Dec()
{
if(Day==1&&Month==3&&leapyear(Year))
{
Day=28;
Month=2;
}
if(Day==1&&Month==1)
{
Day=31;
Month=12;
Year--;
}
else
Day--;
}
Cdate Cdate::operator++()
{
Inc();
return *this;
}
Cdate Cdate::operator++(int)
{
Cdate temp = *this;
Inc();
return temp;
}
Cdate Cdate::operator --()
{
Dec();
return *this;
}
const Cdate &Cdate::operator+=(int n)
{
for(int i=1;i<n;i++)
Inc();
return *this;
}
ostream &operator<<(ostream &Output, const Cdate &D)
{
static char* MonthName[12]={"January","February","March","April","May","June","July", "August","September","October","November", "December" };
Output << MonthName[D.Month-1] << ' '<< D.Day << ", " << D.Year;
return Output;
}
int main()
{
Cdate D1,D2(12,27,1992),D3(0,99,8045);
cout << "D1 is " << D1 << endl<< "D2 is " << D2 << endl<< "D3 is " << D3 << endl << endl;
cout << "D2 += 7 is " << (D2 += 7) << endl << endl;
D3.Setday(2, 28, 1992);
cout << " D3 is " << D3 << endl;
cout << "++D3 is " << ++D3 << endl << endl;
Cdate D4(3, 18, 1969);
cout << "Testing the preincrement operator:" << endl << " D4 is " << D4 << endl;
cout << "++D4 is " << ++D4 << endl;
cout << " D4 is " << D4 << endl << endl;
cout << "Testing the postincrement operator:" << endl << " D4 is " << D4 << endl;
cout << "D4++ is " << D4++ << endl;
cout << " D4 is " << D4 << endl;
return 0;
}
thanks mọi người
Thông qua phân tích dữ liệu Google từ 86 quốc gia, mới đây, một công ty tại Anh đã công bố bảng xếp hạng kích tấc "cậu nhỏ" của các nước trên thế giới. Kết quả, hầu hết các nước xếp ở nhóm đầu của...
"Chim" của chàng trai Việt thuộc...