C++定义一个时间计时类

来源:互联网 发布:尼康d810调焦软件 编辑:程序博客网 时间:2024/06/06 04:36
#include<iostream>#include<stdio.h>#include<Windows.h>//----------------------------------------定义一个自动显时的类-------------------------using namespace std;class timer{      //计时类    int hour;     //小时    int minute;   //分钟    int second;   //秒public:    void set(int h, int m, int s);//设置当前时间    void caculate();              //计时    void show();                  //显示时间};//-------------------------------------设置时间方法------------------------------------void timer::set(int h, int m, int s){    hour = h;    minute = m;    second = s;    if (h > 23 || h < 0)    {        cout << "h时间有误" << endl;    }    if (m > 59 || m < 0)    {        cout << "m时间有误" << endl;    }    if (s > 59 || s < 0)    {        cout << "s时间有误" << endl;    }}//--------------------------------------设置计时方法------------------------------------void timer::caculate(){        Sleep(1000);        second++;    if (second>59){        minute++;        second = 0;    }    if (minute > 59){        hour++;        minute = 0;    }    if (hour > 23){        hour = 0;    }}//----------------------------------------设置显示时间的类---------------------------------void timer::show(){    system("cls");    cout << hour << ":" << minute << ":" << second << endl;}int main1(){    cout << "请输入当前时间(时 分 秒)" << endl;    int a, b, c;    cin >> a >> b >> c;    timer t;    t.set(a, b, c);    while (1){        t.caculate();        t.show();    }    return 0;}
原创粉丝点击