Time和Date的结合【exercise17.9】
目的:每秒输出一个当前日期
重点看Time的member function:tick
1 //Time.h
#ifndef TIME_H 2 #define TIME_H 3 #include"DAT_Date.h" 4 5 class Time{ 6 public: 7 Time(int=23,int=59,int=55); 8 9 //get functions 10 int getHour(); 11 int getMinute(); 12 int getSecond(); 13 14 //set functions 15 void setTime(int,int,int); 16 void setHour(int); 17 void setMinute(int); 18 void setSecond(int); 19 20 void printUniversal(); 21 void printStandard(); 22 23 void tick(double,Date); 24 void printTimeAndDate(Date); 25 private: 26 void increment();//utility function 27 int hour; 28 int minute; 29 int second; 30 }; 31 #endif
1 //Time.cpp 2 #include"DAT_Time.h" 3 #include"DAT_Date.h" 4 #include<iostream> 5 #include<stdexcept> 6 #include<iomanip> 7 #include<ctime> 8 using namespace std; 9 10 Time::Time(int hour,int minute,int second){ 11 setTime(hour,minute,second); 12 } 13 14 int Time::getHour(){ 15 return hour; 16 } 17 18 int Time::getMinute(){ 19 return minute; 20 } 21 22 int Time::getSecond(){ 23 return second; 24 } 25 26 void Time::setHour(int h){ 27 if(h>=0&&h<=23){ 28 hour=h; 29 }else{ 30 throw invalid_argument("invalid input of hour\n"); 31 } 32 } 33 34 void Time::setMinute(int m){ 35 if(m>=0&&m<=59){ 36 minute=m; 37 }else{ 38 throw invalid_argument("invalid input of minute\n"); 39 } 40 } 41 42 void Time::setSecond(int s){ 43 if(s>=0&&s<=59){ 44 second=s; 45 }else{ 46 throw invalid_argument("invalid input of second\n"); 47 } 48 } 49 void Time::setTime(int h,int m,int s){ 50 setHour(h); 51 setMinute(m); 52 setSecond(s); 53 } //删了一段所以行号跳了ww 72 void Time::printStandard(){ 73 cout<<setfill('0')<<setw(2) 74 <<((getHour()==0||getHour()==12)?12:getHour()%12)<<":" 75 <<setw(2)<<getMinute()<<":" 76 <<setw(2)<<getSecond()<<((getHour()>12)?" PM":" AM")<<endl; 77 } 78 79 void Time::printUniversal(){ 80 cout<<setfill('0') 81 <<setw(2)<<getHour()<<":" 82 <<setw(2)<<getMinute()<<":" 83 <<setw(2)<<getSecond()<<endl; 84 } 85 86 void Time::tick(double lastingSecond,Date date1){ 87 double last=0,now; 88 printTimeAndDate(date1); 89 while(1){ 90 now=(double)clock()/CLOCKS_PER_SEC; 91 if(now-last>=1){ 92 increment(); 93 if(hour==0&&minute==0&&second==0){ 94 date1.increment(); 95 } 96 printTimeAndDate(date1); 97 --lastingSecond; 98 if(lastingSecond<=0){ 99 break; 100 } 101 last=now; 102 } 103 } 104 } 105 106 void Time::printTimeAndDate(Date date1){ 107 date1.print(); 108 cout<<" "<<flush; 109 printStandard(); 110 } 111 112 void Time::increment(){ 113 if(second==59){ 114 if(minute==59){ 115 if(hour==23){ 116 setHour(0); 117 setMinute(0); 118 setSecond(0); 119 }else{ 120 setHour(hour+1); 121 setMinute(0); 122 setSecond(0); 123 } 124 }else{ 125 setSecond(0); 126 setMinute(minute+1); 127 } 128 }else{ 129 setSecond(second+1); 130 } 131 }
1 //Date.h 2 #ifndef DATE_H 3 #define DATE_H 4 class Date{ 5 public: 6 Date(int=2021,int=3,int=2); 7 void setDate(int,int,int); 8 void print(); 9 void increment(); 10 private: 11 int year; 12 int month; 13 int day; 14 }; 15 #endif
1 //Date.cpp 2 #include"Date.h" 3 #include<iostream> 4 #include<stdexcept> 5 using namespace std; 6 7 Date::Date(int y,int m,int d){ 8 setDate(y,m,d); 9 } 10 11 void Date::setDate(int y,int m,int d){ 12 if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){ 13 if(d>=1&&d<=31){ 14 day=d; 15 }else{ 16 throw invalid_argument("invalid date"); 17 } 18 month=m; 19 }else if(m>0&&m<13){ 20 if(m!=2){ 21 if(d>=1&&d<=30){ 22 day=d; 23 }else{ 24 throw invalid_argument("invalid date"); 25 } 26 }else{ 27 if((year%4==0&&year%100!=0)||y%400==0){ 28 if(d>=1&&d<=29){ 29 day=d; 30 }else{ 31 throw invalid_argument("invalid date"); 32 } 33 }else{ 34 if(d>=1&&d<=28){ 35 day=d; 36 }else{ 37 throw invalid_argument("invalid date"); 38 } 39 } 40 } 41 month=m; 42 }else{ 43 throw invalid_argument("invalid month"); 44 } 45 if(y>0) year=y; 46 } 47 48 void Date::print(){ 49 cout<<month<<"/"<<day<<"/"<<year; 50 } 51 52 void Date::increment(){ 53 if(day==31){ 54 if(month==12){ 55 ++year; 56 month=1; 57 day=1; 58 }else{ 59 ++month; 60 day=1; 61 } 62 }else{ 63 if(day==30){ 64 if(month==4||month==6||month==9||month==11){ 65 ++month; 66 day=1; 67 }else{ 68 ++day; 69 } 70 }else{ 71 if(day==28||day==29){ 72 ++month; 73 day=1; 74 }else{ 75 ++day; 76 } 77 } 78 } 79 }
//main.cpp #include"Time.h" #include"Date.h" #include<iostream> using namespace std; int main(){ Time timer; Date date1; double lastingSecond=10;//10秒 timer.tick(lastingSecond,date1); }
Look, if you had one shot , one opportunity , to seize everything you ever wanted , in one moment.
Would you captrue it , or just let it slip ?

浙公网安备 33010602011771号