VC++问题!急~~~

来源:百度知道 编辑:UC知道 时间:2024/09/21 08:35:19
我自己再写了一编~没有编译错误了~
我要实现的是对所给日期加一天,然后输出~
请高手帮我改一改~看我有什么错误~请指正~谢谢!
#include <iostream.h>
class Date
{
private:
int year;
int month;
int day;
public:
Date()
{
year=2007;
month=5;
day=28;
}
void setDate()
{
cin>>year>>month>>day;
}
Date operator++();
Date operator++(int);
friend ostream & operator<<(ostream &,Date &);
friend istream & operator>>(istream &,Date &);
};
ostream & operator<<(ostream & output,Date &c)
{
output<<c.year<<"/"<<c.month<<"/"<<c.day<<endl;
return output;
}
istream & operator>>(istream & input,Date &c)
{
input>>c.year>>c.month>>c.day;
return input;
}
Date Date::operator++()
{
if(day==27&&month==2)
{
if((year%4==0&&year%100!=

这个函数有问题Date Date::operator++();你只考虑了边界情况,忽略了正常情况的++day;
改成如下:
Date Date::operator++()
{
if(day==27&&month==2)
{
if((year%4==0&&year%100!=0)||(year%400==0))
{
++day;
}
else
{
++month;
day=1;
}
}else if(day==30&&(month==1||month==3||month==5||month==7||month==8||month==10||month==12))
{
++day;
}else if(day==31&&(month==4||month==6||month==5||month==9||month==11))
{
++month;
day=1;
}
else
++day;

return *this;
}

不知道你这程序要干什么,我试了一下,能编译运行啊,