为你定制了一个框架,使用了一个Date类,类中有三个构造函数:无参数(今日),带年月日,以及天数;有重载了操作符的,直接可以把日期对象做运算,重载的操作符有:+、-、+=、-=、=、==、++、--。集成了月历输出。提供设置日期、读取日期、显示日期等方法;集成了日期输入检验规则。请看头文件:
#pragma once
#ifndef _DATA_H_
#define _DATA_H_
#include
#define START_YEAR 1940
#define END_YEAR 2040 //不包含该年
class CDate
{
public:
CDate(unsigned int iYear,unsigned int iMonth,unsigned int iDay);//构造函数
CDate(unsigned int nDays);//构造函数
CDate(void);//构造函数(今天)
virtual ~CDate(void);//析构函数
const CDate& operator+(int days);//操作符+
const CDate& operator-(int days);//操作符-
const int operator-(CDate Date);//操作符- (两个日期相减)
const CDate& operator++();//自加
const CDate& operator--();//自减
const CDate& operator+=(int days);//加赋值
const CDate& operator-=(int days);//减赋值
const CDate& operator=(CDate Date);//赋值
const bool operator==(CDate Date);//是否相等
void GetDate(int &iYear,int &iMonth,int &iDay);//取日期
bool SetDate(int iYear,int iMonth,int iDay);//设置日期
int GetBaseDays(void);//返回当前对象的基准天数
bool SetBaseDay(int nDays);//设置基准天数
unsigned int GetDayOfWeek();//获取星期
bool IsLeapYear(int nYear,bool & IsLeap);//判断闰年
unsigned int GetDaysOfMonth(int uYear,unsigned int uMonth);//获取该月总天数
bool ShowCalendar(unsigned int uYear,unsigned int uMonth);//显示月历
void Display(void);//显示日期
private:
int int2(double dNum);//取整(天数)
bool DateCheck(int uYear,unsigned int uMonth,unsigned int uDay);//日期合法性检察
int GetBaseDays(int uYear,unsigned int uMonth,unsigned int uDay);//获取距离基准日期的天数,以START_YEAR年1月1日0时为0,负代表早于该时刻,正代表晚于该时刻
bool GetDateFromDays(void);//从距离基准天数的积日换算成日期
private:
unsigned int m_iYear;//年
unsigned int m_iMonth;//月
unsigned int m_iDay;//日
unsigned int m_iDayOfWeek;//星期
int m_nDays;//积日(距离基准日期的天数)
char day[11];
};
#endif //_DATA_H_
并且帮你写好了程序:
#include "Date.h"
#include
using namespace std;
int main(int argc,char *argv[])
{
CDate Date;
int iYear=0,iMonth=0,iDay=0,iTotalDays=0;
int nChoise=0;
bool ret=false;
cout<<"**************************************\n";
cout<<"* *\n";
cout<<"* C++ 年历系统 *\n";
cout<<"* 年份范围:"<cout<<"* *\n";
cout<<"**************************************\n";
while (1)
{
cout<<"\n\n◆1. 显示月历\n◆2. 距离今日天数\n\n\n";
cout<<"请选择:";
cin>>nChoise;
switch (nChoise)
{
case 1:
cout<<"\n显示月历\n\n";
do
{
cout<<"请输入年和月:Year Month>";
cin>>iYear>>iMonth;
if (cin.fail())
{
return 1;
}
ret=Date.ShowCalendar(iYear,iMonth);
if (ret==false)
{
cout<<"年、月、日输入不正确,请重新输入!\n";
}
} while (ret==false);
break;
case 2:
cout<<"\n距离今日天数\n\n";
do
{
cout<<"请输入年、月和日:Year Month Day>";
cin>>iYear>>iMonth>>iDay;
if (cin.fail())
{
return 1;
}
ret=Date.SetDate(iYear,iMonth,iDay);
if (ret==false)
{
cout<<"年、月输入不正确,请重新输入!\n";
}
} while (ret==false);
CDate Today;
iTotalDays=Date-Today;
cout<Today.Display();
cout<<")的天数为"<break;
}
}
return 0;
}
输出整年你就直接用For循环吧。其余的功能,星期,是可以直接调用类的方法来输出的,关于节气,直接通过月和日来判断。
此外还有一个核对程序,可以检验正确性。
这类程序很多,百度一下吧。