f0806.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:1k
源码类别:

C#编程

开发平台:

Visual C++

  1. //==================================
  2. // f0806.cpp
  3. // class Time's operator++
  4. //==================================
  5. #include<iostream>
  6. #include<iomanip>
  7. using namespace std;
  8. //----------------------------------
  9. class Time{
  10.   int hour, minute, second;
  11. public:
  12.   void set(int h, int m, int s){ hour=h, minute=m, second=s; }
  13.   friend Time& operator++(Time& a);
  14.   friend Time operator++(Time& a, int);
  15.   friend ostream& operator<<(ostream& o, const Time& t);
  16. };//-------------------------------
  17. Time& operator++(Time& a){
  18.   if(!(a.second=(a.second+1)%60)&&!(a.minute=(a.minute+1)%60))
  19.     a.hour=(a.hour+1)%24;
  20.   return a;
  21. }//--------------------------------
  22. Time operator++(Time& a, int){
  23.   Time t(a);
  24.   if(!(a.second=(a.second+1)%60)&&!(a.minute=(a.minute+1)%60))
  25.     a.hour=(a.hour+1)%24;
  26.   return t;
  27. }//------------------------------------
  28. ostream& operator<<(ostream& o, const Time& t){
  29.   o<<setfill('0')<<setw(2)<<t.hour<<":"<<setw(2)<<t.minute<<":";
  30.   return o<<setw(2)<<t.second<<"n"<<setfill(' ');
  31. }//--------------------------------
  32. int main(){
  33.   Time t;
  34.   t.set(11, 59, 58);
  35.   cout<<t++;
  36.   cout<<++t;
  37. }//================================
  38.