美文网首页
C语言打字游戏源码

C语言打字游戏源码

作者: 玉宾 | 来源:发表于2020-08-21 20:34 被阅读0次

    到大街上,还是会羡慕那些情侣,但是依然相信舔狗一无所有,渣男满载而归。。。

    ----  网易云热评

    #include <stdio.h>

    #include <time.h>

    #include <stdlib.h>

    #include <string.h>

    #define MAX 51 //第一测试字母的最大长度

    void help()

    {

           printf("\n************************");

           printf("\n* 请快速输入上面的字母 *");

           printf("\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';  //随机获取一个0-25的数字并于‘a’相加,从而获取一个任意字符

           }

           *(str + MAX - 1) = '\0';//给字符串最后加上结束符

    }

    void on_game(char *str)

    {

           char ch;

           int i = 0;

           int count = 0;

           time_t t_start, t_end; //开始时间和结束时间

           printf("\n%s\n", str);

           count = 0;

           for (i=0;i<MAX-1;i++)

           {

                  ch = getch();

                  if (i==0)

                  {

                         t_start = time(NULL);

                  }

                  if (ch==*(str+i))

                  {

                         printf("%c",ch);//输入正确,打印出字符

                         count++;

                  }

                  else

                  {

                         printf("*");//输入错误,打印*

                  }

           }

           t_end = time(NULL);//取结束时间

           printf("\n用时%d 秒\n",t_end-t_start);

           printf("正确率%lf %%\n", (count * 1.0 / (MAX - 1)) * 100);

           printf("按下q键退出");

    }

    int main() {

           char str[MAX] = { 0 };

           while (1)

           {

                  help();//弹出开始内容

                  start(str);//生成随机字母

                  on_game(str);//开始游戏

                  char ch = getch();

                  if (ch == 'q') //按下esc键退出

                  {

                         break;

                  }

                  system("cls");

           }

           system("pause");

           return 0;

    }

    1、getch(), 用户按下某个字符时,函数自动读取,无需按回车

    2、gechar(),用户按下某个字符时,函数自动读取,需要按回车

    3、%%,两个%在格式化字符串中输出一个

    4、rand(),获取一个随机数,最大值为32767

    5、 count * 1.0,将count变成小数

    6、system("cls"),清屏操作

    运行结果:

    欢迎关注公众号:顺便编点程

    相关文章

      网友评论

          本文标题:C语言打字游戏源码

          本文链接:https://www.haomeiwen.com/subject/rqppjktx.html