飛飛(二十三)时间类

#include <iostream>
#include"cmath"
using namespace std;
class Time
{
public:
      void set_time();
      void show_time();
private:
      int hour;
      int minute;
      int sec;
      bool is_time(int, int, int );
};
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 n)
      {
            if(h<0||h>24||m<0||m>60||n<0||n>60)
                  return false;
            else
                  return true;
      }
     int main()
     {
      Time t1;
      t1.set_time();
      t1.show_time();
      return 0;
     }

<img src="http://img.blog.csdn.net/20160410152708949?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

你可能感兴趣的:(C语言)