题目一:
#include<iostream>
using namespace std;
class DateTime
{
private :
int year;
int month;
int day;
public:
DateTime(){}
DateTime(int y,int m,int d){year=y;month=m;day=d;}
void showDate()
{
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
int getyear(){return year;}
int getmonth(){return month;}
int getday(){return day;}
void setyear(){year+=1;}
void setmonth(){month+=1;}
void setday(){day+=1;}
void setmonth1(){month=1;}
void setday1(){day=1;}
DateTime data(DateTime D,int days)
{
int i=0,add;
for(i=0;i<days;i++)
{
D.setday();
switch(D.getmonth())
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
add=31;break;
case 2:
{
if(D.getyear()%4==0)
add=29;
else add=28;
}
break;
default:add=30;break;
}
if(D.getday()>add) {D.setday1();D.setmonth();if(D.getmonth()>12){D.setmonth1();D.setyear();}}
}
return D;
}
};
void main()
{
DateTime Birthday(1777,4,30);
DateTime now;
int n;
cout<<"请输入天数:"<<endl;
cin>>n;
now=Birthday.data(Birthday,n-1);
now.showDate();
}
好久没写过代码了!