您的位置:首页 > 其它

date 日期类 框架存在

2016-03-09 23:53 218 查看
//因为编辑器 用输入箭头,方向键控制不美观 故换了值
//37 38 39 40 左上右下
#include"sts.h"
#include <time.h>
class date
{
public:
date(int year,int month,int day):year(year),month(month),day(day){}
int datetoday()// 把日期转换为总多少天
{
int day=0;
int i=0,j=0,k=0;
int count=0;
int star[13]={0,1,32,60,91,121,152,182,213,244,274,304,334};//0-6
for(i=1;i<this->year;i++)
{  if(Year(i)) count+=366;else count+=365;}
if( Year(this->year) ) {  for(j=3;j<=12;j++) star[j]++; }
count=count+star[this->month]+this->day-1;
//cout<<count<<endl;
return count;
}

date & daystodate(int n)
{
int day=0;
int i=0,j=0,k=0;
int chazhi=0;
int star[13]={0,1,32,60,91,121,152,182,213,244,274,304,334};//0-6
for(i=1;day<(n);i++)
{    if(Year(i))  day+=366; else    day+=365; }
if(Year(i))
{  day-=366;  for(j=3;j<12;j++) star[j]++; }
else day-=365;
chazhi=n-day;
for(j=1;j<13;j++) {   if( star[j] < chazhi )  k++;  }
this->year=i-1;
this->month=k;
this->day=chazhi-star[k];
return *this;
}

void display(){cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;}
int Year(int year) { if(year%4==0&&year%100!=0||year%400==0)return 1; else return 0; }
int operator-(date &d)//重载 日期相减
{int x=0; x= this->datetoday()-d.datetoday(); if(x>0) return x;  else return -x; }
void  outmonth()
{
int day=0;
int i=0;
int count=0;
int cc[40]={0};
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
cout<<" 一 二 三 四 五 六 日"<<endl;
cout<<"______________________"<<endl;
count=datetoday()%7;
cc[count]=1;
for(i=count;i<40;i++)
{cc[i]=cc[i-1]+1;}
for(i=0;i<40;i++)
{
if(cc[i]==0)cout<<"   ";
if(cc[i]>0&&cc[i]<=m[this->month])cout<<setw(3)<<cc[i];if(i%7==0)cout<<endl;
}
cout<<endl;
};
void outyear()
{
int i=1;
for(i=1;i<13;i++) {this->month=i; cout<<this->year<<"年"<<this->month<<"月"<<endl;outmonth();}
}
date& operator+(int n)
{
int day=0;
int i=0,j=0,k=0;
int count=0;
int star[13]={0,1,32,60,91,121,152,182,213,244,274,304,334};//0-6
int chazhi=0;      //用于月天数计算
count=datetoday();
if(Year(this->year))
{
for(j=3;j<12;j++)star[j]--;
}
cout<<this->year<<"年" <<this->month<<"月 "<<this->day<<"日 "<<n<<" 天后的时间如下:" <<endl;
for(i=1;day<(count+n);i++)
{
if(Year(i))  day+=366;
else    day+=365;
}
if(Year(i))// 如本例  原会是365+365>400天 所以要根据最后加的判断一下再回到原来的天数
{
day-=366;
for(j=3;j<12;j++) star[j]++;
}
else day-=365;
chazhi=count+n-day;//计算差值以确定月份
for(j=1;j<13;j++)
{   if( star[j] < chazhi )  k++;  }

cout<<i-1<<"年"<<k<<"月"<<(chazhi-star[k])<<"日" <<endl;
this->year=i-1;
this->month=k;
this->day=chazhi-star[k];
return *this;

}
void monthadd() {this->month++;}
void monthsub() {this->month--;}
void yearadd() {this->year++;}
void yearsub() {this->year--;}
private:
int year;
int month;
int day;
};
void DIS()
{
cout<<"****欢迎进入时间程序* "<<endl;
cout<<"*** sts 当前时间*** "<<endl;
cout<<" ** sts1 更改  **"<<endl;
cout<<"  *sts2计算天数*"<<endl;
cout<<"  *date+days*"<<endl;
cout<<"0 is exit there"<<endl;
cout<<"********************"<<endl;
}

void time(date &t)
{
int XX=0;
int n=0;
cout<<"1:打印当前月 2: 打印当前年"<<endl;
cin>>XX;
if(XX==1) {t.outmonth();}
if(XX==2) {t.outyear();}
cout<<"3:按左右键查看前后月,4:按上下键查看上下年同日信息"<<endl;
cin>>XX;
if(XX==1) { t.monthsub();t.outmonth();}
if(XX==2) { t.monthadd();t.outmonth();}
if(XX==3) { t.yearadd(); t.outmonth();}
if(XX==4) { t.yearsub(); t.outmonth();}
cout<<"5:n天后的时间 "<<endl;
cin>>n;
t+n;
}
void sts()
{
int year;
int month;
int day;
cout<<"已获取系统当前时间:"<<endl;
struct tm *t;
time_t tt;
time(&tt);
t=localtime(&tt);
year=t->tm_year+1900;
month=t->tm_mon+1;
day=t->tm_mday;
cout<<t->tm_year+1900<<"年"<<t->tm_mon+1<<"月"<<t->tm_mday<<"日"<< t->tm_hour+16<<":"<< t->tm_min<<":" <<t->tm_sec<<endl;
date today(year,month,day);
today.display();
time(today);
}
void sts1()
{
int year=0;
int month=0;
int day=0;
cout<<"输入新的时间"<<endl;
cin>>year>>month>>day;
date d(year,month,day);
d.display();
time(d);
}

void sts2()
{
int year1=0,year2=0;
int month1=0,month2=0;
int day1=0,day2=0;
int count=0;
cout<<"时间1"<<endl;
cin>>year1>>month1>>day1;
date d1(year1,month1,day1);

cout<<"时间2"<<endl;
cin>>year2>>month2>>day2;
date d2(year2,month2,day2);

count=d1-d2;
d1.display();
d2.display();
cout<<"之间是"<<count<<"天"<<endl;

}
int main()
{

int x=1;
while(x)
{
DIS();
cin>>x;
if(x==1){sts();}
if(x==2){sts1();}
if(x==3){sts2();}
// if(x==4){sts3();}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: