读书人

动物猜想简化版,该如何解决

发布时间: 2012-03-25 20:55:16 作者: rapoo

动物猜想简化版
动物猜想简化版

尝试完成具有如下功能的程序。

举例说明:
程序提示你“你现在猜想的是一只狗吗?Y:N”。
如果你想到的是一只狗,程序会说“我赢了!”,结束。
如果你想的是一只鸽子,程序会说“我输了,请告诉我你想像的动物,并告诉我这个动物不同与以上猜测动物的特征!”,输入“鸽子,(为空代表从根插入),一种鸟类”,结束。
再次运行程序“你现在猜想的是一种鸟类吗?Y:N”。
否则提示“你现在猜想的是一只狗吗?”,是则提示“你现在猜想的是一只鸽子吗?Y:N”。
是则程序赢并结束。
如果不是程序则继续认输并提示你输入想像的动物。如输入“海鸥,一种鸟类,一种在海边生存的鸟类”,结束。
这样程序在最开始的时候只会猜狗这一种动物,然后会变的越来越聪明。

刚开始学习C++,这样的程序怎样做啊?求教。。。。。。。。

[解决办法]
还没仔细调试过,不知道有没有错:

C/C++ code
//#include "stdafx.h"#include <iostream>#include <string>using namespace std;struct node{public:    char type;      //  节点类型,0表示条件,1表示具体动物    string name,condition;  //  节点内容    node *yes,*no,*parent;};class thesaurus{private:    node *root;    node *current;public:    thesaurus();    string readCurrentName() {return current->name;};    string readCurrentCondition() {return current->condition;};    char readCurrentType() {return current->type;};    void move(int answer);      //  根据answer参数将current指针指向下一个位置。1表示用户选择了yes,0表示选择了no    void increase(const string name, const string condition);  //  增加新知识    void resetCurrent() {current=root->yes;};   //  复位当前位置指针};thesaurus::thesaurus(){    //  创建根节点    root=new node();    root->type=0;    root->name="";    root->condition="";    root->yes=NULL;    root->no=NULL;    root->parent=NULL;    //  初始化知识库    node *p=new node();    p->type=1;    p->name="狗";    p->condition="";    p->parent=root;    p->yes=NULL;    p->no=NULL;    root->yes=p;    current=p;}void thesaurus::move(int answer){               //  根据answer参数将current指针指向下一个位置。1表示用户选择了yes,0表示选择了no    if (current->type==0)    {        if (answer)            current=current->yes;        else            current=current->no;    }    return;}void thesaurus::increase(const string cName, const string cCondition){    //  创建新节点,保存新知识    node *p,*q;    p=new node();    q=new node();        p->type=0;    p->name="";    p->condition=*(new string(cCondition));    p->yes=q;    p->no=current;    p->parent=current->parent;    q->type=1;    q->name=*(new string(cName));    q->condition="";    q->yes=NULL;    q->no=NULL;    q->parent=p;    //  插入新节点    if (current->parent->yes==current)        current->parent->yes=p;    else        current->parent->no=p;    current->parent=p;}void main(){    thesaurus ts;    char ch;    string name,condition;    while (1)    {        ts.resetCurrent();        while (1)        {            if (ts.readCurrentType()==0)            {                cout<<"你现在猜想的是"<<ts.readCurrentCondition()<<"吗?(Y/N)";                cin>>ch;                ts.move(ch=='Y'||ch=='y');            }            else            {                cout<<"你现在猜想的是一只"<<ts.readCurrentName()<<"吗?(Y/N)";                cin>>ch;                if (ch=='Y'||ch=='y')                {                    printf("我赢了!\n");                    break;                }                else                {                    printf("我输了,请告诉我你想像的动物?");                    cin>>name;                    printf("请告诉我这个动物不同与以上猜测动物的特征!");                    cin>>condition;                    ts.increase(name,condition);                    break;                }            }        }    }    return;} 

读书人网 >C++

热点推荐