C++时钟-系统时间

  1 # include <iostream>
  2 # include <windows.h>
  3 # include <time.h>
  4 # include <iomanip>
  5 # define BUF 255
  6 
  7 using namespace std;
  8 
  9 class Time
 10 {
 11 private:
 12     int hour,minute,second;
 13 public:
 14     Time():hour(0),minute(0),second(0){}
 15     Time(int h,int m,int s):hour(h),minute(m),second(s){}
 16     
 17     //成员函数 前++重载 
 18     /*Time operator++()
 19     {
 20         this->second+=1;
 21         if(this->second==60)
 22         {
 23             this->second=0;
 24             this->minute++;
 25         }
 26         if(this->minute==60)
 27         {
 28             this->minute=0;
 29             this->hour++;
 30         }
 31         if(this->hour==24)
 32         {
 33             this->hour=0;
 34         }
 35     }*/
 36     
 37     //友元函数 前++重载
 38     friend operator++(Time &time1)
 39     {
 40         time1.second+=1;
 41         if(time1.second==60)
 42         {
 43             time1.second=0;
 44             time1.minute++;
 45         }
 46         if(time1.minute==60)
 47         {
 48             time1.minute=0;
 49             time1.hour++;
 50         }
 51         if(time1.hour==24)
 52         {
 53             time1.hour=0;
 54         }
 55     } 
 56     
 57     //成员函数 后++重载
 58     /*Time operator++(int)
 59     {
 60         this->second+=1;
 61         if(this->second==60)
 62         {
 63             this->second=0;
 64             this->minute++;
 65         }
 66         if(this->minute==60)
 67         {
 68             this->minute=0;
 69             this->hour++;
 70         }
 71         if(this->hour==24)
 72         {
 73             this->hour=0;
 74         }
 75     }*/
 76     
 77     //友元函数 后++重载
 78     friend operator++(Time &time1,int)
 79     {
 80         time1.second+=1;
 81         if(time1.second==60)
 82         {
 83             time1.second=0;
 84             time1.minute++;
 85         }
 86         if(time1.minute==60)
 87         {
 88             time1.minute=0;
 89             time1.hour++;
 90         }
 91         if(time1.hour==24)
 92         {
 93             time1.hour=0;
 94         }
 95     } 
 96     //重载流插入 <<运算符 
 97     /*friend ostream &operator<<(ostream &out, const Time &time1) 
 98     {
 99         out <<setw(2)<<time1.hour<< " : " <<setw(2)<<time1.minute<< " : " <<setw(2)<<time1.second;
100         return out;
101     }*/
102     void show()
103     {
104        time_t t = time( 0 );
105        char tmpBuf[BUF];
106        strftime(tmpBuf, BUF, "%Y-%m-%d %H:%M:%S %A", localtime(&t));
107        printf("time is [%s]",tmpBuf);
108     }    
109 };
110   
111 int main()
112 {
113     int hour,minute,second; 
114     //获取系统时间
115     time_t time2;
116     struct tm *p;
117     time (&time2);
118     p=gmtime(&time2);
119     hour=8+p->tm_hour;
120     minute=p->tm_min;
121     second=p->tm_sec;
122     Time time1(hour,minute,second); 
123     while(1)
124     {
125         time1.show();
126         Sleep(1000);
127         //++time1;
128         time1++;
129         system("CLS");    
130     }
131     return 0;
132 }

 

posted @ 2019-09-16 18:50  浪迹天崖_1  阅读(420)  评论(0编辑  收藏  举报