一个简单打字游戏的设计(C语言)

需求:

生成的20个随机字符串由大小写组成,规则如下:

程序源码:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void display();
void list_bulid();
char str[21];
int main(int argc, char const *argv[])
{
	/* code */
	display();
	return 0;
}
void display()
{
	printf("*****************************************\n");
	printf("*输入过程无法退出!                  	*\n");
	printf("*请按所给字母敲击键盘!    	     	*\n");
	printf("*按任意键开始测试,按下首字母开始计时!	*\n");
	printf("*输入出则以 _ 表示                   	*\n");
	printf("*****************************************\n");
	system("stty -icanon");//关闭缓冲区,输入字符无需按回车键直接接受
	getchar();
	printf("\b\n");
	list_bulid();
	return ;
}
/***********************************
建立一串字母,并对键盘输出进行处理的函数

************************************/
void list_bulid()
{
	int i = 0;
	int t;
	int right = 20;
	char c;
	time_t start,end;
	srand((unsigned)time(NULL));
	while(i < 20)
	{
		t=rand() % 2;
		if(t == 0)
			c ='a'+rand()%26;
		else
			c = 'A'+rand()%26;
		str[i] = c;
		i++;
		printf("%c",c);
	}
	printf("\n");
	system("stty -icanon");//关闭缓冲区,输入字符无需按回车键直接接受
	i = 0;
	while(i < 20)
	{
		c = getchar();
		if(i == 0) //计时开始
		{
			start = time(0);
		}
		//printf("%c --- %c -\n", str[i] , c);
		if(str[i] != c)
		{
			printf("\b_");
			right--;
		}
		//else printf("%c!", c);
		i++;
		//printf("%s\n",str );*/
	}
	end = time(0); //计时结束
	printf("\n完成输入!\n");
	i = end - start;
	printf("用时%d s\n", i);
	printf("正确率%.2lf %%", right/20.0 * 100);
	printf("按下Esc退出,空格键继续!\n");
	while(1)
	{
		c = getchar();
		i = c;
		if(c == ' ')
		{
			printf("\n");
			display();
		}
		else if(i == 27)
		{
			printf("\b");
			printf("\b");
			printf("\b");
			printf("\b");
			return ;
		}
		else
		{
			printf("\b");
		}
	}
}

 

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值