渐渐地,博主发现了一个巨大的BUG:计时器用起来不灵敏,不好用。。。等博主有空再来改吧。
这就是一个计时器的变种,加入了键钮操作。没什么难的,几十行代码而已、
// making by zhouzhuan
#include <bits/stdc++.h>
#include <Windows.h>
using namespace std;
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000)? 1 : 0)
int l, r;
int hour, minute, second;
void get_timer()
{
system("cls");
printf("------------------计时器------------------\n");
printf(" making by zhouzhuan\n");
// begin segmentation time end
printf("开始(b) 分割(s) Time : %.2d : %.2d : %.2d 清除(e)\n", hour, minute, second);
return ;
}
int main()
{
while (true)
{
// initialization
int a[10005], len = 0;
hour = 0, minute = 0, second = 0;
get_timer();
// start timer
while (!KEY_DOWN('B')) ;
// get left time
l = clock();
// printf("%d\n", l);
// Sleep(5000);
while (true)
{
// end timer
if (KEY_DOWN('E'))
break;
else if (KEY_DOWN('S'))
{
//get right time
r = clock();
a[++len] = r - l;
}
// else
// {
get_timer();
// next time
second++;
if (second >= 60)
{
minute++;
second = 0;
}
if (minute >= 60)
{
hour++;
minute = 0;
}
if (hour >= 24)
hour = 0;
for (int i = 1; i <= len; i++)
printf("%d : %d\n", i, a[i]);
Sleep(1000);
// }
}
}
return 0;
}
嗯,很好。现在博主发现了BUG:原来是Sleep(1000)太影响读入,于是博主分段成100次读入。这下就基本没有问题啦。
// making by zhouzhuan
#include <bits/stdc++.h>
#include <Windows.h>
using namespace std;
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000)? 1 : 0)
int l, r;
int hour, minute, second;
void get_timer()
{
system("cls");
printf("------------------计时器------------------\n");
printf(" making by zhouzhuan\n");
// begin segmentation time end
printf("开始(b) 分割(s) Time : %.2d : %.2d : %.2d 清除(e)\n", hour, minute, second);
return ;
}
int main()
{
while (true)
{
// initialization
int a[10005], len = 0;
hour = 0, minute = 0, second = 0;
memset(a, 0, sizeof(a));
get_timer();
// start timer
while (!KEY_DOWN('B')) ;
// get left time
l = clock();
// printf("%d\n", l);
// Sleep(5000);
while (true)
{
// end timer
if (KEY_DOWN('E'))
break;
else if (KEY_DOWN('S'))
{
//get right time
r = clock();
a[++len] = r - l;
}
get_timer();
// next time
second++;
if (second >= 60)
{
minute++;
second = 0;
}
if (minute >= 60)
{
hour++;
minute = 0;
}
if (hour >= 24)
hour = 0;
for (int i = 1; i <= len; i++)
{
// print time
int _time = a[i] - l;
printf("%3d . %.2d:%.2d:%.2d:%.3d\n", i, _time / 3600000 % 60, _time / 60000 % 60, _time / 1000 % 60,_time % 1000);
}
// printf("------------------------------------------\n");
/*
quick time :
100 * 10 = 1000 ms = 1 s
second ++ / 1 s
*/
for (int i = 1; i <= 100; i++)
{
if (KEY_DOWN('S'))
{
// get right time
r = clock();
// get time
if (r - a[len] > 500)
a[++len] = r;
}
Sleep(10);
}
}
}
return 0;
}
还是几十行,没什么难度,大佬勿喷。