幫我看下是不是代碼不對,29/2/2001日期+1就成30/2/2001了,而28/2/2001日期+1是1/3/2001
#include
using namespace std;
class Date
{
public:
Date()
{year=0;
month=0;
day=0;
}
void show_date();
void add_date();
void set_date();
private:
int year;
int month;
int day;
};
void Date::set_date()
{cin>>day;
cin>>month;
cin>>year;
}
void Date::show_date()
{cout<<day<<"/"<<month<<"/"<<year<<endl;}
void Date::add_date()
{if (year/4==0)
{if (month==1||month==3||month==5||month==7||month==8||month==10)
{if (day==31)
{day=1;month++;}
else day++;
}
else if (month==4||month==6||month==9||month==11)
{if (day==30)
{day=1;month++;}
else day++;
}
else if (month==12)
{ if(day==31)
{day=1;month=1;year++;}
else day++;
}
else {if (day==29)
{day=1;month++;}
else day++;}
}
else
{if (month==1||month==3||month==5||month==7||month==8||month==10)
{if (day==31)
{day=1;month++;}
else day++;
}
else if (month==4||month==6||month==9||month==11)
{if (day==30)
{day=1;month++;}
else day++;
}
else if (month==12)
{ if(day==31)
{day=1;month=1;year++;}
else day++;
}
else {if (day==28)
{day=1;month++;}
else day++;}
}
}
int main()
{cout<<"please enter the date:day/month/year"<<endl;
Date t1;
t1.set_date();
cout<<"the original date:"<<endl;
t1.show_date();
t1.add_date();
cout<<"the alerant date:"<<endl;
t1.show_date();
Date t2;
t2.set_date();
cout<<"the original date:"<<endl;
t2.show_date();
t2.add_date();
cout<<"the alerant date:"<<endl;
t2.show_date();
Date t3;
t3.set_date();
cout<<"the original date:"<<endl;
t3.show_date();
t3.add_date();
cout<<"the alerant date:"<<endl;
t3.show_date();
Date t4;
t4.set_date();
cout<<"the original date:"<<endl;
t4.show_date();
t4.add_date();
cout<<"the alerant date:"<<endl;
t4.show_date();
應該是取模而不是整除啊。
if (year%4==0)