1933: 【C++运算符重载】时间类(24小时)——待完善

来源:http://acm.ujn.edu.cn

Description

定义24小时制时间类time24,数据成员包含:hours,minutes,seconds,成员函数包括:

(1) 构造函数time24(int h, int m, int s),可以设定默认的形参值为0:0:0

(2) 赋值函数set_time (int h, int m, int s),用来给三个数据成员赋值,暂时不考虑非法赋值的情况

(3) 获取时间函数get_time (int &h, int &m, int &s),用来获得time24的数据成员小时、分钟、秒

(4) 重载运算符+,可以实现时间对象和秒数相加

(5) 重载运算符+,可以实现两个时间对象相加

(6) 重载运算符-,可以实现时间对象和秒数相减

(7) 重载运算符-,可以实现两个时间对象相减

 

实现上述功能,使主函数正常输出,主函数如下

int main()
{
    int h, m, s, secs;
    time24 t1(0, 59, 57);
    time24 t2, t3;
    cin >> secs;
    t2 = t1 + secs;
    t2.get_time(h, m, s);
    cout << h << ':' << m << ':' << s << endl;
    t2 = t2 - secs;
    t2.get_time(h, m, s);
    cout << h << ':' << m << ':' << s << endl;
    
    cin >> h >> m >> s;
    t2.set_time(h, m, s)
    t3 = t1 + t2;
    t3.get_time(h, m, s);
    cout << h << ':' << m << ':' << s << endl;
    t1 = t3 - t2;
    t1.get_time(h, m, s);
    cout << h << ':' << m << ':' << s << endl;
    
    return 0; 
}

Input

输入数据为两行,第一行为增加、减少的秒数;第二行为增加、减小的时间(小时、分钟、秒)

Output

主函数的实际输出

Sample Input

10000

1 30 30

Sample Output

3:46:37

0:59:57

2:30:27

0:59:57

题解

  1 #include<iostream>
  2 using namespace std;
  3 class time24
  4 {
  5     public:
  6         time24(int h=0,int m=0,int s=0);
  7         void set_time (int h, int m, int s);
  8         void get_time (int &h, int &m, int &s)const;
  9         time24 operator + (int s)const;
 10         time24 operator + (const time24 &tt)const;
 11         time24 operator - (int s)const;
 12         time24 operator - (const time24 &t)const;
 13     private:
 14         int hours,minutes,seconds;
 15 };
 16 int main()
 17 {
 18     time24 t1(0, 59, 57),t2, t3;int h, m, s, secs;
 19     cin >> secs;
 20     t2 = t1 + secs;
 21     t2.get_time(h, m, s);cout << h << ':' << m << ':' << s << endl;
 22     t2 = t2 - secs;
 23     t2.get_time(h, m, s);cout << h << ':' << m << ':' << s << endl;
 24     
 25     cin >> h >> m >> s;
 26     t2.set_time(h, m, s);
 27     t3 = t1 + t2;
 28     t3.get_time(h, m, s);cout << h << ':' << m << ':' << s << endl;
 29     t1 = t3 - t2;
 30     t1.get_time(h, m, s);cout << h << ':' << m << ':' << s << endl;
 31     return 0;
 32 }
 33 time24::time24(int h,int m,int s)
 34 {
 35     hours=h;
 36     minutes=m;
 37     seconds=s;
 38 }
 39 void time24::set_time(int h,int m,int s)
 40 {
 41     hours=h;
 42     minutes=m;
 43     seconds=s;
 44 }
 45 void time24::get_time(int &h,int &m,int &s)const
 46 {
 47     h=hours;
 48     m=minutes;
 49     s=seconds;
 50 }
 51 time24 time24::operator + (int s) const 
 52 {
 53     time24 t;
 54     t.seconds = seconds + s; 
 55     t.minutes = minutes + t.seconds / 60 ;
 56     t.seconds %= 60 ;
 57     t.hours = hours + t.minutes / 60 ; 
 58     t.minutes %= 60 ;
 59     t.hours %= 24;
 60     return t;
 61 }
 62 time24 time24::operator + (const time24 &tt)const
 63 {
 64     time24 t;
 65     int secs=tt.hours*3600 + tt.minutes*60 + tt.seconds;
 66     t.seconds = seconds + secs;  
 67     t.minutes = minutes + t.seconds / 60 ;
 68     t.seconds %= 60 ;
 69     t.hours = hours + t.minutes / 60 ; 
 70     t.minutes %= 60 ;
 71     t.hours %= 24;
 72     return t;
 73 }
 74 time24 time24::operator - (int s)const
 75 {
 76     time24 t;
 77     int ss,mm,hh,mmm,sss,hhh;
 78     ss=s%60;mm=(s/60)%60;hh=s/3600;
 79     t.hours=hh;t.minutes=mm;t.seconds=ss;
 80     sss=seconds-t.seconds;
 81     mmm=minutes-t.minutes;
 82     hhh=hours-t.hours;
 83     if (sss<0){sss+=60;mmm--;}
 84     if (mmm<0){mmm+=60;hhh--;}
 85     while (hhh<0) hhh+=24;
 86     t.hours=hhh;t.minutes=mmm;t.seconds=sss;
 87     return t;
 88 }
 89 time24 time24::operator - (const time24 &t)const
 90 {
 91     time24 tt;
 92     int h,m,s;
 93     s=seconds-t.seconds;
 94     m=minutes-t.minutes;
 95     h=hours-t.hours;
 96     if (s<0){s+=60;m--;}
 97     if (m<0){m+=60;h--;}
 98     while (h<0) h+=24;
 99     tt.hours=h;tt.minutes=m;tt.seconds=s;
100     return tt;
101 }

 

posted @ 2022-06-11 16:55  杏花疏影  阅读(348)  评论(0)    收藏  举报