|
给你一个正确的程序吧:
// a.cpp : 定义控制台应用程序的入口点。
//
#include \"stdafx.h\"
#include<iostream>
using namespace std;
class Date
{
public:
void SetDate();
void Next(int,int ,int);
public:
int year;
int month;
int day;
int days;
};
void Date::SetDate()
{
cout<<\"Enter the year,month,day:\";
cin>>year>>month>>day;
for(;year<0;)
{
cout<<\"Enter a wrong year.\\n\";
cin>>year;
}
for(;month<0||month>12;)
{
cout<<\"Enter a wrong month\\n\";
cout<<\"Enter the month:\";
cin>>month;
}
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days=31;break;
case 4:
case 6:
case 9:
case 11:days=30;break;
case 2:if((year%4==0&&year%100!=0)||(year%400==0))
days=28;
else days=28;
}
}
void Date::Next(int year,int month,int day)//第二天
{
if(month==12&&day==days)//年底
{
++year;
month=1;
day=1;
}
else if(month!=12&&day==days)//月底
{
++month;
day=1;
}
else ++day;
cout<<\"The next day is \"<<day<<\"/\"<<month<<\"/\"<<year;
}
int _tmain(int argc, _TCHAR* argv[])
{
Date ymd;
ymd.SetDate();
ymd.Next(ymd.year,ymd.month,ymd.day);
char c=getchar();
while(c!=\'e\')
c=getchar();
return 0;
}
我在vc.net环境下运行通过了,实现了你要的功能。
你还可以扩展一下,在处理异常方面做得更好些。
自己慢慢体会。。。。 |
|