万年历(程序阅读)

来源:互联网 发布:主站域名该填什么 编辑:程序博客网 时间:2024/05/23 01:14

/*
* Copyright (c) 2012, 烟台大学计算机学院
* All rights reserved.
* 作    者:庄子豪
* 完成日期:2013 年  3月30日
* 版 本 号:v1.0
*
* 输入描述:
* 问题描述:
* 程序输出:
* 问题分析:
 *算法设计:

#include<iostream>#include<iomanip>using namespace std;class Date{private:int year;int month;int day;int monthDay[12];public:Date(int y=1,int m=1,int d=1):year(y),month(m),day(d){monthDay[0]=monthDay[2]=monthDay[4]=monthDay[6]=monthDay[7]=monthDay[9]=monthDay[11]=31;monthDay[1]=28;monthDay[3]=monthDay[5]=monthDay[8]=monthDay[10]=30;}void SetYear(int y){year=y;}void SetMonth(int m){month=m;}void SetDay(int d){day=d;}int GetYear()const{return year;}int GetMonth()const{return month;}int GetDay()const{return day;}int GetMonthDay(const int i){if(i==2&&Isleapyear(year))return 29;return monthDay[i-1];}bool Isleapyear(int y)//判断是否为闰年{return ((y%4==0 && y%100!=0)||(y%400==0));}int GetYearDays(int y)//年份y的天数{if(Isleapyear(y))return 366;return 365;}int DateToNum()//给定日期,返回天数{int sum=0;int i=0;for(i=1;i<year;i++)sum+=GetYearDays(i);if(Isleapyear(year))monthDay[1]=29;elsemonthDay[1]=28;for(int j=1;j<month;j++)sum+=monthDay[j-1];return sum+day;}Date NumToDay(int n)//给定天数返回日期{Date d(1,1,1);for(;n>=GetYearDays(d.year);d.year++)n-=GetYearDays(d.year);if(Isleapyear(d.year))monthDay[1]=29;elsemonthDay[1]=28;for(;n>=monthDay[d.month];d.month++)n-=monthDay[d.month];d.day=n;return d;}void OutputYearDate(int y)//给定年份Y,输出年份Y的日历{if(y<=0)return;int i=0;int j=0;Date d;d.year=y;d.day=1;cout<<endl<<endl<<setw(20)<<y<<"年"<<endl;while(i++<12){cout<<endl<<endl<<setw(15)<<i<<"月"<<endl;cout<<endl;cout<<setw(5)<<"SUN";cout<<setw(5)<<"MON";cout<<setw(5)<<"TUE";cout<<setw(5)<<"WED";cout<<setw(5)<<"THU";cout<<setw(5)<<"FRI";cout<<setw(5)<<"SAT";cout<<endl;cout<<"----------------------------------------------"<<endl;j=0;d.month=i;cout<<setw(5*(d.DateToNum()%7)+5)<<1;for(j=1;j<monthDay[i-1];j++){if((j+(d.DateToNum()%7))%7==0)cout<<endl;cout<<setw(5)<<j+1;}cout<<endl;cout<<"----------------------------------------------"<<endl;}}void OutputYearMonthDate(int y,int m)//给定年y月m,输出y年m月的日历{if(y<=0)return;Date d(y,m,1);cout<<endl<<endl<<setw(15)<<m<<"月"<<endl;cout<<endl;cout<<setw(5)<<"SUN";cout<<setw(5)<<"MON";cout<<setw(5)<<"TUE";cout<<setw(5)<<"WED";cout<<setw(5)<<"THU";cout<<setw(5)<<"FRI";cout<<setw(5)<<"SAT";cout<<endl;cout<<"----------------------------------------------"<<endl;cout<<setw(5*(d.DateToNum()%7)+5)<<1;for(int j=1;j<monthDay[m-1];j++){if((j+(d.DateToNum()%7))%7==0)cout<<endl;cout<<setw(5)<<j+1;}cout<<endl;cout<<"----------------------------------------------"<<endl;}static int Week(Date d){if(d.DateToNum()%7==0)return 7;return(d.DateToNum()%7);}};int main(void){Date d;int number;char choose;bool flag=true;while(flag){cout<<endl<<"       ===========>>>>使用说明<<<<=========="<<endl<<endl;cout<<"                       | |"<<endl;cout<<"                       | |"<<endl;cout<<"                       | |"<<endl;cout<<"             设公元年月日为第一天"<<endl<<endl;cout<<"          输入==》  1打印  某年  的日历:"<<endl<<endl;cout<<"          输入==》  2查看某年是否是闰年:"<<endl<<endl;cout<<"          输入==》  3查看某日期是星期几:"<<endl<<endl;cout<<"          输入==》  4查看某年某月的日历"<<endl<<endl;cout<<"          输入==》  5给定天数  返回日期:"<<endl<<endl;cout<<"          输入==》  6    退     出     :"<<endl<<endl;cout<<"请输入您的选择:";cin>>choose;while(getchar()!='\n');if(!cin){cin.clear();cin.sync();cout<<"输入错误";continue;}switch(choose){case '1':cout<<"输入年:";cin>>number;d.SetYear(number);if(d.GetYear()<=0){cout<<"输入年份有错,返回。"<<endl;break;}d.OutputYearDate(d.GetYear());break;case '2':cout<<"输入年:";cin>>number;d.SetYear(number);if(d.GetYear()<=0){cout<<"输入年份有误,返回。"<<endl;break;}if(d.Isleapyear(d.GetYear()))cout<<"闰年。"<<endl;elsecout<<"非闰年。";break;case'3':cout<<"输入年:";cin>>number;d.SetYear(number);cout<<"输入月:";cin>>number;d.SetMonth(number);cout<<"输入日:";cin>>number;d.SetDay(number);if(d.GetYear()<=0||d.GetMonth()>12||d.GetMonth()<1||d.GetDay()<1||d.GetDay()>d.GetMonthDay(2)){cout<<"输入有误,返回。"<<endl;break;}cout<<"星期"<<Date::Week(d)<<endl;break;case'4':cout<<"输入年:";cin>>number;d.SetYear(number);cout<<"输入月:";cin>>number;d.SetMonth(number);if(d.GetYear()<=0||d.GetMonth()>12||d.GetMonth()<1){cout<<"输入有误,返回。"<<endl;break;}d.OutputYearMonthDate(d.GetYear(),d.GetMonth());break;case'5':cout<<"输入天数:";cin>>number;if(number<=0){cout<<"输入有误,返回。"<<endl;break;}cout<<"年:"<<d.NumToDay(number).GetYear()<<endl;cout<<"月:"<<d.NumToDay(number).GetMonth()<<endl;cout<<"日:"<<d.NumToDay(number).GetDay()<<endl;break;case'6':exit(1);default:cout<<"输入有误,请重新输入!";}}return 0;}


原创粉丝点击