万年历-更新详细程序代码

#include
#include
#include //windows头文件,包含所有与windows相关大函数库,如fflush(stdin)、cin.clear()等
#include //cin与cout的操纵运算子
using namespace std;
class Date //定义万年历 时间类Date
{
public:
Date() {year=1989;month=03;day=28;} //无参构造函数,默认生成数据成员值
Date(int y,int m,int d):year(y),month(m),day(d){} //有参构造函数,实例化函数列表
bool isleapyear() //定义瑞年判断成员函数
{return (year%4==0&&year%100!=0||year%400==0);}
bool judgelegal(); //申明所输入日期合法性成员函数
void setdate(int y,int m,int d) //定义设置时间的成员函数
{year=y;month=m;day=d;}
int Get_year(){return year;} //申明获取年份成员函数
int Get_month(){return month;} //申明获取月份成员函数
int Get_day(){return day;} //申明获取日期成员函数
int Get_currenttotalday() //定义获取所输入日期在当年天数的成员函数
{int sum=0;
days[1]=isleapyear()?29:28;
for(int i=0;isum+=days[i];
sum+=day;
return sum;
}
int GetTotaldays() //定义获取所输入日期离公元零年的总天数的成员函数
{int y=year-1,n;
n=y*365+y/4-y/100+y/400;
days[1]=isleapyear()?29:28;
for(int i=1;in+=days[i-1];
n+=day;
return n; //为生成万年历做准备
}
int Get_week(){return GetTotaldays()%7;} //定义获取所输入当天在一周内的星期数的成员函数
Date operator ++(); //前置++
Date operator ++(int); //后置++

private:
static int days[12]; //申明静态数据成员:一年十二月每月天数
int year;
int month;
int day;
};
int Date::days[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //定义静态成员
Date Date::operator ++() //定义前置++成员函数 ++dat
{days[1]=isleapyear()?29:28;
day++;
if(day>days[month-1])
{day=1;
month++;
}
if(month>12)
{month=1;
year++;
}
return *this;//内指针
}
/*Date Date::operator ++(int) //定义后置++成员函数 dat++
{Date t;
days[1]=isleapyear()?29:28;
day++;
if(day>days[month-1])
{day=1;
month++;
}
if(month>12)
{month=1;
year++;
}
return t;
}*/
bool Date::judgelegal() //定义所输入日期合法性的Date类成员函数
{if(year<0||year>3000||month<0||month>12||day<0||day>31||cin.bad()

)
return false;
else return true;
}
int main() //万年历程序 运行入口函数
{int year,month,day,week;
int flag=1,flg=0;
int f0=0,f1=0,f2=0;
Date dat;
while(flag==1) //flag用于手动输入功能选择标识,当输入值flag为1时表示继续生成万年历,否则退出整个程序wode
{
cin.clear(ios::goodbit); //清除cin流,并设置goodbit状态位,将一个出错的流的状态恢复为“好”。
fflush(stdin); //用来清空输入缓存,以便不影响后面输入的东西
fflush(stdout); //用来清空输出缓存,以便不影响后面输出的东西
cout<<"******万年历******欢迎使用******\n ※※※※※※※ \n※※※※※^-^Welcome^-※^※※※※※\n ※※※※※※※ \n";

/***************以下对分别输入的年、月、日进行判断打印*********************/
for(;;)
{
for(;;)
{
cin.clear(ios::goodbit);
cin.sync();
cout<<"请输入年(0~3000以内数字年份):\n";
cin>>year;
if(year<0||year>=3000||cin.fail()) cout<<"年份不合法!重新输入……\n";
else {f0=1;break;}
}
if(f0==1)
{
for(;;)
{
cin.clear(ios::goodbit); //清除std::cin的错误状态
cin.sync(); //清空输入缓冲区
cout<<"请输入月(1~12以内数字月份):\n";
cin>>month;
cin.sync();
cin.sync_with_stdio();
if(month<0||month>12||cin.fail()) cout<<"月份不合法!重新输入……\n";
else {f1=1;break;}
}
if(f1==1)
{
for(;;)
{
cin.clear(ios::goodbit);
cin.sync();
cout<<"请输入日:\n";
cin>>day;
cin.sync();
cin.sync_with_stdio();
switch(month) //对所输入的月份进行归类判断,从而进一步判断所输入打印日期的合法性,配合正确打印万年历
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: if(day>0&&day<=31&&cin.good()) {flg=1;f2=1;break;}
else {cout<<"所输入日期超出本月天数!!!\n";continue;}
case 4:
case 6:
case 9:
case 11: if(day>0&&day<=30&&cin.good()) {flg=1;f2=1;break;}
else {cout<<"所输入日期超出本月天数!!!\n";continue;}
case 2: if(year%4==0&&year%100!=0||year%400==0)
{
if(day>0&&day<=29&&cin.good()) {flg=1;f2=1;break;}
else {cout<<"所输入日期超出本月天数("<}
else
{
if(day>0&&day<=28&&cin.good()) {flg=1;f2=1;break;}
else {cout<<"所输入日期超出本月天数("<}

default : cout<<"

所输入月份不合法,超出1~12范围!!!\n";break;
}
if(f2==1) break;
}
}
}
break;
}//第一个for语句end
if(flg==1&&month>0&&month<=12) //若判断OK,则设置年月日并统计日期,同时打印万年历
{
dat.setdate(year,month,day);
if(dat.isleapyear())
{cout<<"\n\n"<cout<<"这天是"<}
else
{cout<<"\n\n"<cout<<"这天是"<}
cout<<"\n倒数三秒揭晓万年历……\n";
Sleep(3000);
dat.setdate(year,1,1);
if(!dat.judgelegal()) cout<<"……\n";
else
{week=dat.Get_week();
while(dat.Get_year()==year)
{day=dat.Get_day();
if(day==1||week==0)
if(day==1) //在打印万年历时,若day值出现为1,则打印月份排头
{cout<<"\n"<<" ============"<<""<cout<<" 日"<cout<<"\n"<}
if(week==0) //如果打印一周日期完毕后,则开始为下一周打印做准备
cout<<"\n";
cout<week=(week+1)%7; //计算周数
++dat; //每打印完一日,则天数++
}
}
cout<<"\n\n******************************";
cout<<"\n※※※※※※※※※※※※※※※";
cout<<"\n※※※※※功能★选择※※※※※ \n\n 1:继续 0:退出\n";
for(;;) //在进行功能选择时,须要进行功能选择判断,从而更好的达到功能选择的实现
{
cin.clear(ios::goodbit); //清除cin流,并设置goodbit状态位,将一个出错的流的状态恢复为“好”。
fflush(stdin); //用来清空输入缓存,以便不影响后面输入的东西
fflush(stdout); //用来清空输出缓存,以便不影响后面输出的东西
cout<<"\n℡№:";
cin>>flag;
if(cin.good()) //输入状态良好,类型匹配
{
if(flag!=0&&flag!=1)
cout<<"\n选择错误,请选择功能键(1或0) \n1:继续 0:退出\n";
else break; //此处功能键选择OK,则退出功能选择判断for语句,而进入下一个万年历打印作业
}
else //否则,提示所输入的是非同类型数据
cout<<"请输入0~9整数数字!!!\n";
}//for语句
//break;
}//if(cin.bad()&&year<0&&year>3000)语句end


else cout<<"输入数据不合法!!!\n"; ////若判断NG,则重新设置年月日(if(cin.bad()&&year<0&&year>3000))


}//while(flag==1)语句end
cout<return 0;
}//main函数end

相关文档
最新文档