?? 日期類1.cpp
字號(hào):
#include<iostream>
using namespace std;
//前置聲明
class Date;
//友元函數(shù)聲明
long operator-(Date c1,Date c2);
bool operator>(Date c1,Date c2);
bool operator<(Date c1,Date c2);
bool operator==(Date c1,Date c2);
istream & operator>>(istream &is,Date &c);
ostream & operator<<(ostream &out,const Date &c);
//定義錯(cuò)誤類
class wrongdate{};
//定義Date類
class Date
{
public:
Date(int y=2000,int m=1,int d=1);
void setdate(int y=2000,int m=1,int d=1);
bool judgeyear();
bool judgemonth();
int month_day();
bool judgeday();
void judgedate();
friend long operator-(Date c1,Date c2);
Date operator+(int c2);
Date operator-(int c2);
Date operator++();
Date operator--();
Date operator++(int nouse);
Date operator--(int nouse);
Date operator+=(int c2);
friend bool operator>(Date c1,Date c2);
friend bool operator<(Date c1,Date c2);
friend bool operator==(Date c1,Date c2);
friend istream & operator>>(istream &is,Date &c);
friend ostream & operator<<(ostream &out,const Date &c);
void showdate();
private:
int year,month,day,m;
};
//定義構(gòu)造函數(shù)
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
//定義賦值函數(shù)
void Date::setdate(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
//判斷年是否為閏年
bool leap_year(int year)
{
return(year%4==0&&year%100!=0||year%400==0);
}
//定義類函數(shù)判斷年的合法性
bool Date::judgeyear()
{
return(year!=0);
}
//定義類函數(shù)判斷月的合法性
bool Date::judgemonth()
{
return(month>=1&&month<=12);
}
//定義類函數(shù)計(jì)算每個(gè)月返回的天數(shù)
int Date::month_day()
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:m=31;break;
case 4:
case 6:
case 9:
case 11:m=30;break;
case 2:
{
if(leap_year(year))
m=29;
else
m=28;
break;
}
}
return m;
}
//類函數(shù)判斷日的合法性
bool Date::judgeday()
{
return(day>0&&day<=month_day());
}
//類函數(shù)判斷日期的合法性
void Date::judgedate()
{
if((judgeyear()&&judgemonth()&&judgeday()));
else
throw wrongdate();
}
//重載友元函數(shù)計(jì)算兩個(gè)日期相差的天數(shù)
long operator-(Date c1,Date c2)
{
long dif1,dif2,dif=0;
for(;c1.month>1;c1.month--)
dif1=c1.day-1+c1.month_day();
for(;c2.month>1;c2.month--)
dif2=c2.day-1+c2.month_day();
while(c1.year!=c2.year)
{
if(c1.year>c2.year)
{
if(leap_year(c2.year))
dif+=366;
else dif+=365;
c2.year++;
}
if(c1.year<c2.year)
{
if(leap_year(c1.year))
dif+=366;
else dif+=365;
c1.year++;
}
}
return(c1.year>c2.year ? (dif+dif1-dif2):(dif+dif2-dif1));
}
//重載日期+天數(shù)
Date Date::operator+(int c2)
{
while(c2!=0)
{
if(month>12)
{
month-=12;
year++;
}
day++;
c2--;
if(day>month_day())
{
day-=month_day();
month++;
}
}
return(*this);
}
//重載日期-天數(shù)
Date Date::operator-(int c2)
{
while(c2!=0)
{
if(month<1)
{
month=12;
year--;
}
day--;
c2--;
if(day<1)
{
month--;
day=month_day();
}
}
return(*this);
}
//類函數(shù)重載前置++號(hào)
Date Date::operator++()
{
*this=*this+1;
return(*this);
}
//類函數(shù)重載前置--號(hào)
Date Date::operator--()
{
*this=*this-1;
return(*this);
}
//類函數(shù)重載后置++號(hào)
Date Date::operator++(int nouse)
{
Date c1;
c1=*this;
*this=*this+1;
return(c1);
}
//類函數(shù)重載后置--號(hào)
Date Date::operator--(int nouse)
{
Date c1;
c1=*this;
*this=*this-1;
return(c1);
}
//類函數(shù)重載+=號(hào)
Date Date::operator +=(int c2)
{
*this=*this+c2;
return(*this);
}
//友元函數(shù)重載〉判斷第一個(gè)日期比第二個(gè)日期后
bool operator>(Date c1,Date c2)
{
long dif1,dif2;
for(;c1.month>1;c1.month--)
dif1=c1.day-1+c1.m;
for(;c2.month>1;c2.month--)
dif2=c2.day-1+c2.m;
if(c1.year>c2.year)
return true;
if(c1.year==c2.year)
{
if(dif1>dif2)
return true;
else return false;
}
else return false;
}
//友元函數(shù)重載〈判斷第一個(gè)日期比第二個(gè)日期前
bool operator<(Date c1,Date c2)
{
long dif1,dif2;
for(;c1.month>1;c1.month--)
dif1=c1.day-1+c1.m;
for(;c2.month>1;c2.month--)
dif2=c2.day-1+c2.m;
if(c1.year<c2.year)
return true;
if(c1.year==c2.year)
{
if(dif1<dif2)
return true;
else return false;
}
else return false;
}
//友元函數(shù)重載==判斷兩個(gè)日期相等
bool operator==(Date c1,Date c2)
{
long dif1,dif2;
for(;c1.month>1;c1.month--)
dif1=c1.day-1+c1.m;
for(;c2.month>1;c2.month--)
dif2=c2.day-1+c2.m;
if(c1.year==c2.year&&dif1==dif2)
return true;
else return false;
}
//重載>>輸入函數(shù)
istream & operator>>(istream &is,Date &c)
{
is>>c.year>>c.month>>c.day;
return is;
}
//重載<<輸出函數(shù)
ostream & operator<<(ostream &out,const Date &c)
{
out<<c.year<<"年"<<c.month<<"月"<<c.day<<"日"<<endl;
return out;
}
//輸出年月日
void Date::showdate()
{
cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
//主函數(shù)
int main()
{
Date c1,c2,c3;
int i,j,k,l,m=0,n;
long num,dif;
int year,month,day;
char a[10];
//用字符串來輸入第一個(gè)日期,并判斷第一個(gè)日期的格式
cout<<"輸入第一個(gè)日期_年_月_日"<<endl;
cin>>a;
//計(jì)算出年與月之間的字符所在的位數(shù)
for(i=0;i<10;i++)
{
if(a[i]<'0'||a[i]>'9')
{
if(m==1)
{
l=i;//用l記錄第二個(gè)非數(shù)字字符的位置
m+=1;
}
if(m==0)
{
k=i;
m+=1;//用k記錄第一個(gè)非數(shù)字字符的位置
}
}
if(a[i]=='\n'&&m==2)
{
n=i;
break;//用n記錄結(jié)束字符的位置
}
}
if(a[k]!=a[l])
{
cout<<"type wrong"<<endl;
return(0);
}
//計(jì)算輸入的年
year=(int)(a[0])-48;
month=(int)(a[k+1])-48;
day=(int)(a[l+1])-48;
for(j=1;j<k;j++)
year=10*year+(int)(a[j])-48;
for(j=k+2;j<l;j++)
month=10*month+(int)(a[j])-48;
for(j=l+2;j<n;j++)
day=10*day+(int)(a[j])-48;
//輸入轉(zhuǎn)換后的第一個(gè)日期
c1.setdate(year,month,day);
c1.judgedate();
//用重載的>>來輸入第二個(gè)日期
cout<<"輸入第二個(gè)日期_年_月_日"<<endl;
cin>>c2;
c2.judgedate();
//用拋出錯(cuò)誤類來判斷日期的合法性
char ch='y';
while()
{
if(ch=='y')
try
{
c1.judgedate();
c2.judgedate();
}
catch(wrongdate)
{
cout<<"輸入的日期錯(cuò)誤;是否繼續(xù)(y/n)"<<endl;
cin>>ch;
}
else break;
}
if(ch!='y')
exit(1);
cout<<"輸入要加減的天數(shù)"<<endl;
cin>>num;
//測(cè)試兩個(gè)日期相差的天數(shù)
dif=c1-c2;
cout<<"兩個(gè)日期相差的天數(shù)"<<dif<<endl;
//測(cè)試重載的+號(hào)
c3=c1+num;
cout<<"日期加天數(shù)的結(jié)果是";
c3.showdate();
//測(cè)試重載的-號(hào)
c1.setdate(year,month,day);
c3=c1-num;
cout<<"日期減天數(shù)的結(jié)果是";
cout<<c3;
//測(cè)試重載的+=號(hào)
c1.setdate(year,month,day);
c1+=num;
cout<<"c1+="<<num<<"="<<c1<<endl;
//測(cè)試重載的后置++
c1.setdate(year,month,day);
cout<<"c1++="<<c1++<<endl;
//測(cè)試重載的前置++
c1.setdate(year,month,day);
cout<<"++c1="<<++c1<<endl;
//測(cè)試重載的后置--
c1.setdate(year,month,day);
cout<<"c1--="<<c1--<<endl;
//測(cè)試重載的前置--
c1.setdate(year,month,day);
cout<<"--c1="<<--c1<<endl;
//判斷兩個(gè)日期的大小
if(c1>c2)
cout<<"c1>c2"<<endl;
if(c1<c2)
cout<<"c1<c2"<<endl;
if(c1==c2)
cout<<"c1=c2"<<endl;
return(0);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -