第四周 20 时间类

来源:互联网 发布:工程进度表制作软件 编辑:程序博客网 时间:2024/06/05 10:08
  1. /* 
  2. *Copyright (c) 2015,烟台大学计算机学院 
  3. *All rights reserved. 
  4. *文件名称:text.cpp 
  5. *作者:李德彪
  6. *完成日期:2015年3月25日 
  7. *版本号:v1.0 
  8. * 
  9. *问题描述:设计时间类 
  10. *输入描述:输入小时,分钟,秒 
  11. *程序输出:输出时间 
  12. */  
  13. #include <iostream>   
  14. using namespace std;  
  15. class Time  
  16. {  
  17. public:  
  18.     void set_time();  
  19.     void show_time();  
  20.     void add_a_seconds()  
  21.     {  
  22.         sec++;  
  23.         if(sec>=60)  
  24.         {  
  25.            sec=sec-60;  
  26.            minute++;  
  27.            if(minute>=60)  
  28.            {  
  29.                minute=minute-60;  
  30.                hour++;  
  31.            }  
  32.         }  
  33.   
  34.   
  35.     }  
  36.     void add_a_minute()  
  37.     {  
  38.         minute++;  
  39.         if(minute>=60)  
  40.            {  
  41.                minute=minute-60;  
  42.                hour++;  
  43.            }  
  44.     }  
  45.     void add_an_hour()  
  46.     {  
  47.         hour++;  
  48.     }  
  49. private:  
  50.     bool is_time(int ,int ,int );  
  51.     int hour;  
  52.     int minute;  
  53.     int sec;  
  54. };  
  55. void Time::set_time()  
  56. {  
  57.     char c1,c2;  
  58.     cout<<"请输入时间(格式hh:mm:ss)";  
  59.     while(1)  
  60.     {  
  61.         cin>>hour>>c1>>minute>>c2>>sec;  
  62.         if(c1!=':'||c2!=':')  
  63.         {  
  64.             cout<<"格式不正确重输"<<endl;  
  65.   
  66.         }  
  67.         else if(!is_time(hour,minute,sec))  
  68.           cout<<"时间非法,请重新输入 "<<endl;  
  69.         else  
  70.             break;  
  71.     }  
  72. }  
  73. void Time::show_time()  
  74. {  
  75.     cout<<hour<<":"<<minute<<":"<<sec<<endl;  
  76.      if(!is_time(hour,minute,sec))  
  77.           cout<<"时间非法 "<<endl;  
  78. }  
  79. bool Time::is_time(int h,int m,int s)  
  80. {  
  81.     if(h<0||h>24||m<0||m>60||s<0||s>60)  
  82.         return false;  
  83.         else  
  84.         return true;  
  85. }  
  86. int main()  
  87. {  
  88.     Time t1;  
  89.     t1.set_time();  
  90.     t1.add_a_seconds();  
  91.     t1.add_a_minute();  
  92.     t1.add_an_hour();  
  93.     t1.show_time();  
  94.     return 0;  
  95. }  
0 0
原创粉丝点击