/*设计一个日期类Date,包括年、月、日等私有成员。要求实现日期的基本运算,例如某日期加上天数或减去天数
,两日期相减的天数等。
实现要求:
实现运算符加与减的重载
设计一个日期类Date,包括年、月、日等私有成员。要求实现日期的基本运算,例如某日期加上天数或减去天数,
两日期相减的天数等。
实现要求:
实现运算符加与减的重载
设计一个时间类,要求能够在时间类上实行以分钟为单位的加减运算,同时当分钟增或减的变化应体现日期变化。
如:2012年11月28日12:10PM 增加12小时后,应变为2012年11月29日00:10AM
*/
#include
#include
using namespace std;
bool prime(int year);
class Date
{
protected:
int year;
int mouth;
int day;
int mouths[12];
public:
Date();
void setDate();
void output();
Date operator +(Date D);
int operator -(Date D);
Date operator +(int D);
Date operator -(int D);
};
class Time :public Date
{
int hour;
int minute;
public :
void setTime();
void output();
Time operator + (int T);
Time operator - (int T);
Time daojishi ();
};
void Time::setTime()
{
cout<
cin>>year>>mouth>>day;
cin>>hour>>minute;
}
void Time::output()
{
cout<
}
Time Time::operator + (int T) //时间加上分钟数
{
Time TT;
int num1=0,sum0=0,sum1=0;
num1=T/60; //num1小时数
T=T%60;
sum1=T+minute;
if(sum1>=60)
{
hour++;
sum1=sum1-60;
}
sum0=hour+num1;
while(sum0>=24) //想家小时>24小时,则天数加1
{
day++;
sum0=sum0-24;
}
while(day>mouths[(mouth-1)%12]) //如果天数加起来大于该月的天数,则天数减去该月的天数,月份加1
{
if(prime(year+mouth/12))
mouths[1]=29;
else
mouths[1]=28;
day=day-mouths[mouth-1];
mouth++;
}
while(mouth>=12) //加起来的月份大于12,则月份减去12,年份加1
{
year++;
mouth=mouth-12;
}
TT.hour=sum0;
TT.minute=sum1;
TT.day=day;
TT.mouth=mouth;
TT.year=year;
return TT;
}
Time Time::operator - (int T) //时间减去分钟数
{
Time TT;
int num1=0,sum0=0,sum1=0;
sum0=hour*60+minute;
if(sum0>=T)
{
num1=T/60;
T=sum0-T;
T=T%60;
sum0=hour-num1;
TT.day=day;
TT.mouth=mouth;
TT.year=year;
TT.hour=sum0;
TT.minute=T;
return TT;
}
else
{
while(sum0
{
day--;
sum0=sum0+24*60;
}
sum0=sum0-T; //剩余总分钟数
T=sum0/60; //剩余小时数
sum0=sum0%60; //剩余分钟数
while(day<=0)
{
if(prime(year+mouth/12))
mouths[1]=29;
else
mouths[1]=28;
mouth--;
day=day+mouths[(mouth-1+12)%12];
}
while(mouth<1)
{
year--;
mouth=mouth+12;
}
TT.day=day;
TT.mouth=mouth;
TT.year=year;
TT.hour=T;
TT.minute=sum0;
}
return TT;
}
Time Time::daojishi() //倒计时
{
Time T1,T2,TT;Date D1,D2;
int num0=0,num1=0,num2=0,num_day=0;
T1.setTime(); //当前时间
T2.setTime(); //未来时间
D1=T1;D2=T2;
num_day=D2-D1;
num0=T1.hour*60+T1.minute; // 当前时间一天多出来的分钟数
num1=T2.hour*60+T2.minute; //未来时间一天多出来的分钟数
if(num1>=num0)
{
num1=num1-num0;
}
else
{
num1=num1+24*60-num0;
num_day--;
}
hour=num1/60; //num0小时数
minute=num1%60; //多出来的分钟数
int i=60;
while(num_day!=0||hour!=0||minute!=0)
{
while(num_day>=0)
{
hour=23;
while(hour>=0)
{ minute=59;
while(minute>=0)
{ i=60;
while(i>0)
{
cout<
system("cls");
}
minute--;
}
hour--;
}
num_day--;
}
}
return TT;
}
void Date::setDate()
{
cin>>year>>mouth>>day;
}
void Date::output()
{
cout<
}
Date::Date()
{
for(int i=0;i<12;i++)
if(i==0||i==2||i==4||i==6||i==7||i==9||i==11)
mouths[i]=31;
else
mouths[i]=30;
mouths[1]=28;
}
Date Date::operator +(Date D) //日期相加
{
Date DD;
int sum_day0=D.day,sum_day1=day,sum_day=0;
while(D.mouth--)
{
if(D.mouth!=0)
sum_day0=D.mouths[D.mouth-1]+sum_day0;
else
sum_day0=sum_day0;
}
while(mouth--)
{
if(mouth!=0)
sum_day1=sum_day1+mouths[mouth-1];
else
sum_day1=sum_day1;
}
sum_day=sum_day0+sum_day1;
if(prime(year+D.year))
{
if(sum_day>=366)
{
DD.year=D.year+year+1;
sum_day=sum_day-366;
}
else
DD.year=D.year+year;
}
else
{
if(sum_day>=365)
{
DD.year=D.year+year+1;
sum_day=sum_day-366;
}
else
DD.year=D.year+year;
}
if(prime(DD.year))
mouths[1]=29;
for(int i=0;sum_day>=mouths[i];i++)
{
sum_day=sum_day-mouths[i];
}
DD.mouth=i+1;
DD.day=sum_day;
return DD;
}
int Date::operator -(Date D) //日期相减
{
Date DD; //D被减日期
int sum_day0=D.day,sum_day1=day,sum_day=0;
int year0=D.year;
while(D.year
{
if(prime(D.year))
sum_day1=sum_day1+366;
else
sum_day1=sum_day1+365;
D.year++;
}
while(mouth--) //减的日期
{
if(prime(year))
mouths[1]=29;
else
mouths[1]=28;
if(mouth!=0)
sum_day1=sum_day1+mouths[mouth-1];
else
sum_day1=sum_day1;
}
while(D.mouth--) //被减日期
{
if(prime(year0))
D.mouths[1]=29;
else
D.mouths[1]=28;
if(D.mouth!=0)
sum_day0=D.mouths[D.mouth-1]+sum_day0;
else
sum_day0=sum_day0;
}
return sum_day1-sum_day0;
}
Date Date::operator +(int D) //日期加天数
{
Date DD;
int sum_day0=day,year_day=0,sum_day=0;
while(mouth--)
{
if(prime(year))
mouths[1]=29;
if(mouth!=0)
sum_day0=sum_day0+mouths[mouth-1];
else
sum_day0=sum_day0;
}
sum_day=sum_day0+D;
if(prime(year))
year_day=366;
else
year_day=365;
while(sum_day>=year_day)
{
if(prime(year))
{
mouths[1]=29;
sum_day=sum_day-366;
}
else
{
mouths[1]=28;
sum_day=sum_day-365;
}
year++;
if(prime(year))
year_day=366;
else
year_day=365;
}
if(prime(year))
mouths[1]=29;
for(int i=0;sum_day>mouths[i];i++)
sum_day=sum_day-mouths[i];
DD.year=year;
DD.mouth=i+1;
DD.day=sum_day;
return DD;
}
Date Date::operator -(int D) //日期减天数
{
Date DD;
int sum_day0=day,year_day=0,sum_day=0;
if(prime(year))
mouths[1]=29;
while(mouth--)
{
if(mouth!=0)
sum_day0=sum_day0+mouths[mouth-1];
else
sum_day0=sum_day0;
}
if(sum_day0>=D)
{
if(prime(year))
mouths[1]=29;
sum_day=sum_day0-D;
for(int i=0;sum_day>mouths[i];i++)
sum_day=sum_day-mouths[i];
DD.year=year;
DD.mouth=i+1;
DD.day=sum_day;
return DD;
}
else
{
while(sum_day0
{
if(prime(--year))
{
sum_day0=sum_day0+366;
}
else
sum_day0=sum_day0+365;
}
sum_day=sum_day0-D;
if(prime(year))
mouths[1]=29;
else
mouths[1]=28;
for(int i=0;sum_day>mouths[i];i++)
sum_day=sum_day-mouths[i];
DD.year=year;
DD.mouth=i+1;
DD.day=sum_day;
return DD;
}
}
bool prime(int year)
{
if((year%4==0&&year%100!=0)||year%400==0)
return true;
else
return false;
}
void show()
{
cout<
cout<
cout<
cout<
}
int main()
{
Date D1,D2,DD;
Time T1,T2,TT;
char ch='Y';
int d1,d2,t1,t2,i;
system("color 71");
cout<
cout<
cout<
cout<
cout<
while(ch=='Y')
{
cout<>i;
switch(i)
{
case 1:D1.setDate();D2.setDate();DD=D1+D2;DD.output();break;
case 2:D1.setDate();D2.setDate();d2=D1-D2;cout<
case 3:D1.setDate();cout<>d1;DD=D1+d1;DD.output();break;
case 4:D1.setDate();cout<>d2;DD=D1-d2;DD.output();break;
case 5:T1.setTime();cout<>t1;TT=T1+t1;TT.output();break;
case 6:T1.setTime();cout<>t2;TT=T1-t2;TT.output();break;
case 7:system("cls");show();break;
case 8:ch='N';break;
case 9:T1.daojishi();break;
}
cout<
}
return 0;
}
/*
1
2000 1 20
2000 1 20
2
2012 11 28
2000 1 20
2012 12 12
2011 11 11
2012 12 31 23 50
310
2013 1 1 5 0
310
*/