#include "iostream.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"
typedef struct Rule//第一规则库
{
char Condition[6][20];
char Result[20];
int Cnum;
bool used;
}rule;
typedef struct Fact//定义事实库
{
char item[20][20];
int Fnum;
}fact;
rule r[100];
fact f;
void Create_Rules();//初始化规则库
void Create_Facts();//初始化事实库
int Get_RulesNum();
void Ratiocinate(int n);
void main()
{
int Rnum=0;
Create_Rules();
Create_Facts();
Rnum=Get_RulesNum();
//cout<<Rnum<<endl;
Ratiocinate(Rnum);
}
void Create_Rules()
{
strcpy(r[0].Condition[0],"产奶");
strcpy(r[0].Result,"哺乳动物");
r[0].Cnum=1;
r[0].used=false;
strcpy(r[1].Condition[0],"有毛发");
strcpy(r[1].Result,"哺乳动物");
r[1].Cnum=1;
r[1].used=false;
strcpy(r[2].Condition[0],"有羽毛");
strcpy(r[2].Result,"鸟");
r[2].Cnum=1;
r[2].used=false;
strcpy(r[3].Condition[0],"会飞");
strcpy(r[3].Condition[1],"生蛋");
strcpy(r[3].Result,"鸟");
r[3].Cnum=2;
r[3].used=false;
strcpy(r[4].Condition[0],"哺乳动物");
strcpy(r[4].Condition[1],"有抓");
strcpy(r[4].Condition[2],"有齿");
strcpy(r[4].Condition[3],"眼睛前视");
strcpy(r[4].Result,"食肉动物");
r[4].Cnum=4;
r[4].used=false;
strcpy(r[5].Condition[0],"哺乳动物");
strcpy(r[5].Condition[1],"吃肉");
strcpy(r[5].Result,"食肉动物");
r[5].Cnum=2;
r[5].used=false;
strcpy(r[6].Condition[0],"哺乳动物");
strcpy(r[6].Condition[1],"有蹄");
strcpy(r[6].Result,"有蹄动物");
r[6].Cnum=2;
r[6].used=false;
strcpy(r[7].Condition[0],"有蹄动物");
strcpy(r[7].Condition[1],"反刍");
strcpy(r[7].Result,"偶蹄动物");
r[7].Cnum=2;
r[7].used=false;
strcpy(r[8].Condition[0],"食肉动物");
strcpy(r[8].Condition[1],"黄褐色");
strcpy(r[8].Condition[2],"有黑色条纹");
strcpy(r[8].Result,"老虎");
r[8].Cnum=3;
r[8].used=false;
strcpy(r[9].Condition[0],"食肉动物");
strcpy(r[9].Condition[1],"黄褐色");
strcpy(r[9].Condition[2],"有黑色斑点");
strcpy(r[9].Result,"金钱豹");
r[9].Cnum=3;
r[9].used=false;
strcpy(r[10].Condition[0],"有蹄动物");
strcpy(r[10].Condition[1],"长腿");
strcpy(r[10].Condition[2],"长颈");
strcpy(r[10].Condition[3],"有暗斑点");
strcpy(r[10].Condition[4],"黄褐色");
strcpy(r[10].Result,"长颈鹿");
r[10].Cnum=5;
r[10].used=false;
strcpy(r[11].Condition[0],"有蹄动物");
strcpy(r[11].Condition[1],"白色");
strcpy(r[11].Condition[2],"有黑色条纹");
strcpy(r[11].Result,"斑马");
r[11].Cnum=3;
r[11].used=false;
strcpy(r[12].Condition[0],"鸟");
strcpy(r[12].Condition[1],"不会飞");
strcpy(r[12].Condition[2],"长腿");
strcpy(r[12].Condition[3],"长颈");
strcpy(r[12].Condition[4],"黑白色");
strcpy(r[12].Result,"鸵鸟");
r[12].Cnum=5;
r[12].used=false;
strcpy(r[13].Condition[0],"鸟");
strcpy(r[13].Condition[1],"不会飞");
strcpy(r[13].Condition[2],"会游泳");
strcpy(r[13].Condition[3],"黑白色");
strcpy(r[13].Result,"企鹅");
r[13].Cnum=4;
r[13].used=false;
strcpy(r[14].Condition[0],"鸟");
strcpy(r[14].Condition[1],"会飞");
strcpy(r[14].Condition[2],"不怕风浪");
strcpy(r[14].Result,"海燕");
r[14].Cnum=3;
r[14].used=false;
}
void Create_Facts()
{
strcpy(f.item[0],"有毛发");
strcpy(f.item[1],"有蹄");
strcpy(f.item[2],"长腿");
strcpy(f.item[3],"长颈");
strcpy(f.item[4],"有暗斑点");
strcpy(f.item[5],"黄褐色");
f.Fnum=6;
}
int Get_RulesNum()
{
int num=0;
int i=0;
while(1)
{
if(r[i].Condition[0][0]=='\0')
{
break;
}
i++;
num++;
}
return num;
}
void Ratiocinate(int n)
{
bool FindFact(char * str);
void InsertIntoFact(char * str);
int i=0,j;/// i用来控制查找的是第几条规则,j用来控制查找的规则中的第即条
while(1)
{
if(r[i].used==false)
{
for(j=0;j<r[i].Cnum;j++)
{
if(FindFact(r[i].Condition[j]))
{
continue;
}
else
{
cout<<"规则"<<i+1<<"匹配失败"<<endl;
break;
}
}
}
if(j==r[i].Cnum) ///如果所有的前件都满足
{
r[i].used=true;
cout<<"规则"<<i+1<<"匹配成功,结论插入到事实库"<<endl;
if(!FindFact(r[i].Result))
{
InsertIntoFact(r[i].Result);///插入事实
cout<<"新增加的事实为:"<<f.item[f.Fnum-1]<<endl;
}
i=0;
//continue;
}
else i++;//查看下条规则
if(i==n)
{
cout<<"没有你要找符合要求的动物,请增加新的规则"<<endl;
break;
}
else if(strcmp(f.item[f.Fnum-1],"老虎")==0||strcmp(f.item[f.Fnum-1],"金钱豹")==0||strcmp(f.item[f.Fnum-1],"长颈鹿")==0||strcmp(f.item[f.Fnum-1],"斑马")==0||strcmp(f.item[f.Fnum-1],"鸵鸟")==0||strcmp(f.item[f.Fnum-1],"企鹅")==0||strcmp(f.item[f.Fnum-1],"海燕")==0)
{
cout<<"与你给的事实匹配的动物:"<<f.item[f.Fnum-1]<<endl;
break;
}
}
}
//查看事实库中的事实否否等于前件,或者是存在这条事实,那么新的结论就不用加到事实库中
bool FindFact(char * str)
{
int i;
for(i=0;i<f.Fnum;i++)
{
if(strcmp(f.item[i],str)) continue;
else return true;
}
return false;
}
//插入新的事实
void InsertIntoFact(char * str)
{
strcpy(f.item[f.Fnum],str);
f.Fnum++;
}
非常简单的功能,主要是要体现出人工智能规则正向演绎系统简单演示和算法!