#include <iostream>
#include <set>
#include <vector>
using namespace std;
const char* animal[] = {"企鹅","信天翁","鸵鸟","斑马","长颈鹿","虎","金钱豹"};
const char* feature[]= {"有毛","产奶","有羽毛","会飞","会下蛋","吃肉","有犬齿","有爪","眼睛盯前方","有蹄","反刍","黄褐色","有斑点",
"有黑色条纹","长脖","长腿","不会飞","会游泳","黑白两色","善飞","哺乳类","鸟类","肉食类","蹄类",
"企鹅","信天翁","鸵鸟","斑马","长颈鹿","虎","金钱豹"
};
typedef struct {
int relation[5];
int name;
} Rule;
Rule rule[15]= {
{{0,-1},20} ,
{{1,-1},20},
{{2,-1},21},
{{3,4,-1},21},
{{20,5,-1},22},
{{6,7,8,-1},22},
{{20,9,-1},23},
{{20,10,-1},23},
{{20,22,11,12,-1},30},
{{20,22,11,13,-1},29},
{{23,14,15,12,-1},28},
{{23,13,-1},27},
{{21,14,15,16,-1},26},
{{21,17,18,16,-1},24},
{{21,19,-1},25}
};
int flag[23]= {0};
int IsAnimal(int a);
int inference();
void input();
void menu();
void menu() {
int i=0;
for(i=0; i<24; i++) {
if(i%4==0&&i!=0) {
cout<<endl;
}
printf("%-3d.%-15s",i,feature[i]);
}
}
void input() {
int ti=0;
for(int i=0; i<24; i++) {
flag[i]=0;
}
while(ti!=-1) {
cout<<"\n 输入选择(-1 结束):";
cin>> ti;
if(ti>=0&&ti<=23)
flag[ti]=1;
else if(ti!=-1) {
cout<<"输入错误!请输入 0~23 之间的数字!"<< endl;
cin.clear();
cin.sync();
}
}
}
int IsAnimal(int a) {
if(a>=24&&a<=30)
return 1;
else
return 0;
}
int inference() {
int ti;
int i,j;
int tres;
cout<<endl;
for(i=0; i<15; i++) {
j=0;
ti=rule[i].relation[j];
while(ti!=-1) {
if(flag[ti]==0)
break;
j++;
ti=rule[i].relation[j];
}
if(ti==-1) {
tres=rule[i].name;
flag[tres]=1;
printf("运用了规则%d : ",i+1);
j=0;
while(rule[i].relation[j]!=-1) {
cout<<feature[rule[i].relation[j]]<<" ";
j++;
}
cout<<"====> "<<feature[tres]<<endl;
if(IsAnimal(tres)) {
cout<<"该动物为:"<<feature[tres]<<endl;
return 1;
}
}
}
if(i==sizeof(rule)/sizeof(rule[0])) {
cout<<"没有这种动物,是否要追加条件(Y/N)?";
char q;
cin>>q;
if(q!='N') {
while(ti!=-1) {
cout<<"\n 输入选择(-1 结束):";
cin>> ti;
if(ti>=0&&ti<=23)
flag[ti]=1;
else if(ti!=-1) {
cout<<"输入错误!请输入 0~23 之间的数字!"<< endl;
cin.clear();
cin.sync();
}
}
inference();
}
}
return -1;
}
void backword() {
cout<<endl;
cout<<"输入事实数据库:"<<endl;
input();
cout<<"请选择动物种类编号"<<endl;
int ti=0;
int j;
int tres;
bool key=1;
cin>>ti;
if(ti>=24&&ti<=30) {
flag[ti]=1;
} else {
while(ti<24||ti>30) {
cout<<"无该动物种类,请重新输入"<<endl;
cin>>ti;
}
}
int *real;
for(int i=0; i<15; i++) {
if(rule[i].name==ti) {
j=0;
cout<<feature[ti]<<','<<"事实列表为:";
while(rule[i].relation[j]!=-1) {
cout<<feature[rule[i].relation[j]]<<" ";
j++;
}
cout<<endl;
real = rule[i].relation;
}
}
for(int i=0; i<8; i++) {
j=0;
ti=rule[i].relation[j];
while(ti!=-1) {
if(flag[ti]==0)
break;
j++;
ti=rule[i].relation[j];
}
if(ti==-1) {
tres=rule[i].name;
flag[tres]=1;
cout<<"添加事实数据库:“"<<feature[tres]<<"”"<<endl;
}
}
for(int i=0;i<sizeof(real)/sizeof(real[0]);i++){
if(flag[real[i]]==0){
key=0;
}
}
cout<<"事实数据库为:";
for(int i=0;i<24;i++){
if(flag[i]!=0){
cout<<feature[i]<<" ";
}
}
cout<<endl;
if(key==0)
cout<<"匹配失败"<<endl;
else
cout<<"匹配成功"<<endl;
}
int main() {
char q;int m=2;
while(q!='n') {
cout<<"请选择正向推理(0)或者逆向推理(1)"<<endl;
cin>>m;
if(m==0){
menu();
input();
inference();
}else{
menu();
cout<<endl;
for(int i=0;i<7;i++){
printf("%-3d.%-15s",i+24,animal[i]);
if(i==3)
cout<<endl;
}
backword();
}
cout<<"\n 继续?(Y/N)"<<endl;
cin>>q;
system("cls");
}
}