Advertisement

C语言打字游戏

阅读量:

1)输入过程中无法推出哦

2)请按所给字母敲击键盘哦

3)按任意键开始测试,按下首字母则开始记时

4)输入出错则以“_”表示

5)空格再玩一次,“ESC”退出

(学习内容)

复制代码
 #include <stdio.h>

    
 #include <conio.h>
    
 #include <time.h>
    
 #include <stdlib.h>
    
 #include <corecrt.h>
    
 void notice(void)
    
 {
    
     printf("*********************************************************\n");
    
     printf("    1)输入过程中无法推出哦\n");
    
     printf("    2)请按所给字母敲击键盘哦\n");
    
     printf("    3)按任意键开始测试,按下首字母则开始记时\n");
    
     printf("    4)输入出错则以“_”表示\n");
    
     printf("    5)空格再玩一次,“ESC”退出\n");
    
     printf("*********************************************************\n");
    
 }
    
 int main()
    
 {
    
     char ch;
    
     char str[51]="";//初始化,50个字母,'\0'
    
     int i;
    
     int count = 0;
    
     time_t start_time, end_time;
    
     while (1)//1:死循环
    
     {
    
     system("cls");//清屏
    
     notice();
    
     ch = _getch();//获取键盘的字符
    
     srand((unsigned)time(NULL));//获取秒数,同一秒产生的种子相同
    
     for (i = 0; i < 50; i++)//输出50个字母
    
     {
    
         str[i] = rand() % 26 + 'a';
    
     }
    
     str[50] = '\0';
    
     printf("%s\n", str);
    
     //获取键盘的字符
    
     count = 0;//count的值清零
    
     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("\n正确率为 %d %c\n", count*100/50,'%');
    
     printf("用时 %ld 秒\n", (long int)end_time - start_time);
    
     while (1)
    
     {
    
         ch = _getch();
    
         if (ch == ' ')
    
         {
    
             break;
    
         }
    
         if (ch == 27)//字符ESC
    
         {
    
             return 0;
    
         }
    
     }
    
     }
    
     return 0;
    
 }

全部评论 (0)

还没有任何评论哟~