| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 782 人关注过本帖
标题:实在是不懂了,,指条道吧……
只看楼主 加入收藏
wzl963358694
Rank: 2
等 级:论坛游民
帖 子:35
专家分:17
注 册:2013-3-10
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:10 
实在是不懂了,,指条道吧……
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
程序代码:
/* Program 4.12 Simple Simon */
#include <stdio.h>                     /* For input and output   */
#include <ctype.h>                     /* For toupper() function */
#include <stdbool.h>                   /* For bool, true, false  */
#include <stdlib.h>                    /* For rand() and srand() */
#include <time.h>                      /* For time() and clock() */

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

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

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

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

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

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

  /* One outer loop iteration is one game */
  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(int 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(int 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;
}
2013-04-17 16:01
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:0 
sequence_length += counter++%3 == 0;
2013-04-17 16:21
Han_FlyB
Rank: 6Rank: 6
等 级:侠之大者
帖 子:143
专家分:424
注 册:2013-3-25
收藏
得分:1 
没仔细看,问题是不是出在这:
 sequence_length += counter++%3 == 0;
2013-04-17 16:23
wzl963358694
Rank: 2
等 级:论坛游民
帖 子:35
专家分:17
注 册:2013-3-10
收藏
得分:0 
回复 2楼 hahayezhe
给我详细讲讲吧、、
2013-04-17 16:26
wzl963358694
Rank: 2
等 级:论坛游民
帖 子:35
专家分:17
注 册:2013-3-10
收藏
得分:0 
回复 2楼 hahayezhe
我觉得这个函数的值仍然是2、、
2013-04-17 16:28
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:18 
sequence_length += counter++%3 == 0;
判断 counter % 3 == 0
sequence_length += 1 或者 sequence_length += 0
2013-04-17 16:33
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:0 
附带一句 先取模 再自加

bool _b = ((counter % 3) == 0);
sequence_length += _b;
counter ++;

这样更直观些

[ 本帖最后由 hahayezhe 于 2013-4-17 16:38 编辑 ]
2013-04-17 16:36
wzl963358694
Rank: 2
等 级:论坛游民
帖 子:35
专家分:17
注 册:2013-3-10
收藏
得分:0 
回复 7楼 hahayezhe
谢谢了,学习了
2013-04-17 16:43
wzl963358694
Rank: 2
等 级:论坛游民
帖 子:35
专家分:17
注 册:2013-3-10
收藏
得分:0 
回复 7楼 hahayezhe
在多一句、、
为什么这个地方不能“先自加,再取模”?
2013-04-17 16:47
wzl963358694
Rank: 2
等 级:论坛游民
帖 子:35
专家分:17
注 册:2013-3-10
收藏
得分:0 
回复 7楼 hahayezhe
那个我知道了、、、、总之还是谢谢你
2013-04-17 17:06
快速回复:实在是不懂了,,指条道吧……
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051392 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved