5_5万年历(读程序)

来源:互联网 发布:淘宝拍卖的车能买吗 编辑:程序博客网 时间:2024/06/06 14:10
/** 程序的版权和版本声明部分* Copyright (c)2012, 烟台大学计算机学院学生* All rightsreserved.* 文件名称: object.cpp* 作者:纪子龙* 完成日期: 2013年3  月 21 日* 版本号: 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<<setw(5)<<"SUN"<<setw(5)<<"MON"<<setw(5)<<"TUE"<<setw(5)<<"WED"<<setw(5)<<"THU"<<setw(5)<<"FRI"<<setw(5)<<"SAT"<<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<<setw(5)<<"SUN"<<setw(5)<<"MON"<<setw(5)<<"TUE"<<setw(5)<<"WED"<<setw(5)<<"THU"<<setw(5)<<"FRI"<<setw(5)<<"SAT"<<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<<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<<"输入错误";system("pause");system("cls");continue;}switch(choose){case '1':cout<<"输入年:";cin>>number;d.SetYear(number);if(d.GetYear()<=0){cout<<"输入年份有错,返回。"<<endl;system("pause");system("cls");break;}d.OutputYearDate(d.GetYear());system("pause");system("cls");break;case '2':cout<<"输入年:";cin>>number;d.SetYear(number);if(d.GetYear()<=0){cout<<"输入年份有错,返回。"<<endl;system("pause");system("cls");break;}if(d.Isleapyear(d.GetYear()))cout<<"闰年。"<<endl;elsecout<<"非闰年."<<endl;system("pause");system("cls");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;system("pause");system("cls");break;}cout<<"星期"<<Date::Week(d)<<endl;system("pause");system("cls");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;system("pause");system("cls");break;}d.OutputYearMonthDate(d.GetYear(),d.GetMonth());system("pause");system("cls");break;case '5':cout<<"输入天数:";cin>>number;if(number<=0){cout<<"输入有误,返回。"<<endl;system("pause");system("cls");break;}cout<<"年:"<<d.NumToDay(number).GetYear()<<endl;cout<<"月:"<<d.NumToDay(number).GetMonth()<<endl;cout<<"日:"<<d.NumToDay(number).GetDay()<<endl;system("pause");system("cls");break;case '6':exit(1);default:cout<<"输入错误,请重新输入";system("pause");system("cls");}}return 0;}运行结果:


原创粉丝点击