• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C++计算用户打字速度

OC/C/C++ 水墨上仙 1984次浏览

C++计算用户打字速度

#include  <stdio.h>
#include  <dos.h>
#include  <conio.h>
#define   ESC 0x1b
#define   BSPACE 0x08
const unsigned long far * const dosTime =
    (const unsigned long far * const)MK_FP( 0x40, 0x6C );
class Timer
{
public:
    Timer();
    void start();
    void stop();
    void reset();
    int status();
    double time();
    static double resolution();
private:
    static unsigned adjust;
    static unsigned calibrate();
    int running;
    struct TIME
		 {
		 unsigned long dosCount;
		 unsigned timerCount;
		 };
    TIME startTime;
    double time_;
};
inline double Timer::time()
{
    return time_/1.E6;
}
inline double Timer::resolution()
{
    return 839/1.E9;
}
unsigned Timer::adjust = calibrate();
Timer::Timer() : time_(0), running(0)
{
}
void Timer::start()
{
    if( !running )
		 {
		 outportb( 0x43, 0x34 );
		 asm jmp __1;
    __1:
		 outportb( 0x40, 0 );
		 asm jmp __2;
    __2:
		 outportb( 0x40, 0 );
		 startTime.dosCount = *dosTime;
		 startTime.timerCount = 0;
		 running = 1;
		 }
}
void Timer::stop()
{
    outportb( 0x43, 0 );
    unsigned char temp = inportb( 0x40 );
    TIME stopTime;
    stopTime.timerCount = (inportb( 0x40 ) << 8) + temp;
    stopTime.dosCount = *dosTime;
    TIME elapsedTime;
    elapsedTime.dosCount = stopTime.dosCount - startTime.dosCount;
    elapsedTime.timerCount = -( stopTime.timerCount - adjust );
    const double fudge = 83810.0/100000.0;
    time_ += ((elapsedTime.dosCount << 16) +
elapsedTime.timerCount)*fudge;
    running = 0;
}
void Timer::reset()
{
    time_ = 0;
    if( running )
		 start();
}
unsigned Timer::calibrate()
{
    adjust = 0;
    unsigned long sum = 0;
    Timer w;
    for( int i = 0; i < 100; i++ )
		 {
		 w.start();
		 w.stop();
		 sum += w.time();
		 w.reset();
		 }
    return (unsigned)((sum+5)/100);
}
void main()
{
 clrscr();
 Timer t;
 char text[1000];
 int i = 0, space_count = 0, letter_count = 0;
 float duration;
 printf("
	       PROGRAM TO TEST TYPING SPEED
");
 printf("Hit any key to start timer...
");
 if(getch())
   {
    printf("Your time has started. Start typing. Hit Esc when done.
");
    t.start();
   }
 while(1)
 {
   text[i] = getche();
   letter_count++;
   if(text[i] == ' ')
      space_count++;
   if(text[i] == '
')
      printf("
");
   if(text[i] == BSPACE)
      printf(" "); // to erase previous character instead of cursoring
over
   if(text[i] == ESC)
    {
      printf(" ");
      // to eliminate a special character that is printed for Esc
      // A Backspace followed by Space erases previous character.
      break;
    }
 }
 t.stop();
 duration = t.time();
 printf("
Your typing speed is : 
");
 printf("%6.2f  characters per minute
",60*letter_count/duration);
 printf("%6.2f  words per minute (Actual)
",60*space_count/duration);
 printf("%6.2f  words per minute (Average)",60*letter_count/duration/5);
 getch();
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C++计算用户打字速度
喜欢 (0)
加载中……