设计一个多功能的MyTime类 代码参考

  1 #include <iostream>
  2 #include <cstdio>
  3 
  4 using namespace std;
  5 
  6 class MyTime
  7 {
  8     private:
  9         int hour;
 10         int minute;
 11         int second;
 12     public:
 13         MyTime(){hour=0;minute=0;second=0;}
 14         MyTime(int,int,int);
 15         void Register(int,int,int);
 16         void Show_12();
 17         void Show_24();
 18         void Sub(int,int,int);
 19         void Add(int,int,int);
 20 };
 21 
 22 MyTime::MyTime(int a, int b, int c)
 23 {
 24     hour=a;
 25     minute=b;
 26     second=c;
 27 }
 28 
 29 void MyTime::Show_12()
 30 {
 31     if(hour>11)
 32     {
 33         printf("%02d:%02d:%02d PM\n",hour-12,minute,second);
 34     }
 35     else
 36     {
 37         printf("%02d:%02d:%02d AM\n",hour,minute,second);
 38     }
 39     return;
 40 }
 41 
 42 void MyTime::Show_24()
 43 {
 44     printf("%02d:%02d:%02d\n",hour,minute,second);
 45     return;
 46 }
 47 
 48 void MyTime::Sub(int a, int b, int c)
 49 {
 50     hour-=a;
 51     minute-=b;
 52     second-=c;
 53     while(second<0)
 54     {
 55         second+=60;
 56         minute-=1;
 57     }
 58     while(minute<0)
 59     {
 60         minute+=60;
 61         hour-=1;
 62     }
 63     while(hour<0)
 64     {
 65         hour+=24;
 66     }
 67     return;
 68 }
 69 
 70 void MyTime::Add(int a, int b, int c)
 71 {
 72     hour+=a;
 73     minute+=b;
 74     second+=c;
 75     while(second>59)
 76     {
 77         second-=60;
 78         minute+=1;
 79     }
 80     while(minute>59)
 81     {
 82         minute-=60;
 83         hour+=1;
 84     }
 85     while(hour>23)
 86     {
 87         hour-=24;
 88     }
 89     return;
 90 }
 91 
 92 void MyTime::Register(int a,int b, int c)
 93 {
 94     hour=a;
 95     minute=b;
 96     second=c;
 97     return;
 98 }
 99 
100 int main()
101 {
102     MyTime one,two(8,10,30);
103     int a,b,c,x,y,z;
104     cin>>a>>b>>c>>x>>y>>z;
105     one.Show_12();
106     one.Show_24();
107     two.Show_12();
108     two.Show_24();
109     one.Register(a,b,c);
110     one.Add(x,y,z);
111     two.Sub(x,y,z);
112     one.Show_12();
113     one.Show_24();
114     two.Show_12();
115     two.Show_24();
116     return 0;
117 }

 

posted @ 2020-03-29 14:04  Conan-jine  阅读(484)  评论(0编辑  收藏  举报