#include<iostream>
#include<iomanip>
#include<time.h>
#include<windows.h>
#include<conio.h>
using namespace std;
class Time
{
private:
int h,m,s;
long a;
char test;
public:
Time(){h=0;m=0;s=58;}
void time0() {h=0;m=0;s=0;}
friend ostream & operator << (ostream & B,Time &AA);
void gotoxy(int x, int y);
void hidden();
void begin();
};
void Time :: hidden()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=0;
SetConsoleCursorInfo(hOut,&cci);
}
void Time :: gotoxy(int x, int y)
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void Time::begin()
{
cout <<(*this);
while(1)
while(kbhit()&&(test=getch())=='a')
{
while(!kbhit()||(test=getch())!='s'&&test!=' ')
{
cout <<(*this);
a=time(0);
while(time(0)!=a+1);
if(++s>=60)
{m++;s=0;}
if((m+1)==60)
{m=0;h++;}
if((h+1)==24)
{h=0;}
}
if(test==' ');
else
time0();
}
}
ostream & operator << (ostream & B,Time & AA)
{
AA.gotoxy(20,10);
AA.hidden();
B << setw(2)<<setfill('0');
B<<AA.h<<":";
B << setw(2)<<setfill('0');
B<<AA.m<<":";
B << setw(2)<<setfill('0');
B<<AA.s<<endl;
AA.gotoxy(20,20);
AA.hidden();
cout<< "press 'a' to start /'s' to over / 'block' to stop"<<endl;
return B;
}
int main()
{
Time a;
a.begin();
return 0;
}
开始
运行
重新开始