用C++实现一个简单的推理机—动物推断
// 简单推理机_动物推断,以鸡、猫、蛇和蜥蜴为例,构建一个简单的动物推断推理机
#include "stdafx.h"
#include <iostream>
#include <string>
#define Chicken 0x01 //鸡 0010
#define Cat 0x00 //猫 0000
#define Snake 0x03 //蛇 0001
#define Lizard 0x04 //蜥蜴 0101
using namespace std;
unsigned char result = 0;
typedef struct Rule{
char *info; //规则信息
Rule *pYes; //匹配到的下一个规则
Rule *pNo; //未匹配到的下一个规则
int pos; //该规则的位置
}Rule;
int CheckRule(char *s, char *info){
if(0 == strcmp(s, "Yes"))
return 1;
else
return 0;
}
//定义规则结构
void MatchRules(char *s, Rule *match){
if(!s || !match)
return;
printf("%s ?",match->info);
printf("\n");
char str[50] ;
std::cin>>str;
s=str;
//规则是否匹配上
if(CheckRule(s, match->info) > 0){
//记录位置
if(NULL==match->pYes)
{ result = match->pos;
return;
}
MatchRules(s, match->pYes);
}
else
{
//未匹配上
if(NULL