第五周项目3(1)

  1. #include <iostream>   
  2. using namespace std;  
  3. class Time  
  4. {  
  5. public:  
  6.     void set_time();  
  7.     void show_time();  
  8.     void add_a_sec(){sec++;}  
  9.     void add_a_minute(){minute++;}  
  10.     void add_an_hour(){hour++;show_time();}  
  11. private:  
  12.     bool is_time(intintint);  
  13.     int hour;  
  14.     int minute;  
  15.     int sec;  
  16. };  
  17. void Time::set_time()  
  18. {  
  19.     char c1,c2;  
  20.     cout<<"请输入时间(格式hh:mm:ss)";  
  21.     while(1)  
  22.     {  
  23.         cin>>hour>>c1>>minute>>c2>>sec;  
  24.         if(c1!=':'||c2!=':')  
  25.             cout<<"格式不正确重输"<<endl;  
  26.         else if(!is_time(hour,minute,sec))  
  27.             cout<<"时间非法,请重新输入"<<endl;  
  28.         else  
  29.             break;  
  30.     }  
  31. }  
  32. void Time::show_time()  
  33. {hour=hour%24;minute=minute%60;sec=sec%60;  
  34.     cout<<hour<<":"<<minute<<":"<<sec<<endl;  
  35. }  
  36. bool Time::is_time(int h,int m,int s)  
  37. {  
  38.     if(h<0||h>24||m<0||m>60||s<0||s>60)  
  39.         return false;  
  40.     return true;  
  41. }  
  42. int main( )  
  43. {  
  44.     Time t1;  
  45.     t1.set_time();  
  46.     t1.show_time();  
  47.     t1.add_a_sec();  
  48.     t1.add_a_minute();  
  49.     t1.add_an_hour();  
  50.     return 0;  
  51. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值