《万年历》

#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);//算出了year年有多少天

		if(Isleapyear(year))
			monthDay[1]=29;
		else
			monthDay[1]=28;

		for(int j=1;j<month;j++)
			sum+=monthDay[j-1];//这个地方是不是可以用这个函数GetMonthDay();这样的话就省去了上面的if else 代码了???
								//不过程序的效率好像就会降低??

		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;
		else
			monthDay[1]=28;

		for(;n>=monthDay[d.month];d.month++)
			n-=monthDay[d.month];//此处未注意n等于0的情况。if (n) d.day = n;
		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;//由此刻(此年此月)到1年1月1日的 天数%7 判断1日的输出格式(即前面空多少天)
			for(j=1;j<monthDay[i-1];j++)
			{
				if((j+(d.DateToNum()%7))%7==0)//由j 即本月中的日期加上此月到1年1月1日的 天数%7 来控制换行(每七天换行)
					cout<<endl;
				cout<<setw(5)<<j+1;
			}
			cout<<endl;
			cout<<"-----------------------------------"<<endl;
		}//end while
	}

	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;
	}
	//这个函数与上一个函数代码重复性很高,是否可以用函数重载的思想完成这两个函数的合并。
	//利用上一个函数将月默认为-1 
	//if (月 == -1)的进行一些操作。
	//else 进行输出此月日历。

	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<<"	            设公元1年1月1日为第一天    "<<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;
			else
				cout<<"非闰年."<<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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值