sdut 2-2 Time類的定義

來源:互聯網
上載者:User

標籤:des   style   blog   io   os   ar   strong   檔案   資料   

2-2 Time類的定義 Time Limit: 1000MS Memory limit: 65536K 題目描述

通過本題目的練習可以掌握類與對象的定義;

設計一個時間類Time,私人資料成員有hour(時)、minute(分)、second(秒);

公有成員函數有:setHour(int)設定資料成員hour的值,非法的輸入預設為12;setMinue(int)設定資料成員minute的值,非法輸入預設為0;setSecond(int)設定資料成員second的值,非法輸入預設為0;setTime(int,int,int)設定時、分、秒三個資料成員的值;showTime()顯示時間對象的值。

在主函數main()中調用相應成員函數,使得時間對象的值能從鍵盤接收,並正確顯示。

輸入 

輸入3個整數,用一個空格間隔

輸出 

輸出 時、分、秒的值,中間用“:”間隔

樣本輸入
10 11 12
樣本輸出
10:11:12
提示

輸入

58 23 85

輸出

12:23:00

來源 黃晶晶樣本程式

這個題遇到一點小問題,當分和秒等於0的時候要輸出00,在c裡用”%02d“能實現(需要用到#include<stdio.h>),在才++中用cout.with(0); cout.fill("0");也可以,不過後者麻煩,(不需要單獨的標頭檔),而且在這題中後者沒實現出來,求教!!

#include <iostream>#include<stdio.h>using namespace std;//聲明類類型class time{//定義私人部分private:    int h;    int m;    int s;//定義公有部分public:    int sethour()//控制時成員函數    {        if(h>12 || h<0)            h=12;        return h;    }    int setminue()//控制分成員函數    {        if(m>60 || m<0)            m=0;        return m;    }    int setsecond()//控制秒成員函數    {        if(s>60 || s<0)            s=0;        return s;    }    void settime()//賦值成員函數    {        cin >>h>>m>>s;    }    void showtime()//輸出成員函數    {        printf("%02d:%02d:%02d\n",h,m,s);//需要用到#include<stdio.h>    }};//不要忘了這個分號//主函數int main(){    class time t;    t.settime();    t.sethour();    t.setminue();    t.setsecond();    t.showtime();    return 0;}


sdut 2-2 Time類的定義

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.