设计一个多功能的MyTime类 代码参考
程序员文章站
2022-06-15 12:45:13
1 #include 2 #include 3 4 using namespace std; 5 6 class MyTime 7 { 8 private: 9 int hour; 10 int minute; 11 int second; 12 public ......
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 }
上一篇: 汗血宝马有什么特点?汗血宝马来自于哪里?