三周3

  1. <p>* 程序头部注释开始  
  2. * 程序的版权和版本声明部分  
  3. * Copyright (c) 2012, 烟台大学计算机学院学生  
  4. * All rights reserved.  
  5. * 文件名称:lianxi.cpp <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>  
  6. * 作 <wbr><wbr><wbr>者:刘文英<  
  7. * 完成日期:2012 年 3 月 12 日  
  8. * 版 本号:v3.2</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p><p>* 对任务及求解方法的描述部分  
  9. * 输入描述:......  
  10. * 问题描述:......  
  11. * 程序输出:......  
  12. * 程序头部的注释结束</p>  

  1. #include <iostream>   
  2.   
  3. using namespace std;  
  4.   
  5. class Time  
  6. {  
  7.     public://定义公用的数据或成员函数   
  8.         void set_time();  
  9.         void show_time();  
  10.     private://定义私有的数据或成员函数   
  11.         int hour;  
  12.         int minute;  
  13.         int sec;  
  14. };  
  15.   
  16. Time t;  
  17.   
  18. int main()  
  19. {  
  20.     t.set_time();  
  21.     t.show_time();//对象之中的函数   
  22.     return 0;  
  23. }  
  24.   
  25. void Time::set_time()//函数类型为空类型  作用域运算符   
  26. {  
  27.     cin >> t.hour;  
  28.     cin >> t.minute;  
  29.     cin >> t.sec;  
  30. }  
  31.   
  32. void Time::show_time()//函数类型为空类型  作用域运算符   
  33. {  
  34.     cout << t.hour << ":" << t.minute << ":" << t.sec << endl;  
  35. }  
  36.   
  37. #include <iostream>   
  38.   
  39. using namespace std;  
  40.   
  41. class Time  
  42. {  
  43.      public:  
  44.          void set_time();  
  45.          void show_time();  
  46.          void add_seconds();  
  47.          void add_minutes();  
  48.          void add_hours();  
  49.      private:  
  50.          bool is_time(intintint);  
  51.          int hour;  
  52.          int minute;  
  53.          int sec;  
  54. };  
  55.   
  56. int main()  
  57. {  
  58.     Time t1;  
  59.     Time & t2 = t1;  
  60.     t1.set_time();  
  61.     t1.add_seconds();  
  62.     t1.add_minutes();  
  63.     t1.add_hours();  
  64.     t2.show_time();  
  65.     return 0;  
  66. }  
  67.   
  68. void Time::set_time()  
  69. {  
  70.     char c1, c2;  
  71.     cout << "请输入时间(格式hh:mm:ss)";  
  72.   
  73.     while(1)  
  74.     {  
  75.         cin >> hour >> c1 >> minute >> c2 >> sec;  
  76.   
  77.         if(c1 != ':' || c2 != ':')  
  78.             cout << "格式不正确,请重新输入" << endl;  
  79.         else if(!is_time(hour, minute, sec))  
  80.             cout << "时间非法,请重新输入" << endl;  
  81.         else  
  82.             break;  
  83.     }  
  84. }  
  85.   
  86. void Time::show_time()  
  87. {  
  88.     cout << hour << ":" << minute << ":" << sec << endl;  
  89. }  
  90.   
  91. bool Time::is_time(int h, int m, int s)  
  92. {  
  93.     if(h < 0 || h >24 || m < 0 || m > 60 || s < 0 || s > 60)  
  94.         return false;  
  95.     return true;  
  96. }  
  97.   
  98. void Time::add_hours()  
  99. {  
  100.     int a;  
  101.   
  102.     cout << "请输入增加的小时:";  
  103.   
  104.     cin >> a;  
  105.   
  106.     hour = hour + a;  
  107.   
  108.     if(!is_time(hour, minute, sec))  
  109.             cout << "时间非法,请重新输入" << endl;  
  110.   
  111. }  
  112.   
  113. void Time::add_minutes()  
  114. {  
  115.     int a;  
  116.   
  117.     cout << "请输入增加的分钟:";  
  118.   
  119.     cin >> a;  
  120.   
  121.     minute = minute + a;  
  122.   
  123.     if(minute >= 60)  
  124.     {  
  125.         hour = hour + minute / 60;  
  126.         minute = minute % 60;  
  127.     }  
  128.   
  129.     if(!is_time(hour, minute, sec))  
  130.             cout << "时间非法,请重新输入" << endl;  
  131.       
  132. }  
  133.   
  134. void Time::add_seconds()  
  135. {  
  136.     int a;  
  137.   
  138.     cout << "请输入增加的秒数:";  
  139.   
  140.     cin >> a;  
  141.   
  142.     sec = sec + a;  
  143.   
  144.     if(sec >= 60)  
  145.     {  
  146.         minute = minute + sec / 60;  
  147.         sec = sec % 60;  
  148.           
  149.         if(minute >= 60)  
  150.         {  
  151.             hour = hour + minute / 60;  
  152.             minute = minute % 60;  
  153.         }  
  154.     }  
  155.   
  156.     if(!is_time(hour, minute, sec))  
  157.             cout << "时间非法,请重新输入" << endl;  
  158. }  
  • 运行结果:  
  • <img src="http://hi.csdn.net/attachment/201203/7/0_13311314756d88.gif" alt="" /> 

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值