#include<iostream>
#include<string>
#define error 0
using namespace std;
class Time
{
private:
int hour;
int minute;
//int second;
public:
Time(int h,int m)
{
hour=h;
minute=m;
//second=s;
JC();
}
int JC()
{
int t=0;
if(minute>=60)
{
hour+=minute/60;
minute=minute%60;
if(hour>=24)
{
t=hour/24;
hour=hour%24;
return t;
}
}
else
{
if(hour>=24)
{
t=hour/24;
hour=hour%24;
return t;
}
}
return t;
}
int Add1(int n) //是以小时为单位
{
int t;
hour+=n;
t=JC();
return t;
}
int Add2(int n) //是以分钟为单位
{
int t;
minute+=n;
t=JC();
return t;
}
void Output()
{
//cout<<"hour:"<<hour<<endl;
//cout<<"minute:"<<minute<<endl;
if(hour==0)cout<<0<<0<<":";
else
cout<<hour<<":";
if(minute==0)cout<<0<<0;
else
cout<<minute;
if(hour>=12)cout<<"PM"<<endl;
else
cout<<"AM"<<endl;
}
};
class Calendar:public Time
{
private:
int month;
int date;
string Day[7];//char Day[7][10];//私有成员不能初始化,而且字符数组要想赋值,要么就一个一个的赋值
int Month[13];
int sumday;
int year;
public:
Calendar(int y,int m,int d,int h,int mi):Time(h,mi)
{
year=y;
month=m;
date=d;
Month[0]=0;Day[0]="日";
Month[1]=31;Day[1]="一";
Month[2]=28;Day[2]="二";
Month[3]=31;Day[3]="三";
Month[4]=30;Day[4]="四";
Month[5]=31;Day[5]="五";
Month[6]=30;Day[6]="六";
Month[7]=31;
Month[8]=31;
Month[9]=30;
Month[10]=31;
Month[11]=30;
Month[12]=31;
sumday=0;
}
bool Leapyear(int y)
{
if((y%400==0)||((y%4==0)&&(y%100!=0)))
{
Month[2]=29;
return true;
}
else
{
Month[2]=28;
return false;
}
}
int yearDay(int y)
{
if(Leapyear(y))return 366;
if(!Leapyear(y))return 365;
return error;
}
int Dateday()
{
return date;
}
int Monthday(int m)
{
return Month[m];
}
void Yearday(int y)
{
if(Leapyear(y))sumday=sumday+366;
else
sumday=sumday+365;
}
void Sumday() //返回从公元1年1月至今的天数一
{
int i=1;
for(;i!=year;i++)
{
Yearday(i);
}
Leapyear(year);
for(i=1;i<month;i++)
{
sumday+=Monthday(i);
}
sumday+=date;
}
int Return_sumday()
{
return sumday;
}
int Onedate1(int t) //返回这一年的第一天是星期几
{
t=sumday%7;
if(t==1) return 1;
if(t==2) return 2;
if(t==3) return 3;
if(t==4) return 4;
if(t==5) return 5;
if(t==6) return 6;
if(t==0) return 0;
return error;
}
void Output1() //往前推得输出年,月,日
{
cout<<"year,month,date:"<<year<<"年"<<month<<"月"<<date<<"日"<<" ";
int t;
cout<<"星期";
t=Onedate1(sumday);
if(t==7)cout<<Day[0]<<" ";
else
cout<<Day[t]<<" ";
Output();
}
int One_Year() //返回本年一共是多少天
{
int i,s1=0;
for(i=1;i<month;i++)
s1=s1+Monthday(i);
s1=s1+date;
return s1;
}
void operator +(int n)
{
sumday+=n;
int s,s2,s3,s4,i,k1,k2,k3;
if(Leapyear(year))s=366;
else
s=365;
s3=One_Year();
s4=s3+n;
if(s>=s4) //在本年内
{
s2=n-Monthday(month)+date;
if(s2<=0)date=date+n; //本月内
else
{
i=month;
while(s2!=0)
{
i++;
k1=s2%Monthday(i);
s2=s2/Monthday(i);
}
month=i;
date=k1;
}
}
else //不在本年内
{
s2=n-s+s3;
while(s2!=0) //确定第几年
{
year++;
k2=s2%yearDay(year);
s2=s2/yearDay(year);
}
month=0;
while(k2!=0) //确定第几个月
{
month++;
k3=k2%Monthday(month);
k2=k2/Monthday(month);
}
date=k3; //第几日
}
}
void operator -(int n) //刚开始没有重载好减号运算符,应为是倒过来算的,所以日起赋值必须是用这个月份的全部天数-求模余下来的天数
{
sumday-=n;
if(sumday<0)
{
cout<<"超出了天数的范围!"<<endl;
return ;
}
else
{
if(sumday==0)
{
cout<<"超出了天数的范围!"<<endl;
return ;
}
else
{
int s1,s2,k1,k2;
s1=One_Year();
s1=s1-n;
if(s1>0) //在本年内
{
s2=date-n;
if(s2>0) //在本月内
date-=n;
else //不在本月内
{
// sumday++; //??不在本年和本月的时候就sumday要加一次
if(s2<0)
{
s2=-s2;
while(s2!=0)
{
month--;
k1=s2%Monthday(month);
s2=s2/Monthday(month);
}
date=Monthday(month)-k1;
}
else
{
month--;
date=Monthday(month);
}
}
}
else //不再本年内
{
s2=-s1;
if(s2!=0)
{
while(s2!=0) //确定第几年
{
year--;
k1=s2%yearDay(year);
s2=s2/yearDay(year);
}
month=13;
while(k1!=0) //确定第几个月
{
month--;
k2=k1%Monthday(month);
k1=k1/Monthday(month);
}
date=Monthday(month)-k2; //第几日
}
else
{
year--;
month=12;
date=Monthday(month);
}
}
}
}
}
int operator -(Calendar b)
{
int n;
Sumday();
b.Sumday();
n=Return_sumday()-b.Return_sumday();
if(n<0)n=n*(-1);
return n;
}
};
int main()
{
cout<<"Please input year,month,date,hour,minute:";
int y,m,d,h,mi,hh;
cin>>y>>m>>d>>h>>mi;
Calendar a(y,m,d,h,mi);
a.Sumday();
cout<<"The sumday from the oneday is:"<<a.Return_sumday()<<endl;
a.Output1();
cout<<"input hour is 1,minute is 2:";
int kk;
cin>>kk;
int k;
if(kk==1)
{
cout<<"Please input add hour is:";
cin>>hh;
k=a.Add1(hh);
}
if(kk==2)
{
cout<<"Please input add minute is:";
cin>>hh;
k=a.Add2(hh);
}
if(k!=0)a+k;
cout<<"The sumday from the oneday is:"<<a.Return_sumday()<<endl;
a.Output1();
return 0;
}
c++课程设计 对日期及时间的基本运算(时间类和日期类) 使用重载
最新推荐文章于 2021-07-22 20:59:36 发布