日期计算

今天无聊,突然想算今天是自己出生的第几天,然后就写了个程序;

顺便分享,很简陋啊~~呵呵。。

//cpp文件,daycal.cpp

#include "daycal.h"
bool DayCal::isLeapYear(int ayr){
if (mod(ayr,400)==0||(mod(ayr,4)==0 && mod(ayr,100)!=0)){return true;}
return false;
};
bool DayCal::isDataLegal(){
bool islegal=true;
int tmp[12];
for (int i=0;i<12;i++){tmp[i]=dayarr[i];}
tmp[1]+=int(islpyr);
if (yr>2500 || yr<0 || mth<1 || mth>12 || day<1 ||day>tmp[mth-1]||wkday<0||wkday>6)
{islegal=false;}
return islegal;
};

int DayCal::DayNum ()
{
int tmp[12];
for (int i=0;i<12;i++){tmp[i]=dayarr[i];}
tmp[1]+=int(islpyr);
int dn=0;
for (int i=0;i<mth-1;i++){
dn=dn+tmp[i];
}
dn=dn+day;
return dn;
};
int DayCal::dateDistn(DayCal aday)
{
DayCal bfday=*this;
DayCal aftday=aday;
if (aftday.isDayEql (bfday)) return 0;
int dn=0;
if (aftday.isDayBf(bfday)) {bfday=aday;aftday=*this;}
for (int i=0;i<aftday.yr-bfday.yr;i++){
dn+=(365+int(isLeapYear(bfday.yr+i)));}
dn+=(aftday.DayNum()-bfday.DayNum());
return dn;
};
DayCal DayCal::dayAftToday(int dnum){
int ayr=yr,amth=0,aday=0;
int today=DayNum();
int tmp[12];
for (int i=0;i<12;i++){tmp[i]=dayarr[i];}
if (dnum>(365+int(isLeapYear(ayr)))){
for (ayr=yr;dnum>(365+int(isLeapYear(ayr)));ayr++){
dnum=dnum-(365+int(isLeapYear(ayr)));}
}
if (dnum>((365+int(isLeapYear(ayr))-today))){
dnum=dnum-(365+int(isLeapYear(ayr)));
ayr++;}
tmp[1]=tmp[1]+int(isLeapYear(ayr));
dnum=dnum+today;
for (amth=1;dnum>tmp[amth-1];amth++){
dnum=dnum-tmp[amth-1];}
aday=dnum;
DayCal aftday(ayr,amth,aday);
return aftday;
};
DayCal DayCal::dayBfToday(int dn){
int ayr,amth,aday;
int today=DayNum();
int tmp[12];
for (int i=0;i<12;i++){tmp[i]=dayarr[i];}
for (ayr=yr;dn>(365+int(isLeapYear(ayr)));ayr--){
dn-=(365+int(isLeapYear(ayr)));}
if (dn>=today){
ayr--;
dn-=today;

today=(365+int(isLeapYear(ayr)));
}
dn=today-dn;
tmp[1]+=int(isLeapYear(ayr));

for (amth=1;dn>tmp[amth-1];amth++){
dn-=tmp[amth-1];}
aday=dn;
DayCal aftday(ayr,amth,aday);
return aftday;
};
int DayCal::dayarr[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int DayCal::wkDayCal(){
DayCal stdday;
int wkd;
int dnum=dateDistn(stdday);
if (isDayEql(stdday)) {wkd=0;}
else if (isDayBf(stdday)){//待测日期在标准日期之前
wkd=int(7-mod(dnum,7));
wkd=int(mod(wkd,7));
}
else if (!isDayBf(stdday)){//待测日期在标准日期之后
wkd=int(mod(dnum,7));
}
return wkd;
};

void DayCal::prtday(){
cout<<yr<<"-";
if (mth<10) cout<<"0";
cout<<mth<<"-";
if (day<10) cout<<"0";
cout<<day;
return;
};

//头文件 daycal.h

#ifndef DAYCAL_H
#define DAYCAL_H
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

class DayCal
{
public:
DayCal(){yr=2000;mth=1;day=2;wkday=0;islpyr=isLeapYear();};
DayCal(int ayr,int amth,int aday){
yr=ayr;mth=amth;day=aday;islpyr=isLeapYear();
wkday=wkDayCal();
};
bool isDataLegal();
bool isLeapYear(){return isLeapYear(yr);};
int dateDistn(DayCal);
bool isDayBf(DayCal aday){//if aday is a day before *this ,ruturn false,else return true
if (yr>aday.yr ) return false;
else if (yr==aday.yr && mth>aday.mth ) return false;
else if (yr==aday.yr && mth==aday.mth && day>aday.day )return false;
return true;
};
bool isDayEql(DayCal aday){
if (yr==aday.yr && mth==aday.mth && day==aday.day) return true;
return false;
};
int wkDayCal();
int DayNum();
DayCal dayAftToday(int dn);
DayCal dayBfToday(int dn);
int getyear(){return yr;};
int getmonth(){return mth;};
int getday(){return day;};
bool getislpyr(){return islpyr;};
void prtday();
int mod(int a,int b){ return int(a-b*int(a/b));};
private:
int yr,mth,day;
bool islpyr;
int wkday;
static int dayarr[12];
bool isLeapYear(int ayr);
};

#endif

//主函数文件 main.cpp

#include<iostream>
#include "daycal.h"
#include <cctype>
#include <string>
using namespace std;
//=============================================
//用到的子函数
void prttitle();
DayCal getaday();
void choice1();
void choice2();
void choice3();
void choice4();
void choice5();
void choice6();
//=============================================
void main()
{
/*DayCal day1(2000,1,1);
cout<<day1.mod (2008,4)<<endl<<day1.mod (2008,100);
return;*/
int chos=0;
//if (!day1.isDataLegal()) {cout<<"error:data illegal"<<endl;return ;}

char ifctn='y';
while ('y'==tolower(ifctn)){
prttitle();
cout<<"请选择一个项目(输入数据后回车确认):";
cin>>chos;
if(chos<0 ||chos>100){chos=0;}
switch (chos){
case 1: choice1();break;
case 2: choice2();break;
case 3: choice3();break;
case 4: choice4();break;
case 5: choice5();break;
case 6: choice6();break;
case 0: ifctn='n';break;
default:
cout<<"error:data illegal"<<endl;

}
if ('y'==tolower(ifctn)){
cout<<"是否继续(按 y 继续,其他键退出):";
cin>>ifctn;}
if (!isalpha(ifctn)) ifctn='n';
}
return ;
}
void prttitle(){
cout<<"|===========================================================|"<<endl;
cout<<"| 日期计算器(v1.0) |"<<endl;
cout<<"|-----------------------------------------------------------|"<<endl;
cout<<"| 1.计算两个日期间的距离天数 |"<<endl;
cout<<"| 2.计算某个日期当年的第几天 |"<<endl;
cout<<"| 3.计算某个日期是星期几 |"<<endl;
cout<<"| 4.计算某个日期之后的第n天的日期 |"<<endl;
cout<<"| 5.计算某个日期之前的第n天的日期 |"<<endl;
cout<<"| 6.判断该年是不是闰年 |"<<endl;
cout<<"| |"<<endl;
cout<<"| 0.退出 |"<<endl;
cout<<"|-----------------------------------------------------------|"<<endl;
cout<<"| 小锐制作(2011-12-28) |"<<endl;
cout<<"|===========================================================|"<<endl;
return;
};
DayCal getaday(){
int yr,mth,day;
cout<<"请输入年份:";
cin>>yr;
cout<<"请输入月份:";
cin>>mth;
cout<<"请输入天数:";
cin>>day;
DayCal aday(yr,mth,day);
return aday;
}
void choice1(){
DayCal day1;
DayCal day2;
bool gooddata=false;
while (!gooddata){
cout<<"请输入第一个日期。。"<<endl;day1=getaday();
cout<<"请输入第二个日期。。"<<endl;day2=getaday();
if (day1.isDataLegal() && day2.isDataLegal()) gooddata=true;
else cout<<"数据不正确,请重新输入!"<<endl;
}
day1.prtday ();
cout<<" 与 ";
day2.prtday ();
cout<<" 之间的距离天数为:"<<day1.dateDistn (day2)<<" 天。"<<endl;
};
void choice2(){
DayCal day1;
bool gooddata=false;
while (!gooddata){
cout<<"请输入日期。。"<<endl;day1=getaday();
if (day1.isDataLegal()) gooddata=true;
else cout<<"数据不正确,请重新输入!"<<endl;
}
day1.prtday ();
cout<<" 为 "<<day1.getyear ()<<" 年的第 "<<day1.DayNum ()<<" 天。"<<endl;
};
void choice3(){
DayCal day1;
bool gooddata=false;
while (!gooddata){
cout<<"请输入日期。。"<<endl;day1=getaday();
if (day1.isDataLegal()) gooddata=true;
else cout<<"数据不正确,请重新输入!"<<endl;
}
day1.prtday ();
cout<<"是:星期";
switch (day1.wkDayCal()){
case 0 :cout<<"日。";break;
case 1 :cout<<"一。";break;
case 2 :cout<<"二。";break;
case 3 :cout<<"三。";break;
case 4 :cout<<"四。";break;
case 5 :cout<<"五。";break;
case 6 :cout<<"六。";break;
};
cout<<endl;
};
void choice4(){
DayCal day1;
int dn;
bool gooddata=false;
while (!gooddata){
cout<<"请输入日期。"<<endl;day1=getaday();
cout<<"请输入距离的天数:";
cin>>dn;
if (day1.isDataLegal()) gooddata=true;
else cout<<"数据不正确,请重新输入!"<<endl;
}
DayCal day2;
day2=day1.dayAftToday (dn);
day1.prtday ();
cout<<" 之后的第 "<<dn<<" 天为 ";
day2.prtday();
cout<<endl;
};
void choice5(){
DayCal day1;
int dn;
bool gooddata=false;
while (!gooddata){
cout<<"请输入日期。"<<endl;day1=getaday();
cout<<"请输入距离的天数:";
cin>>dn;
if (day1.isDataLegal()) gooddata=true;
else cout<<"数据不正确,请重新输入!"<<endl;
}
DayCal day2;
day2=day1.dayBfToday (dn);
day1.prtday ();
cout<<" 之前的第 "<<dn<<" 天为 ";
day2.prtday();
cout<<endl;
};
void choice6(){
int yr;
bool gooddata=false;
while (!gooddata){
cout<<"请输入年份:";
cin>>yr;
if (yr>0 && yr<99999) gooddata=true;
else cout<<"数据不正确,请重新输入!"<<endl;
}
DayCal day1(yr,1,1);
cout<<yr<<" 年";
if (!day1.getislpyr())cout<<"不";
cout<<"是闰年"<<endl;
return;

};

 输出的格式好像乱了额。。。

posted @ 2011-12-28 17:00  木lin木  阅读(433)  评论(0编辑  收藏  举报