设为首页 加入收藏

TOP

C++ 如何使用时间函数(二)
2013-02-08 14:30:55 来源: 作者: 【 】 浏览:603
Tags:  如何 使用 时间 函数

 

  do

  {

  correct = true;         /* By default indicates correct sequence entered */

  counter = 0;            /* Initialize count of number of successful tries*/

  sequence_length = 2;    /* Initial length of a digit sequence            */

  time_taken = clock();  /* Record current time at start of game       */

  /* Inner loop continues as long as sequences are entered correctly */

  while(correct)

  {

  /* On every third successful try, increase the sequence length */

  sequence_length += counter++%3 == 0;

  /* Set seed to be the number of seconds since Jan 1,1970  */

  seed = time(NULL);

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

  /* Generate a sequence of numbers and display the number */

  srand((unsigned int)seed);      /* Initialize the random sequence */

  for(int i = 1; i <= sequence_length; i++)

  printf("%d ", rand() % 10);    /* Output a random digit          */

  /* Wait one second */

  for( ;clock() - now < CLOCKS_PER_SEC; );

  /* Now overwrite the digit sequence */

  printf("\r");                   /* go to beginning of the line */

  for(i = 1; i <= sequence_length; i++)

  printf("  ");                 /* Output two spaces */

  if(counter == 1)           /* Only output message for the first try */

  printf("\nNow you enter the sequence  - don't forget"

  " the spaces\n");

  else

  printf("\r");                /* Back to the beginning of the line */

  /* Check the input sequence of digits against the original */

  srand((unsigned int)seed);     /* Restart the random sequence    */

  for(i = 1; i <= sequence_length; i++)

  {

  scanf("%d", &number);         /* Read an input number         */

  if(number != rand() % 10)     /* Compare against random digit */

  {

  correct = false;            /* Incorrect entry             */

  break;                      /* No need to check further… */

  }

  }

  printf("%s\n", correct "Correct!" : "Wrong!");

  }

  /* Calculate total time to play the game in seconds)*/

  time_taken = (clock() - time_taken) / CLOCKS_PER_SEC;

  /* Output the game score */

  printf("\n\n Your score is %d", --counter * 100 / time_taken);

  fflush(stdin);

  /* Check if new game required*/

  printf("\nDo you want to play again (y/n)? ");

  scanf("%c", &another_game);

  }while(toupper(another_game) == 'Y');

  return 0;

  }

      

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C/C++ 数组的初始化 下一篇二维数组的建立与文件操作

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: