只以写明白为目的,注释有些多余,请高手多多指点我编码的毛病
解决方案 »
- win7下界面有误,但xp下显示正常
- Codejock.Xtreme.Toolkit.Pro.v11.20.Full.Source(源码版)
- 视频程序出了些问题,会的帮帮我吧,找了好些天找不到原因,55555
- 请问:我要在自己的程序里启动另一个软件B,如何可以知道B的版本?
- 怎样将char* 指向的字符串转换成 wchar* 字符串?
- 两主机间的Bitmap显示问题
- MouseButton類包含在哪個頭文件中?
- 数据库ODBC编程
- 救命啊!!!
- 点、线等可以用异或两次擦掉,那么文字怎么能擦掉呢?不能用InvalidateRect()一类的刷新方法!???紧急呼救!!
- 关于多线程处理---难道又是编译器问题?
- 大虾救命!!!如何向调制解调器发送at命令?
[email protected]
[email protected]
谢谢!
谢谢!!
[email protected]
[email protected]
[email protected]
#define ANIMAL_H
#define True 1
#define False 0
#define DontKnow -1#include "iostream.h"
#include "stdio.h"
#include "math.h"
#include "string.h"
//事实: cud:反刍 hoove膨胀 ungulate:有蹄 tawny:茶色 albatross:信天翁
char *str[]={"chew_cud","hooves","mamal","forward_eyes","claws",
"pointed_teeth","eat_meat","lay_eggs","fly","feathers","ungulate",
"carnivore","bird","give_milk","has_hair","fly_well",
"black&white_color","can_swim","long_legs","long_neck",
"black_stripes","dark_spots","tawny_color","albatross",
"penguin","ostrich","zebra","giraffe","tiger","cheetah",/*"\0"*//*""*/NULL};
//penguin:企鹅 ostrich:鸵鸟 zebra:斑马 giraffe:长颈鹿 cheetah:豹
//规则的数据(前提):以0表示规则的结束
int rulep[][6]={{22,23,12,3,0,0},{21,23,12,3,0,0},{22,19,20,11,0,0},
{21,11,0,0,0,0},{17,19,20,13,-9,0},{17,18,13,-9,0,0},{16,13,0,0,0,0},
{15,0,0,0,0,0},{14,0,0,0,0,0},{10,0,0,0,0,0},{8,7,0,0,0,0},
{7,0,0,0,0,0},{4,5,5,0,0,0},{2,3,0,0,0,0},{1,3,0,0,0,0}};
int rulec[]={30,29,28,27,26,25,24,3,3,13,13,12,12,11,11,0};//15条规则的结论
//前提链:
class list
{
private:
int Number;
public:
list * Next;
list(int Num)
{
Number=Num;
Next=NULL;
}
int GetNumber()
{
return Number;
}
};
class rule
{
char* Name;
list * Pre;//前提链表
int Conc;//结论
public:
rule * Next;//一条规则链组成一个知识库
rule(char *N, int P[], int C);//规则名,前提编号数组,结论编号
~rule();
int Query();//一条规则可有咨询推理功能
void GetName()
{
cout<<Name;
}
};//对于产生式系统,除了知识库外,还有个事实库记录用户与系统对话的结果及推理的中间结果。
//也就是表示当前环境。事实库是动态的,它最初包括原始的所有断言,但不知断言是真假。随着与用户对话,
//激活断言有的为真,也有的为假。同时规则库中的规则根据事实库的断言真假做出推理。
class fact
{
private:
int Number;
char Name[21];
int Active;
int Succ;
public:
fact *Next;
fact(int Num, char *L);
char * GetName();
int GetNumber()
{
return Number;
}
int GetAct()
{
return Active;
}
int GetSucc()
{
return Succ;
}
void PutAct(const int Act0,int Suc0)
{
Active=Act0;
Succ=Suc0;
}
};
fact *Fact;fact::fact(int Num,char *L)
{
strcpy(Name,L);
Number=Num;
Active=False;
Succ=DontKnow;
Next=NULL;
}
char * fact::GetName()
{
char *L;
L=new char[21];
strcpy(L,Name);
return L;
}
rule::rule(char *N, int P[], int C)//规则名,前提编号数组,结论编号
{
int i;
list *L;
Pre=NULL;
Next=NULL;
Name=new char[strlen(N)+1];
strcpy(Name,N);
i=0;
while(P[i]!=0)
{
L=new list(P[i++]);
L->Next=Pre;
Pre=L;
}
Conc=C;
}
rule::~rule()
{
list *L;
while(Pre)
{
L=Pre->Next;
delete Pre;
Pre=L;
}
delete Name;
}
int rule::Query()
{
// int i;
char c;
int Tag=0;
list *L;
fact *F;
F=Fact;
L=Pre;
if(L==NULL) cout<<"\nError";
while(L!=NULL)
{
F=Fact;
for(;;)//找前提链表中的每一个事实号是否存在,对每一rule,都有一Pre链表存各个前提的事实号
{
if(abs(L->GetNumber())==F->GetNumber())break;
F=F->Next;
} if(L->GetNumber()>0)//指向事实链表中对应于当前前提的fact对象(fact对象是事先系统设计好的)
{
if((F->GetSucc())==True){L=L->Next;continue;}//目前事实链表中的各个事实构造时均调用的是构造函数,置Succ为don't know.
if((F->GetSucc())==False) return False;//在当前信息中找不到此前提的答案,所以要向用户询问
}
else
{
if((F->GetSucc())==True) return False;
if((F->GetSucc())==False){L=L->Next;continue;}
}
cout<<endl<<F->GetName()<<"(Y/N)";
c=getchar();
flushall();
if((c=='Y')||(c=='y'))
{
if(L->GetNumber()>0)
F->PutAct(1,True);
if(L->GetNumber()<0)
{
F->PutAct(1,True);
Tag=-1;
return False;
}
}
else
{
if(L->GetNumber()<0)
F->PutAct(-1,False);
else
{
F->PutAct(-1,False);
Tag=-1;
return False;
}
}
L=L->Next;
}
F=Fact;
for(;;)
{
if(Conc==F->GetNumber())break;
F=F->Next;
}
if(Conc<24)
{
F->PutAct(1,True);
return False;
}
if(Tag!=-1)
{
F=Fact;
for(;;)
{
if(Conc==F->GetNumber())break;
F=F->Next;
}
if(Conc<24)
{
F->PutAct(1,True);
return False;
}
cout<<"\nThis animal is "<<F->GetName();
return True;
}
return False;
}
int main(int argc, char* argv[])
{
// list *List;
// fact *F,*T,*Fact;//比原码多加了Fact的定义
fact *F,*T;
rule *Rule,*R;
char ch[8];
int i=0;
Fact=NULL;
while(str[i])//激活事实对象集
{
F=new fact(i,str[i]);
i++;//原文为str[i++]难确定意思
F->Next=Fact;
Fact=F;
}
//形成一个30个事实的链表,F为表头,指向str[29]
F=Fact;//?可能没用
Fact=NULL;
while(F)
{
T=F;
F=F->Next;
T->Next=Fact;
Fact=T;
}//事实链表翻转,Fact和T此时作为表头指向str[0]
i=0;
ch[0]='R';ch[1]='U';ch[2]='L',ch[3]='E';
ch[4]='_';ch[5]='a';ch[6]='\0';
Rule=NULL;
for(i=0;i<15;i++)//激活规则对象集
{
R=new rule(ch, rulep[i],rulec[i]);
R->Next=Rule;
Rule=R;
ch[5]++;
}
R=Rule;//R 和 Rule现在是规则链表头
for(;;)//咨询
{
i=R->Query();
if((i==1)||(i==-1))break;
R=R->Next;
if(!R)break;
}
if(!R) cout<<endl<<"I don't know.";
cout<<endl<<"press any key to exit.";
getchar();
return True;}
#endif
知识获取:系统由15条规则组成,可以识别7种动物,共出现30个概念,我们把概念称作事实,共30个事实,每个事实一个编号,从1到30,规则对象中不存储事实概念,规则的结论也是事实的编号。事实与规则的数据以常量表示。可以改造或添加规则。例如可添加识别花,鸟等新动物的规则。
15条规则的中文表示是:
规则1:
如果:动物有毛发
则: 该动物是哺乳动物
规则2:
如果:动物有奶
则: 该动物是哺乳动物
...
...
Rule->{规则名,前提链,结论}--->{规则名,前提链,结论}--->
|
|
----->前提1-->前提2-->..->前提nNULLFact->{事实号,事实名,激活标志,断言,事实链建立知识库的同时也建立了事实库。事实库是一个动态链表,一个事实是链表的一个结点。知识库通过事实号与事实库发生联系。
规则名,前提链,结论------->事实Kn+1
|
|
前提1-->事实K1
|
|
前提2-->事实K2
|
|
:
:
前提n-->事实Kn
NULL前提链只有两个数据域:事实号 和 指向下一结点的指针推理机制:
将推理机制同规则对象封装在一起:
用户《----------》规则对象集---------》事实对象集
事实对象记录了当前的状态,规则对象首先拿出前提条件的断言(只有这些前提都有符合时才会做这条规则的结论),询问事实对象集,如事实对象集不知道,则询问用户,如所有前提条件都被证实为真则结论为真,否则系统不知道结论真假。
[email protected]
[email protected]
听起来挺难的!其实也没什么,就是几个树的基本算发在那里倒来倒去的。
好象很用到了些数据库,和scoket的东西。后来回头才发现,那根本就不是专家系统,顶多一个推理机。
thanks in advance
to:xiguazi() 1。做成windows程序
2。把事实数据保存到数据库里面
3。要实现规则的增加等修改功能
4。实现b/s吧