abc1078278096 于 2016.04.17 20:08 提问

C++类输出年月日，小白，我没有C币可以悬赏，可以么？

class Date
{

int day, month, year;
void InitDate(int d, int m, int y);
//要求验证day,month的有效性，-2000<=year<=3000
};

1.InitDate(int d, int m, int y)需要验证year\month\day的有效性，避免出现(5678,14,37)(2014,2,29)等无效日期。

4个回答

webcq   2016.04.17 20:11

u012155923   2016.04.17 20:23

/*

class Date
{

int day, month, year;
void InitDate(int d, int m, int y);
//要求验证day,month的有效性，-2000<=year<=3000
};

1.InitDate(int d, int m, int y)需要验证year\month\day的有效性，避免出现(5678,14,37)(2014,2,29)等无效日期。

*/
#include
#include
using namespace std;
class Date
{
private:
int day, month, year;

public:
void InitDate(int d, int m, int y);
void PrintDate(){
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
//要求验证day,month的有效性，-2000<=year<=3000
};

void Date::InitDate(int d,int m,int y){

``````bool isDate=true;
Date::day=d;
Date::month=m;
Date::year=y;

//处理year
if(year<-2000||year>3000) {
isDate=false;
}
//处理months
if(month<=0||month>12){
isDate=false;
}
//处理day
int days[12]={
31,28,31,30,31,30,31,31,30,31,30,31
};

bool Leapyear=(year%400==0)||(year%4==0&&year%100!=0);
if(Leapyear){
days[1]=29;
}
if(day<=0||day>days[m-1]){
isDate=false;
}
if(!isDate){
cout<<"This is a Wrong Date!";
}
``````

}

Date::year+=year;
}

``````int tmpmonth=Date::month+month;
if(tmpmonth<0){
Date::year-=int(ceil(abs(tmpmonth)/12.0));
Date::month=12-abs(tmpmonth)%12;
}
else if(tmpmonth>12){
Date::year+=int(floor(tmpmonth/12));
Date::month+=month%12;
}
else{
Date::month=tmpmonth;
}
``````

}

``````int tmpday=Date::day+day;
int days[12]={
31,28,31,30,31,30,31,31,30,31,30,31
};

bool Leapyear=(Date::year%400==0)||(Date::year%4==0&&Date::year%100!=0);
if(Leapyear){
days[1]=29;
}
int sumday=0;
if(tmpday<0){
if(Date::month==1){
Date::year-=floor(abs(tmpday)/365);
}
for(int i=Date::month-2;i>=0;i--){
sumday+=days[i];
if(sumday+tmpday<days[i]){
Date::month=i+1;
Date::day=sumday+tmpday;
}
}
sumday=0;
}
else{
if(tmpday<days[Date::month-1]){
Date::day=tmpday;
}
else{
if(Date::month==12){
Date::year+=int(ceil(tmpday/365));
}
for(int i=Date::month-1;i<=12-1;i++){
sumday+=days[i];
if(sumday-tmpday<days[i]){
Date::month=i+1;
Date::day=sumday-tmpday;
}
else if(i=11){
i=0;
}
}
}
}
``````

}
int main(){
Date date;
date.InitDate(11,11,2012);
date.PrintDate();
}

abc1078278096 看不懂

abc1078278096 16 6 D:\编程\007.cpp [Error] extra qualification 'Date::' on member 'InitDate' [-fpermissive]

abc1078278096   2016.04.17 20:18