改至别人的程序,仅供参考
你看看有用没:
#include<iostream.h>
#include<math.h>
class Date
{
public:
Date()//
{
year=0000;
month=00;
day=00;
}
Date(int year_input,int month_input,int day_input)//
{
year=year_input;
month=month_input;
day=day_input;
}
void date_set(int year_input,int month_input,int day_input)//
{
year=year_input;
month=month_input;
day=day_input;
}
void date_output()
{
cout<<year<<"--"<<month<<"--"<<day<<endl;
}
void date_plus()
{
if(day+1<=get_days(month,year))
day++;
else
{
month_plus();
day=1;
}
}
void date_down()
{
if(day-1>=1)
day--;
else
{
month_down();
day=get_days(month,year);
}
}
void month_plus()
{
if(month+1<=12)
month++;
else
{
year++;
month=1;
}
}
void month_down()
{
if(month-1>=1)
month--;
else
{
year--;
month=12;
}
}
int days_calculate(int year_input,int month_input,int day_input)//计算两个日期间的天数
{
int total_days=0;
int leapyears;
int i;
if(year_input==year)
{
for(i=1;i<month;i++)
total_days+=get_days(i,year);
total_days+=day;
for(i=1;i<month_input;i++)
total_days-=get_days(i,year_input);
total_days-=day_input;
return abs(total_days);
}
else if(year_input<year)
{
leapyears=0;
for(i=year_input;i<year;i++)
if(is_leap(i))
leapyears++;
total_days=(year-year_input)*365+leapyears;
for(i=1;i<month;i++)
total_days+=get_days(i,year);
total_days+=day;
for(i=1;i<month_input;i++)
total_days-=get_days(i,year_input);
total_days-=day_input;
return abs(total_days);
}
else
{
leapyears=0;
for(i=year;i<year_input;i++)
if(is_leap(i))
leapyears++;
total_days=(year_input-year)*365+leapyears;
for(i=1;i<month_input;i++)
total_days+=get_days(i,year_input);
total_days+=day_input;
for(i=1;i<month;i++)
total_days-=get_days(i,year);
total_days-=day;
return abs(total_days);
}
}
private:
int get_days(int month_,int year_)
{
switch(month_)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
break;
case 4:
case 6:
case 9:
case 11:
return 30;
break;
case 2:
if(is_leap(year_))
return 29;
else
return 28;
break;
default:
return 0;
}
}
bool is_leap(int year_)
{
if(year_%4)
return false;
else if(year_%100)
return true;
else if(year_%400)
return false;
return true;
}
private:
int year,month,day;
};
void main()
{
int m_year,m_month,m_Day,result;
char m_choice;
Date date(2008,11,26);
cout<<"
1.日期的设置'S';2.日期的显示'X';3.日期加一天'A';\n
4.日期减一天'J';5.日期算间隔'G';6.退出'Q';\n\n\n"<<endl;
while(m_choice!='Q')
{
cout<<"请选择:"; cin>>m_choice;
switch(m_choice)
{
case 'S':
cout<<"Y:"; cin>>m_year;
cout<<"M:"; cin>>m_month;
cout<<"D:"; cin>>m_Day;
date.date_set(m_year,m_month,m_Day);
break;
case 'X':
break;
case 'A':
date.date_plus();
break;
case 'J':
date.date_down();
break;
case 'G':
cout<<"
目前时间:\n\n"<<endl;
date.date_output();
cout<<"Y:"; cin>>m_year;
cout<<"M:"; cin>>m_month;
cout<<"D:"; cin>>m_Day;
result=date.days_calculate(m_year,m_month,m_Day);
cout<<"
间隔时间:"<<result<<endl;
break;
}
if(m_choice!='G')
date.date_output();
}
}