#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <iostream.h>
#include <iomanip.h>
#define ESC 27
#if (__BORLANDC__ <= 0x460)
enum bool { false, true };
#endif
class TChrono
{
clock_t time, start, end;
int res;
bool paused;
public:
TChrono();
virtual ~TChrono();
void Start();
void Stop();
void Pause();
void Continue();
bool IsPaused();
clock_t Time();
};
TChrono::TChrono()
: time(0), start(0), end(0), res(100), paused(true)
{}
TChrono::~TChrono()
{}
void TChrono::Start()
{
start = clock();
time = 0;
paused = false;
}
void TChrono::Stop()
{
if(!IsPaused())
{
end = clock();
time += (end - start) / CLK_TCK * res;
paused = true;
}
}
void TChrono::Pause()
{
Stop();
}
void TChrono::Continue()
{
if(IsPaused())
{
paused = false;
start = clock();
}
}
bool TChrono::IsPaused()
{
return paused;
}
clock_t TChrono::Time()
{
return time + (IsPaused() ? 0 : + (clock() - start) / CLK_TCK * res);
}
void main()
{
TChrono chrono;
char opt;
clrscr();
_setcursortype(_NOCURSOR);
cout << "1 - Start" << endl;
cout << "2 - Stop" << endl;
cout << "3 - Pause" << endl;
cout << "4 - Continue" << endl;
cout << "ESC - Exit" << endl;
do
{
while(!kbhit())
{
clock_t time = chrono.Time();
clock_t dec = time % 100;
clock_t sec = time / 100;
clock_t min = sec / 60;
sec %= 60;
gotoxy(10, 10);
cout << setfill('0')
<< setw(2) << min << '\''
<< setw(2) << sec << '"'
<< setw(2) << dec;
}
opt = getch();
switch(opt)
{
case '1':
chrono.Start();
break;
case '2':
chrono.Stop();
break;
case '3':
chrono.Pause();
break;
case '4':
chrono.Continue();
break;
}
} while(opt != ESC);
_setcursortype(_NORMALCURSOR);
}