>第 8 章 数组>多维数组

陈杨希 fusang12@126.com
2009-07-04 13:16:28

在“剪刀石头布”游戏中如果输入一个字母,会进入死循环。。


宋劲杉 songjinshan@akaedu.org
2009-07-09 08:54:53

多谢你发现这个问题!原因是scanf扫描的输入不匹配则不会读走,下次循环这个字母还在,还读不走,再下次循环这个字母还在……要避免这个问题目前没啥好办法,只能是出错就终止程序了。讲到后面可以先用fgets读上来再用sscanf扫描。


陈杨希 fusang12@126.com
2009-07-10 06:25:37

我是学软件测试的。。所以经常会搞些这种去试。
正常的应该是让你输入整数你就不应该输入字母或者其他字符,但软件应该有个容错功能。。我觉得可以把那个scanf("%d",&man)改成scanf("%c",&man)用它的asc码来判断。。内存出错我也用过一个fflush(stdin)来清空。。这个函数是怎么实现的我不知道,只知道他可以用。。班门弄斧了,见笑见笑。


宋劲杉 songjinshan@akaedu.org
2009-07-12 13:12:50

你的意见非常对,但是scanf("%c"...)实在不是好玩的,要费更多唇舌才能解释清楚。scanf在标准库一章会详细解释,在这一章为了避免跑题,还是用这个不怎么容错的办法吧。


陈杨希 fusang12@126.com
2009-07-14 19:06:54

今天又想到一种方法。。可以一试哦~这回不要用scanf("%c"...)。

flag=1;
while(flag){
fflush(stdin);
flag=0;
x=99999;//设置一不太可能用到的数
if(x==999999)
{
  flag=1
}
}


zhoudy dongyong800@163.com
2009-08-01 10:40:34

十分同意并欣赏:“数据代替了代码,数据驱动的编程(Data-driven Programming)"
“剪刀石头布”游戏的结果裁决也可以利用这个原理:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

    char game_type[3][8]={"scissor", "stone", "cloth"};
    char result_type[3][13]={"Draw", "Computer win", "Human win"};
    int computer_choice,human_choice;
    int ret,result;

    while(1){
        //computer choice
        computer_choice = rand()%3;
        //human choice
        do{
            printf("Please input your choice.0 for scissor, 1 for stone, 2 for cloth \n");
            ret = scanf("%d",&human_choice);
        }while(human_choice >2||human_choice<0);

        //Judgment of choise
        printf("Computer choose:%s\n",game_type[computer_choice]);
        printf("human choose:   %s\n",game_type[human_choice]);

        //Judgment of  result
        result= ((computer_choice - human_choice) + 3) % 3;
        printf("Game result:%s\n",result_type[result]);

    }

    return 0;
}



码匠 code_smith@sohu.com
2009-09-04 14:08:10

(man - computer + 4) % 3 - 1神奇

I know somebody see it as natural,
but for me, the expression makes me lunatic.

plus 4 bracket modulo 3 minus 1
  I collapse!

Who can talk about the secret of the statement?


lonny lonny_liu@hotmail.com
2009-09-18 09:54:06

我也不知道(man - computer + 4) % 3 - 1这个算式怎么想出来的?为什么会达到这个效果,宋老师,能否解答一下?谢谢!


宋劲杉 songjinshan@gmail.com
2009-09-24 22:29:16

这个其实没有多复杂。再仔细想想吧。把所有可能情况下每一步的计算结果都列出来就明白了。


860284226 860284226@qq.com
2010-02-05 19:47:21

我觉得例8.4的语句 printf("%s\n", days[day]);前面应加上else


宋劲杉 songjinshan@gmail.com
2010-02-27 21:59:49

你说得很对,多谢!在实体书第二次印刷时会改过来的


laciqs 530107999@qq.com
2010-04-27 20:37:01

860284226 860284226说的问题我竟然没注意到,汗死。。。。。。
恩,只是想提醒一下上面的几位朋友,fflush(stdin)结果是C标准未定义的,所以对程序的可移植性有要求时候要注意。


laciqs 530107999@qq.com
2010-04-27 20:39:15

对于这个问题,我常常使用
while (getchar() != '\n')
        continue;
来解决。


如果您有建设性意见,哪怕只是纠正一个错别字,也请不吝赐教,您留下的姓名和email将会出现在本书前言的致谢中。再次感谢您的宝贵意见!