第五周项目3-时间类

来源:互联网 发布:福建知鱼科技有限公司 编辑:程序博客网 时间:2024/04/30 23:27

 

/*2015.烟台大学计算机与控制工程学院 *ALL rightreserved. *文件名称:test.cpp *作者:陈文浩 *完成日期:2016年4月6日。 */  /*问题及代码: 阅读、运行程序后,按要求扩充类的功能[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片#include <iostream>  using namespace std;  class Time  {  public:      void set_time( );         void show_time( );    private:       bool is_time(int, int, int);   //这个成员函数设置为私有的,是合适的,请品味      int hour;      int minute;      int sec;  };  void Time::set_time( )   {      char c1,c2;      cout<<"请输入时间(格式hh:mm:ss)";      while(1)      {    cin>>hour>>c1>>minute>>c2>>sec;          if(c1!=':'||c2!=':')              cout<<"格式不正确,请重新输入"<<endl;          else if (!is_time(hour,minute,sec))              cout<<"时间非法,请重新输入"<<endl;          else               break;      }  }  void Time::show_time( )        {      cout<<hour<<":"<<minute<<":"<<sec<<endl;  }  bool Time::is_time(int h,int m, int s)  {      if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)          return false;      return true;  }  int main( )  {      Time t1;        t1.set_time( );         t1.show_time( );      return 0;  }  


 

 

 


要求:
(1)请在原类基础上,在类内增加下列成员函数(将是内置成员函数)

  • add_a_sec()  //增加1秒钟
  • add_a_minute() //增加1分钟
  • add_an_hour() //增加1小时

  在main()数中,调用新增加的成员函数,以测试扩充后的功能。
(2)再增加三个成员函数,要求在类内声明,类外定义。

  • add_seconds(int) //增加n秒钟
  • add_minutes(int) //增加n分钟
  • add_hours(int) //增加n小时

提示:

  • 要考虑增加后超出取值范围的情形;
  • 增加n秒后,秒数可能会超过60,调整秒数,并可以调用增加分钟数的成员函数,使时间合法;同理,增加分钟数也有类似问题。

 

 

01.#include <iostream>  02.using namespace std;  03.class Time  04.{  05.public:  06.    void set_time( );  07.    void show_time( );  08.    inline void add_a_sec();  //增加1秒钟  09.    inline void add_a_minute(); //增加1分钟  10.    inline void add_an_hour(); //增加1小时  11.    void add_seconds(int); //增加n秒钟  12.    void add_minutes(int); //增加n分钟  13.    void add_hours(int); //增加n小时  14.private:  15.    bool is_time(int, int, int);  16.    int hour;  17.    int minute;  18.    int sec;  19.};  20.  21.  22.void Time::set_time( )  23.{  24.    char c1,c2;  25.    cout<<"请输入时间(格式hh:mm:ss)";  26.    while(1)  27.    {  28.        cin>>hour>>c1>>minute>>c2>>sec;  29.        if(c1!=':'||c2!=':')  30.            cout<<"格式不正确,请重新输入"<<endl;  31.        else if (!is_time(hour,minute,sec))  32.            cout<<"时间非法,请重新输入"<<endl;  33.        else  34.            break;  35.    }  36.}  37.  38.void Time::show_time( )  39.{  40.    cout<<hour<<":"<<minute<<":"<<sec<<endl;  41.}  42.  43.bool Time::is_time(int h,int m, int s)  44.{  45.    if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)  46.        return false;  47.    return true;  48.}  49.  50.inline void Time::add_a_sec()  //增加1秒钟  51.{  52.    ++sec;              //直接修改sec的值即可,sec是Time类的数据成员  53.    if (sec>59)          //sec超出规定的范围,因为只是增加1秒,最多也就是向分钟进位1,所以增加1分钟。  54.    {  55.        sec=0;  56.        add_a_minute(); //至于增加1分钟是否会引起小时变化,由add_a_minute()处理  57.    }  58.}  59.  60.inline void Time::add_a_minute() //增加1分钟  61.{  62.    ++minute;  63.    if (minute>59)       //参见add_a_sec()中的注释  64.    {  65.        minute=0;  66.        add_an_hour();  67.    }  68.}  69.  70.inline void Time::add_an_hour() //增加1小时  71.{  72.    ++hour;  73.    if (hour>23)  74.        hour=0;     //到第2天了  75.  76.}  77.void Time::add_seconds(int n) //增加n秒钟  78.{  79.    sec+=n;         //直接加上去。此操作可能使sec超出取值范围,将在下面处理,我们只要保证此函数执行完sec的取值正确即可  80.    if (sec>59)      //思考:if中的两条语句能否交换顺序?为什么不能?后果将是……?  81.    {  82.        add_minutes(sec/60);    //增加sec/60分钟  83.        sec%=60;                //秒数应该是sec%=60  84.    }  85.}  86.  87.void Time::add_minutes(int n) //增加n分钟  88.{  89.    minute+=n;  90.    if (minute>59)       //参见add_seconds()中的注释  91.    {  92.        add_hours(minute/60);  93.        minute%=60;  94.    }  95.}  96.  97.void Time::add_hours(int n) //增加n小时  98.{  99.    hour+=n;  100.    if (hour>23)  101.        hour%=24;       //此程序不涉及日期,如果设计类DateTime,修改将继续下去  102.}  103.  104.int main( )  105.{  106.    Time t1;  107.    Time &t2=t1;  108.    t1.set_time( );  109.    cout<<"现在时间是:";  110.    t2.show_time( );  111.  112.    t1.add_a_sec();  //增加1秒钟  113.    cout<<"增加1秒钟后:";  114.    t1.show_time( );  115.  116.    t1.add_a_minute(); //增加1分钟  117.    cout<<"增加1分钟后:";  118.    t1.show_time( );  119.  120.    t1.add_an_hour(); //增加1小时  121.    cout<<"增加1小时后:";  122.    t1.show_time( );  123.  124.    t1.add_seconds(40); //增加40秒钟  125.    cout<<"增加40秒钟后:";  126.    t1.show_time( );  127.  128.    t1.add_minutes(127); //增加127分钟  129.    cout<<"增加127分钟后:";  130.    t1.show_time( );  131.  132.    t1.add_hours(8); //增加8小时    cout<<"增加8小时后:";      t1.show_time( );      return 0;  }  


 

0 0