📄 日期类1.cpp
字号:
#include<iostream>
using namespace std;
//前置声明
class Date;
//友元函数声明
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);
//定义错误类
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;
};
//定义构造函数
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
//定义赋值函数
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);
}
//定义类函数判断年的合法性
bool Date::judgeyear()
{
return(year!=0);
}
//定义类函数判断月的合法性
bool Date::judgemonth()
{
return(month>=1&&month<=12);
}
//定义类函数计算每个月返回的天数
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;
}
//类函数判断日的合法性
bool Date::judgeday()
{
return(day>0&&day<=month_day());
}
//类函数判断日期的合法性
void Date::judgedate()
{
if((judgeyear()&&judgemonth()&&judgeday()));
else
throw wrongdate();
}
//重载友元函数计算两个日期相差的天数
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));
}
//重载日期+天数
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);
}
//重载日期-天数
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);
}
//类函数重载前置++号
Date Date::operator++()
{
*this=*this+1;
return(*this);
}
//类函数重载前置--号
Date Date::operator--()
{
*this=*this-1;
return(*this);
}
//类函数重载后置++号
Date Date::operator++(int nouse)
{
Date c1;
c1=*this;
*this=*this+1;
return(c1);
}
//类函数重载后置--号
Date Date::operator--(int nouse)
{
Date c1;
c1=*this;
*this=*this-1;
return(c1);
}
//类函数重载+=号
Date Date::operator +=(int c2)
{
*this=*this+c2;
return(*this);
}
//友元函数重载〉判断第一个日期比第二个日期后
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;
}
//友元函数重载〈判断第一个日期比第二个日期前
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;
}
//友元函数重载==判断两个日期相等
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;
}
//重载>>输入函数
istream & operator>>(istream &is,Date &c)
{
is>>c.year>>c.month>>c.day;
return is;
}
//重载<<输出函数
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;
}
//主函数
int main()
{
Date c1,c2,c3;
int i,j,k,l,m=0,n;
long num,dif;
int year,month,day;
char a[10];
//用字符串来输入第一个日期,并判断第一个日期的格式
cout<<"输入第一个日期_年_月_日"<<endl;
cin>>a;
//计算出年与月之间的字符所在的位数
for(i=0;i<10;i++)
{
if(a[i]<'0'||a[i]>'9')
{
if(m==1)
{
l=i;//用l记录第二个非数字字符的位置
m+=1;
}
if(m==0)
{
k=i;
m+=1;//用k记录第一个非数字字符的位置
}
}
if(a[i]=='\n'&&m==2)
{
n=i;
break;//用n记录结束字符的位置
}
}
if(a[k]!=a[l])
{
cout<<"type wrong"<<endl;
return(0);
}
//计算输入的年
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;
//输入转换后的第一个日期
c1.setdate(year,month,day);
c1.judgedate();
//用重载的>>来输入第二个日期
cout<<"输入第二个日期_年_月_日"<<endl;
cin>>c2;
c2.judgedate();
//用抛出错误类来判断日期的合法性
char ch='y';
while()
{
if(ch=='y')
try
{
c1.judgedate();
c2.judgedate();
}
catch(wrongdate)
{
cout<<"输入的日期错误;是否继续(y/n)"<<endl;
cin>>ch;
}
else break;
}
if(ch!='y')
exit(1);
cout<<"输入要加减的天数"<<endl;
cin>>num;
//测试两个日期相差的天数
dif=c1-c2;
cout<<"两个日期相差的天数"<<dif<<endl;
//测试重载的+号
c3=c1+num;
cout<<"日期加天数的结果是";
c3.showdate();
//测试重载的-号
c1.setdate(year,month,day);
c3=c1-num;
cout<<"日期减天数的结果是";
cout<<c3;
//测试重载的+=号
c1.setdate(year,month,day);
c1+=num;
cout<<"c1+="<<num<<"="<<c1<<endl;
//测试重载的后置++
c1.setdate(year,month,day);
cout<<"c1++="<<c1++<<endl;
//测试重载的前置++
c1.setdate(year,month,day);
cout<<"++c1="<<++c1<<endl;
//测试重载的后置--
c1.setdate(year,month,day);
cout<<"c1--="<<c1--<<endl;
//测试重载的前置--
c1.setdate(year,month,day);
cout<<"--c1="<<--c1<<endl;
//判断两个日期的大小
if(c1>c2)
cout<<"c1>c2"<<endl;
if(c1<c2)
cout<<"c1<c2"<<endl;
if(c1==c2)
cout<<"c1=c2"<<endl;
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -