1、C编程_简单打字游戏测试

//程序来源:传智播客C语言老师的教程

//简单打字游戏:字母输入测试程式 

//有点问题按ESC无法退出循环
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include<conio.h>
//#include <mygetch.h>

#define MAX 51 //定义测试字母的最大长度

void help()
{
    printf("\n******************************************");
    printf("\n* 输入过程中无法退出,输入出错则以_表示!*");
    printf("\n* 按任意键开始测试,按下首字母时开始计时!*");
    printf("\n******************************************\n\n");
    
 } 

void start(char *str)
{
    srand((unsigned int)time(NULL));//随机种子 
    int i = 0;
    for(i = 0;i<MAX-1;i++)//打印50个英文字母符用于测试
    {
        *(str+i) = rand() % 26 +'a';
     } 
     *(str+MAX-1) = '\0';//写完字符后补上结束标志位 

 printf("\n%s\n",str);
}     
void on_game(char *str)
{
    
    char ch;
    int i =0;
    int count = 0;
    time_t t_start,t_end;
//    system("clear");//先清屏
    count = 0; 
    for(i = 0;i<MAX-1;i++)
    {
        //ch = mygetch();
        ch = getchar();
        //scanf("%c",p[i]);
        //fputs(p[i],stdin);
        //ch = getch();
        if( i ==0)
        {
            t_start = time(NULL);//取开始时间 
        }
        if(ch == *(str+i))
        //if(p[i] == *(str+i))
        {
            //printf("%s",ch);
            printf("%c",ch);
            count++;
        }
        else
        {
            printf("_");//打印出错标志 
        }
    }
    t_end = time(NULL);//取结束时间 
    
 
     
    printf("\n完成输入!\n用时 %ld s\n",t_end-t_start);
    printf("正确率%lf %%\n",(count*1.0/(MAX-1))*100);
    printf("按下Esc退出,任意键继续!\n\n");
    
}


int main()
{
    char str[MAX] = {0};
    while(1)
    {
        help();//游戏说明菜单
        start(str);//产生随机 字母
    //    mygetch();

   if(ch == ' ')//空格//ESC的ascii为27 //有点问题按ESC无法推出
        {
            break;
         } 
        on_game(str);//开始游戏
        //char ch = mygetch();
    char ch = getchar();
         //char ch = getch();
      
        //system("clear");
    }
    return 0;
}

//(如果侵权,联系删除)

转载于:https://my.oschina.net/u/3758137/blog/1596392

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值