#include <ctime>

class CTimer
{
    public:
        clock_t Begin;
        clock_t End;

       
        double elapTicks;
        double elapMilli, elapSeconds, elapMinutes;

      
        CTimer () {this->Begin = clock () * CLK_TCK;}
      
        void Stop () { this->End   = clock () * CLK_TCK; getTimes ();}

      
        void getTimes ()
        {
                        this->elapTicks   = this->End – this->Begin;
            this->elapMilli   = this->elapTicks/1000;   
            this->elapSeconds = this->elapMilli/1000; 
            this->elapMinutes = this->elapSeconds/60;  
        }
};

/** EXAMPLE USAGE **/
#include <iostream>
using namespace std;
int main ()
{
    CTimer myTimer;
    char input;
    cin >> input;
    myTimer.Stop ();
    cout << myTimer.elapSeconds << endl;

    cin.get ();
    return EXIT_SUCCESS;
}

 

 

4 对 “定时器类(C++实现)”的想法;

  1. 测试头像。。哇咔咔
    另外,为什么百度百科里面写的是用clock ()除以 CLK_TCK?
    CLK_TCK是一秒的滴答数目?不懂。。。

评论被关闭。