1. 程式人生 > >第五週專案3-時間類

第五週專案3-時間類

/*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;  

}  
#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;  
}