万年历代码

更新时间:2023-09-04 07:28:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int everyMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
class PC
{
public:
void Input();//进行操作选择
void SetDays();//如果输入某年,进行'3'操作处理
void SetMonth();//如果选输入某年某月,进行'2'操作处理
void SetYear();//如果输入某年某月某日,进行'1'操作处理
bool IsLeap(int year);//判断是否是闰年
bool IsRight(int year,int month,int day);//判断输入是否合法
int GetDays(int year,int month,int day);//得到此日期前(从1年1月1号还是算)的天数
void Print(int year,int month);//输出到界面上
void Output(int year); //打印到文档out,一年的

};


void PC::Input()
{
char choose;
bool flag=true;
while(flag)
{
cout<<"欢迎使用万年历!请选择: (1/2/3/4/else)"<<endl;
cout<<"1:输入某年,显示该年的年历"<<endl;
cout<<"2:输入某年某月,显示该月的万年历"<<endl;
cout<<"3:输入某年某月某日,显示该日是星期几"<<endl;
cout<<"4:退出"<<endl;
cout<<"elseNumber:退出"<<endl;

cout<<"输入您的选择: ";
cin>>choose;
switch(choose)
{
case '1':SetYear();break;
case '2':SetMonth();break;
case '3':SetDays();break;
case '4':flag=false;break;
default:cout<<"输入错误,请重新输入";
}
}
}

bool PC::IsRight(int year,int month,int day) //判断日期输入是否正确
{
if(year<1 || year>9999 || month<1 || month>12)//年月是否正确
return false;
//日期是否正确
if(day<0)return false;
else if( day==29)
return((month==2 && IsLeap(year))|| month!=2);
else if(day==31)return(month==1 ||month==3 ||month==5 ||month==7 ||month==8 || month==10 || month==12);
else if(day>31) return false;
else return true;
}

void PC::SetDays()//如果输入某年某月某日,进行'1'操作处理
{
int weekDay;
int year,month,day;
cout<<" 请输入年_月_日:";
cin>>year>>month>>day;

while(!IsRight(year,month,day))
{
cout<<"输入错误,请重新输入年_月_日:";

cin>>year>>month>>day;
}

weekDay=GetDays(year, month,day)%7;

switch(weekDay)
{
case 0: cout<<year<<"年"<<month<<"月"<<day<<"日 星期日"<<endl; break;
case 1: cout<<year<<"年"<<month<<"月"<<day<<"日 星期一"<<endl; break;
case 2: cout<<year<<"年"<
<month<<"月"<<day<<"日 星期二"<<endl; break;
case 3: cout<<year<<"年"<<mo

nth<<"月"<<day<<"日 星期三"<<endl; break;
case 4: cout<<year<<"年"<<month<<"月"<<day<<"日 星期四"<<endl; break;
case 5: cout<<year<<"年"<<month<<"月"<<day<<"日 星期五"<<endl; break;
case 6: cout<<year<<"年"<<month<<"月"<<day<<"日 星期六"<<endl; break;
}

}

bool PC::IsLeap(int year)//判断是否是闰年
{
return ((year%4==0 && year%100!=0)||(year%400==0));
}


int PC::GetDays(int year,int month,int day)//得到此日前所有已经过的日子
{
int yearDays, monthDays,sum;
int accumulate=0;
for(int i=1;i<year;i++)
if(IsLeap(i))
accumulate++;
yearDays=accumulate+365*(year-1);

if((year%4==0 && year%100!=0)||(year%400==0)) everyMonth[2]=29;//如果是闰年,则2月为29天

for(int j=1;j<month;j++)
monthDays+=everyMonth[j];

sum=yearDays+monthDays+day;//所有已经过的日子之和
return sum;
}

void PC::SetMonth()//当输入的是年月时,处理
{
int weekDay;
int year,month,day;
cout<<" 请输入年_月:";
cin>>year>>month;
day=1;

while(!IsRight(year,month,day))//判断输入是否正确,设置day=1
{
cout<<"输入错误,请重新输入年_月:";
cin>>year>>month;
}
weekDay=GetDays(year, month,day)%7;
Print(year,month);
}

void PC::SetYear()
{
int year,month,day;
cout<<" 请输入年:";
cin>>year;
month=1;day=1;

while(!IsRight(year,month,day))//判断是否输入正确,若错误,请重新输入 .设置day=1,month=1,
{
cout<<"输入错误,请重新输入年:";
cin>>year;
}

cout<<"万年历打印在out文档中,请查看。"<<endl;
Output(year);

for(int k=1;k<=12;k++)//将12个月的万年历在界面上输出
Print(year,k);
}

void PC:: Print(int year,int month)//打印到界面上
{
int weekday,day=1;
cout<<" 公元"<<year<<"年"<<month<<"月"<<endl;
cout<<" SUN MON TUE WES THU FRI SAT"<<endl;
weekday=GetDays(year, month,day)%7;//所有的日期之和取余
switch(weekday)//输出处理
{
case 0: cout<<" "<<setw(6)<<setiosflags(ios::left)<<"1"; break;
case 1: cout<<" "<<setw(6)<<setiosflags(ios::left)<<"1";break;
case 2: cout<<" "<<setw(6)<<setiosflags(ios::left)<<"1";break;
case 3: cout<<" "<<setw(6)<<setiosflags(ios::left)<<"1";break;
case 4: cout<<" "<<setw(6)<<setiosflags(ios::left)<<"1";break;
case
5: cout<<" "<<setw(6)<<setiosflags(ios::left)<<"1";break;
case 6

: cout<<" "<<setw(6)<<setiosflags(ios::left)<<"1";break;
}

for(int i=2;i<=everyMonth[month];i++)
{
weekday=(++weekday)%7;
if(!weekday)
{cout<<endl; cout<<" "<<setw(6)<<setiosflags(ios::left)<<i;}
else
cout<<setw(6)<<setiosflags(ios::left)<<i;
}
cout<<endl;
}

void PC::Output(int year)//打印到out文本框内
{
int weekday,month=1,day=1;
int i,j;
ofstream outfile("out.txt",ios::out);
for(i=1;i<=12;i++)
{
outfile<<" 公元"<<year<<"年"<<i<<"月"<<endl;
outfile<<" SUN MON TUE WES THU FRI SAT"<<endl;
weekday=GetDays(year, i,day)%7;
switch(weekday)
{
case 0: outfile<<" "<<setw(6)<<setiosflags(ios::left)<<"1"; break;
case 1: outfile<<" "<<setw(6)<<setiosflags(ios::left)<<"1";break;
case 2: outfile<<" "<<setw(6)<<setiosflags(ios::left)<<"1";break;
case 3: outfile<<" "<<setw(6)<<setiosflags(ios::left)<<"1";break;
case 4: outfile<<" "<<setw(6)<<setiosflags(ios::left)<<"1";break;
case 5: outfile<<" "<<setw(6)<<setiosflags(ios::left)<<"1";break;
case 6: outfile<<" "<<setw(6)<<setiosflags(ios::left)<<"1";break;
}
for( j=2;j<=everyMonth[i];j++)
{
weekday=(++weekday)%7;
if(!weekday)
{outfile<<endl; outfile<<" "<<setw(6)<<setiosflags(ios::left)<<j;}
else
outfile<<setw(6)<<setiosflags(ios::left)<<j;
}


outfile<<endl;
outfile<<endl;

}
cout<<endl;
outfile.close();
}


int main()
{
PC pc;
pc.Input();
}


本文来源:https://www.bwwdw.com/article/ywgi.html

Top