C++实现日期计算器
运行效果
代码如下
1. 头文件
#ifndef _DATE_H_
#define _DATE_H_
#include<iostream>
#define HUNDRED 36524
#define HUNDRED_4 (36524*4+1)
#define RUNNIAN (365*4+1)
using namespace std;
class Date{
static int n;
int y,m,d;
int leap(int);
int dton(Date&);
Date ntod(int);
public:
static int day_tab[2][12];
int &getY(){return y;}
int &getM(){return m;}
int &getD(){return d;}
int getLeap(int i){return leap(i);}
Date(int y,int m,int d);
Date(int d);
~Date(){
n--;
if(0==n)
cout<<"\n本程序由ZhongNan GW 制作"<<endl;
}
Date operator+(int);
Date operator-(int);
int operator-(Date);
friend ostream &operator<<(ostream &sout,Date idx);
};
int Date::n=0;
int Date::day_tab[2][12]={{31,29,31,30,31,30,31,31,30,31,30,31},
{31,28,31,30,31,30,31,31,30,31,30,31}};
//构造函数
Date::Date(int y=0,int m=0,int d=0){
this->y=y;
this->m=m;
this->d=d;
n++;
}
Date::Date(int d){
y=0;m=0;this->d=d;
n++;
}
//判断闰年
int Date::leap(int year){
if(0==year%400||0==year%4&&0!=year%100)return 0;
else return 1;
}
//计算天数
int Date::dton(Date&x){
int tmp=0;
for(int i=1;i<x.y;i++){
if(0==leap(i))tmp+=366;
else tmp+=365;
}
for(int i=0;i<x.m-1;i++){
tmp+=day_tab[leap(x.y)][i];
}
tmp+=x.d;
return tmp;
}
//计算日期
Date Date::ntod(int n){
Date temp;
int hund_4=n/HUNDRED_4;//有hund_4个400年
int hund=n%HUNDRED_4/HUNDRED;//有hund个100年
int in_hundred=n%HUNDRED_4%HUNDRED/RUNNIAN;//有in_hundred个4年
int in_4year=n%HUNDRED_4%HUNDRED%RUNNIAN/365;//有in_4year个1年
temp.getD()=n%HUNDRED_4%HUNDRED%RUNNIAN%365;
if(0==temp.getD())temp.getD()=365;
else in_4year+=1;
temp.getM()=1;
temp.getY()=in_4year+in_hundred*4+hund*100+hund_4*400;
int i=0;
while(temp.getD()>Date::day_tab[temp.getLeap(temp.getY())][i]){
temp.getD()-=Date::day_tab[temp.getLeap(temp.getY())][i];
temp.getM()+=1;
i++;
}
return temp;
}
//运算符重载:加上天数
Date Date::operator+(int d){return ntod(dton(*this)+d);}
//减去天数
Date Date::operator-(int d){return ntod(dton(*this)-d);}
//日期相减
int Date::operator-(Date d){
return dton(*this)-dton(d);
}
//流重载
ostream &operator<<(ostream &sout,Date idx){
sout<<idx.y<<"年"<<idx.m<<"月"<<idx.d<<"日"<<endl;
return sout;
}
#endif
2. main函数源文件
#include<iostream>
#include"Date.h"
using namespace std;
int main(){
int y,m,d;
char election;
cout<<"日期计算器"<<endl;
cout<<"请选择功能:\n"<<endl;
cout<<"********************************"<<endl;
cout<<"*功能1:计算日期之间相距天数 *"<<endl;
cout<<"* *"<<endl;
cout<<"*功能2:计算日期加上天数后的日期*"<<endl;
cout<<"* *"<<endl;
cout<<"*功能3:计算日期减去天数后的日期*"<<endl;
cout<<"********************************"<<endl;
do{
cout<<"请输入:";
cin>>election;
switch(election){
case '1':{
cout<<"请输入起始年 月 日:";
cin>>y>>m>>d ;
Date today(y,m,d);
cout<<"请输入终点年 月 日:";
cin>>y>>m>>d ;
Date yesterday(y,m,d);
cout<<"相距"<<today-yesterday<<"天"<<endl;
break;
}
case '2':{
cout<<"请输入年 月 日:";
cin>>y>>m>>d ;
Date today(y,m,d);
cout<<"输入要加上的天数:";
cin>>d;
cout<<"得到日期为:"<<today+d;
break;
}
case '3':{
cout<<"请输入年 月 日:";
cin>>y>>m>>d ;
Date today(y,m,d);
cout<<"输入要减去的天数:";
cin>>d;
cout<<"得到日期为:"<<today-d;
break;
}
}
cout<<"是否继续查询?(y/n)"<<endl;
getchar();
cin>>election;
}while('y'==election);
}
第一次尝试,就当练个手了。。。
这个可以用来计算从公元1年1月1日开始,任意日期间的差值,以及日期加减天数的计算