4------2

来源:互联网 发布:java虚拟机原理pdf 编辑:程序博客网 时间:2024/05/17 22:38
 

头文件。

[cpp] view plaincopyprint?
  1. class Time    
  2. {    
  3. public:    
  4.     void set_time( );       
  5.     void show_time( );     
  6.     void add_seconds(int);    
  7.     void add_minutes(int);    
  8.     void add_hours(int) ;    
  9. private:     
  10.     bool is_time(intintint);    
  11.     int hour;    
  12.     int minute;    
  13.     int sec;    
  14. };    


源文件。

[cpp] view plaincopyprint?
  1. #include <iostream>   
  2.   
  3. #include "ssss.h"   
  4.   
  5. using namespace std;  
  6.   
  7. int main( )    
  8. {    
  9.     int h,m,s;    
  10.     Time t1;      
  11.     Time &t2=t1;    
  12.     t1.set_time( );     
  13.     cout << "需要增加的秒数" << endl;    
  14.         
  15.     cin >> s;    
  16.         
  17.     t1.add_seconds(s);    
  18.         
  19.     cout << "需要增加的分钟数" << endl;    
  20.         
  21.     cin >> m;    
  22.         
  23.     t1.add_minutes(m);    
  24.         
  25.     cout << "需要增加的小时数" << endl;    
  26.         
  27.     cin >> h;    
  28.         
  29.     t1.add_hours(h);    
  30.         
  31.     t2.show_time();    
  32.         
  33.     t2.show_time( );    
  34.   
  35.     system("pause");  
  36.     return 0;    
  37. }    
  38.     
  39. void Time::set_time( )     
  40. {    
  41.     char c1,c2;    
  42.     cout<<"请输入时间(格式hh:mm:ss)";    
  43.     while(1)    
  44.     {    
  45.         cin>>hour>>c1>>minute>>c2>>sec;    
  46.         if(c1!=':'||c2!=':')    
  47.             cout<<"格式不正确,请重新输入"<<endl;    
  48.         else if (!is_time(hour,minute,sec))    
  49.             cout<<"时间非法,请重新输入"<<endl;    
  50.         else     
  51.             break;    
  52.     }    
  53. }    
  54.     
  55. void Time::show_time( )          
  56. {    
  57.     cout<<hour<<":"<<minute<<":"<<sec<<endl;    
  58. }    
  59.     
  60. bool Time::is_time(int h,int m, int s)    
  61. {    
  62.     if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)    
  63.         return false;    
  64.     return true;    
  65. }     void Time::add_seconds(int n)    
  66. {    
  67.      sec = sec + n;    
  68.          
  69.      while(sec >= 60)    
  70.      {    
  71.                sec = sec - 60;    
  72.                minute++;    
  73.      }     
  74. }    
  75. void Time::add_minutes(int n)    
  76. {    
  77.      minute = minute + n;    
  78.          
  79.      while(minute >= 60)    
  80.      {    
  81.          minute = minute - 60;    
  82.          hour++;    
  83.      }    
  84. }    
  85. void Time::add_hours(int n)    
  86. {    
  87.     hour = hour + n;    
  88. }         
原创粉丝点击