c++实现“反应时间”测试

  一、程序说明:

1、程序会产生一个延迟时间,延迟一定的时间后,屏幕出现start!看到这个标志后,测试者摁空格键,程序输出测试者的反应时间,这个反应时间是测试者摁空格键的时刻与标志刚出现时刻的差值。

2、在start!标志出现前摁任意键,程序提示 “请不要抢答!”

3、看到 “Start! ” 后摁非空格键会提示“请输入空格键进行测试!”


  二、代码

1、头文件(reaction.h)

#ifndef _REACTION_H 
#define _REACTION_H

//函数声明
void instruction();//instruction函数用来输出开头的游戏说明
void time_record();//time_record函数是主体,用来实现具体的细节
#endif


2、源文件

(1)reaction.cpp

#include<reaction.h>
#include<time.h>
#include<conio.h>
#include<iostream>
#include<stdlib.h>

using namespace::std;

//instruction函数的具体实现
//可以根据具体的显示情况加"\t"或回车等...
void instruction()
{
	cout << endl;
	cout << "\t****************************************************************" << endl;
	cout << "\t\t     欢迎来到   “测试反应时间 ”  小游戏!" << endl;
	cout << "\t****************************************************************" << endl;
	cout << endl;
	cout << endl;
	cout << "        游戏说明:" << endl;
	cout << endl;
	cout << "\t\t1、看到“ Start! ” 后摁空格键即可进行测试" << endl;
	cout << endl;
	cout << "\t\t2、看到 “Start! ” 后摁非空格键会提示“请输入空格键进行测试!” " << endl;
	cout << endl;
	cout << "\t\t3、看到“ Start!”  之前就摁任意键会提示 “请不要抢答!” " << endl;
	
}
//time_record函数的具体实现
void time_record()
{
	int timeover;//用来表示是否超过随机生成的start!产生时间,若超过,timeover值为0
	double sec;//随机产生的start!延迟时间
	double sec1,sec2;//用来提高sec的随机性添加的两个随机变量
	double start;//时钟开始计时的时刻
	double time_begin;//start!出现时的时刻
	double time_end;//摁键后的时刻
	char key;//输入摁键
	char goon_key;//抢答情况出现后是否继续的输入摁键,这里有点小bug,最后调的时候加上后就ok了...
	srand(time(0));//为rand()产生随机种子,具体可百度srand和rand()的用法,time()函数需要#include<time.h>
	do{
		
        sec1 = rand();//rand()需要#include<stdlib.h>
		sec2 = rand();
		sec = (sec1 + sec2) / 2;
    } while ((sec<=2000)||(sec >= 7000));//产生的随机延迟时间必须在2000毫秒和7000毫秒之间
	start = double(clock());//start为开始的时刻,可以网上查一下clock()函数的用法,double后单位是毫秒
	//当time_over为1,说明时钟计时到现在还没有超过sec,start!标志还没有出现;反之超过sec值,说明start!出现了
	//当有摁键输入时,&&右边部分为0,跳出循环
	//&&左右两部分只要有一个的值是0,则跳出循环,否则一直执行while循环,即等待标志start!的出现,也可以理解为一个延时设置
	while ((timeover = (double(clock()) - start <= sec)) && !_kbhit());
	//若执行if语句,说明此时start!没有出现,但有摁键输入导致跳出while循环,这种情况即为抢答
	if (timeover == 1)
	{
		cout << endl;
		cout << endl;
		cout << endl;
		cout << "\t\t\t请不要抢答!" << endl;
		cout << endl;
		cout << endl;
		cout << endl;
		goon_key = _getch();//_getch()函数需要#include<conio.h>
	}
	//执行else语句说明timeover值为0,此时start!标志应出现,开始计反应时间
	else
	{
		cout << endl;
		cout << endl;
		cout << endl;
	    cout << "\t\t\t\tStart!" << endl;
     	time_begin = double(clock());
		key = _getch();//看到start!标志后输入摁键
	    time_end = double(clock());
		//只接受空格输入进行测试,否则提示“请输入空格键进行测试!”
		if (key != ' ')
		{
			cout << endl;
			cout << endl;
			cout << endl;
			cout << "\t\t\t请输入空格键进行测试!" << endl;
			cout << endl;
			cout << endl;
			cout << endl;
		}
		else
		{
			cout << endl;
			cout << endl;
			cout << endl;
			cout << "\t\t\t您的反应时间是:" << time_end - time_begin << "毫秒" << endl;//两个时刻差即为反应时间,此处忽略键盘的反应时间
		}
	}
}
(2)main函数的cpp代码

#include<reaction.h>
#include<iostream>
#include<conio.h>
using namespace::std;

int main()
{
	char continue_key;//测反应时间部分继续循环的输入摁键
	char instruction_key;//说明部分继续的输入摁键
	do
	{
	    instruction();
		cout << endl;
		cout <<endl;
		cout <<endl;
		cout << endl;
		cout << "\t\t\t\t输入y继续..." << endl;
		instruction_key = _getch();
	} while (instruction_key != 'y');
	
	do{
		system("cls");
	    time_record();
		cout << endl; 
		cout << "\t\t是否重新开始?输入y确定,输入其他键退出游戏..." << endl;
		continue_key = _getch();
	} while (continue_key == 'y');
	system("pause");
	return 0;
}

下面是程序运行时的样子:



注:刚开始写代码的人喜欢把所有东西写在一个cpp里,这里还是规范地分开写比较好。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值