?? 使用友元函數計算兩個日期之間間隔的天數.txt
字號:
使用友元函數計算兩個日期之間間隔的天數。
#include<iostream.h>
class Date
{ private:
int Year,Month,Day;
public:
Date(int NewY,int NewM,int NewD);
int GetYear(){return Year;}
int GetMonth(){return Month;}
int GetDay(){return Day;}
bool IsLeapYear();
friend long AllDays(Date &a);
};
Date::Date(int NewY,int NewM,int NewD)
{ Year=Month=Day=1;
if(NewY>0&&NewY<10000) Year=NewY;
if(NewM>0&&NewM<12) Month=NewM;
if(NewD>0&&NewD<32) Day=NewD;
}
bool Date::IsLeapzYear()
{ return (Year%4==0&&Year%100!=0)||(Year%400==0);}
long AllDays(Date &a)
{ long days=0;
for(int i=1;i<a.Year;i++)
if(i%4==0&&i%100!=0||i%400==0) days+=366;
else days+=365;
for(int i=1;i<a.Month;i++)
{ if(i==1||i==3||i==5||i==7||i==8||i==10||i==12) days+=31;
else if(i==4||i==6||i==9||i==11) days+=30;
else if(a.IsLeapYear()) days+=29;
else days+=28;
}
days+=a.Day;
return days;
}
void Distance(Date &a,Date &b)
{ long daysa=AllDays(a);
long daysb=AllDays(b);
long i;
if(daysa>daysb) i=daysa-daysb;
else i=daysb-daysa;
cout<<a.GetYear()<<"年"<<a.getMonth()<<"月"<<a.GetDay()<<"日距離"
<<b.GetYear()<<"年"<<b.GetMonth()<<"月"<<b.GetDay()<<"日有"<<i<<"天。"<<endl;
}
void main()
{ Date d1(2005,3,11);
Date d2(2000,1,1);
Distance(d1,d2);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -