鱼C论坛

 找回密码
 立即注册
查看: 1950|回复: 8

Linux C语言

[复制链接]
发表于 2015-1-19 20:24:00 | 显示全部楼层 |阅读模式
8鱼币
有个源代码,用途是在屏幕上显示一串数字几秒种,然后由用户凭记忆把它打出来
在windows下能用gcc(带-std=c99)编译成功并正常运行,在linux下也编译正常,但不能显示机器给出的数字
  1. /* Program 4.12 Simple Simon */
  2. #include <stdio.h>                     /* For input and output   */
  3. #include <ctype.h>                     /* For toupper() function */
  4. #include <stdbool.h>                   /* For bool, true, false  */
  5. #include <stdlib.h>                    /* For rand() and srand() */
  6. #include <time.h>                      /* For time() and clock() */

  7. int main(void)
  8. {
  9.   /* Records if another game is to be played */
  10.   char another_game = 'Y';

  11.   /* true if correct sequence entered, false otherwise */
  12.   int correct = false;

  13.   /* Number of sequences entered successfully          */
  14.   int counter = 0;

  15.   int sequence_length = 0;     /* Number of digits in a sequence        */
  16.   time_t seed = 0;             /* Seed value for random number sequence */
  17.   int number = 0;              /* Stores an input digit                 */

  18.   time_t now = 0;            /* Stores current time - seed for random values  */
  19.   int time_taken = 0;        /* Time taken for game in seconds                */

  20.   /* Describe how the game is played */
  21.   printf("\nTo play Simple Simon, ");
  22.   printf("watch the screen for a sequence of digits.");
  23.   printf("\nWatch carefully, as the digits are only displayed"
  24.                                                 " for a second! ");
  25.   printf("\nThe computer will remove them, and then prompt you ");
  26.   printf("to enter the same sequence.");
  27.   printf("\nWhen you do, you must put spaces between the digits. \n");
  28.   printf("\nGood Luck!\nPress Enter to play\n");
  29.   scanf("%c", &another_game);

  30.   /* One outer loop iteration is one game */
  31.   do
  32.   {
  33.     correct = true;         /* By default indicates correct sequence entered */
  34.     counter = 0;            /* Initialize count of number of successful tries*/
  35.     sequence_length = 2;    /* Initial length of a digit sequence            */
  36.     time_taken = clock();  /* Record current time at start of game       */

  37.     /* Inner loop continues as long as sequences are entered correctly */
  38.     while(correct)
  39.     {
  40.       /* On every third successful try, increase the sequence length */
  41.       sequence_length += counter++%3 == 0;

  42.       /* Set seed to be the number of seconds since Jan 1,1970  */
  43.       seed = time(NULL);

  44.       now = clock();                  /* record start time for sequence  */

  45.       /* Generate a sequence of numbers and display the number */
  46.       srand((unsigned int)seed);      /* Initialize the random sequence */
  47.       for(int i = 1; i <= sequence_length; i++)
  48.         printf("%d ", rand() % 10);    /* Output a random digit          */

  49.       /* Wait one second */
  50.       for( ;clock() - now < CLOCKS_PER_SEC; );

  51.       /* Now overwrite the digit sequence */
  52.       printf("\r");                   /* go to beginning of the line */
  53.       for(int i = 1; i <= sequence_length; i++)
  54.         printf("  ");                 /* Output two spaces */

  55.       if(counter == 1)           /* Only output message for the first try */
  56.         printf("\nNow you enter the sequence  - don't forget"
  57.                                                " the spaces\n");
  58.       else
  59.         printf("\r");                /* Back to the beginning of the line */

  60.       /* Check the input sequence of digits against the original */
  61.       srand((unsigned int)seed);     /* Restart the random sequence    */
  62.       for(int i = 1; i <= sequence_length; i++)
  63.       {
  64.         scanf("%d", &number);         /* Read an input number         */
  65.         if(number != rand() % 10)     /* Compare against random digit */
  66.         {
  67.           correct = false;            /* Incorrect entry             */
  68.           break;                      /* No need to check further... */
  69.         }
  70.       }
  71.       printf("%s\n", correct? "Correct!" : "Wrong!");
  72.     }

  73.     /* Calculate total time to play the game in seconds)*/
  74.     time_taken = (clock() - time_taken) / CLOCKS_PER_SEC;

  75.     /* Output the game score */
  76.     printf("\n\n Your score is %d", --counter * 100 / time_taken);

  77.     /*fflush(stdin);*/
  78.     getchar();

  79.     /* Check if new game required*/
  80.     printf("\nDo you want to play again (y/n)? ");
  81.     scanf("%c", &another_game);

  82.   }while(toupper(another_game) == 'Y');
  83.   return 0;
  84. }
复制代码




小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-1-19 20:50:51 | 显示全部楼层
好长啊!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-1-19 22:11:23 | 显示全部楼层

从第27到34行是一连串的printf();语句,所以出问题的几率不大。。ps:而且书上本来就是这么长
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-1-20 15:35:29 | 显示全部楼层
这个,好抽象的问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-1-20 16:41:48 | 显示全部楼层
顶顶~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-1-21 14:42:51 | 显示全部楼层
都是大神呀
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-2-8 12:17:24 | 显示全部楼层
zhao zhe shang bian yun xing le ,bu zuo biao shi .:call:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-2-10 13:01:23 | 显示全部楼层
真是每一行都注释了!好习惯:lol:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-2-10 23:24:50 | 显示全部楼层
linux,没用过:sweat::sweat:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 22:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表