//因爲編輯器 用輸入箭頭,方向鍵控制不美觀 故換了值
//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();} } }