5:打字游戏

1、随机函数
1.1、srand((unsigned)time(NULL)):以当前时间为准,设置随机种子
1.2、ch = rand();注意:rand()函数每调用一次,产生一个随机数字;

2、获得键值函数
ch=_getch();//无需按回车,可直接获得键盘上按下的键值

3、时间函数
start_time=time(NULL);
end_time=time(NULL);

4、system("cls");//清空屏幕

点击查看代码
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<corecrt.h>


void help()
{
	printf("\n**********************************");
	printf("\n输入过程中无法退出!             *");
	printf("\n请按所给字母敲击键盘!           *");
	printf("\n输入出错则以_表示                *");
	printf("\n**********************************\n\n");
}

int main(int argc, char* argv[])
{
	char str[51] = "";
	char ch;
	int i, count;
	time_t start_time, end_time;
	while (1)
	{
		system("cls");//第一次玩过后如果要重新玩会自动清屏。
		help();
		ch = _getch();
		srand((unsigned int)time(NULL));
		for (i = 0; i < 50; i++)
		{
			str[i] = rand() % 26 + 'a';
		}
		str[i] = '\0';
		printf("%s\n", str);

		count = 0;
		for (i = 0; i < 50; i++)
		{
			ch = _getch();
			if (i == 0)
			{
				start_time = time(NULL);
			}
			if (ch == str[i])
			{
				count++;
				printf("%c", ch);
			}
			else
			{
				printf("_");
			}
		}
		end_time = time(NULL);

		printf("用时%ld秒\n", (long int)end_time - start_time);
		printf("\n正确率:%d %c\n", count*100/50,'%');
		while (1)
		{
			ch = _getch();
			if (ch == ' ')
			{
				break;
			}
			if (ch = 27)
			{
				return 0;
			}
		}
	}

	return 0;
}
posted @ 2023-12-24 00:01  首心  阅读(9)  评论(0)    收藏  举报