📄 rules.cpp
字号:
/*-------------------------------------------------------------------*\
| 人工智能 专家系统示例 参见付 DOC文档 |
| Written by Redream <redlqs@163.com> |
| Copyright ? 2004 |
\*-------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <malloc.h>
struct LRules {
char *strIfRule ;
char *strThenRul ;
int intDTrust ;
int intFlagLive ;
struct LRules * next ;
} ;
struct SFact {
char strIfRule[10] ;
struct SFact *next ;
} ;
class CIntelligence
{
public :
int totalno ;
struct SFact *Factnew ;
struct SFact *Facthead ;
struct SFact *Facttmp ;
struct LRules * AllRulesHead ;
struct LRules * AllRulestmp ;
public :
void Initrules() ;
void Display() ;
void Deduce() ;
void Setq() ;
void Use_then() ;
void Destroy() ;
void Translate( const char * src, char * rst) ;
//void Setq2( char *facts , UINT Flag1) ;
int Remember(const char *partofrule ) ;
int Try_rule() ;
int Step_forward() ;
} ;
//// 初始化数值.
void CIntelligence::Initrules()
{
AllRulestmp = NULL ;
AllRulesHead = new LRules ; //15
AllRulesHead->strIfRule = "F20,M4," ;
AllRulesHead->strThenRul = "H7" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //14
AllRulesHead->strIfRule = "F17,F18,F19,M4," ;
AllRulesHead->strThenRul = "H6" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //13
AllRulesHead->strIfRule = "F14,F16,F17,F18,M4," ;
AllRulesHead->strThenRul = "H5" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //12
AllRulesHead->strIfRule = "F15,M3," ;
AllRulesHead->strThenRul = "H4" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //11
AllRulesHead->strIfRule = "F13,F14,F16,M3," ;
AllRulesHead->strThenRul = "H3" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //10
AllRulesHead->strIfRule = "F12,F15,M1,M2," ;
AllRulesHead->strThenRul = "H2" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //9
AllRulesHead->strIfRule = "F12,F13,M1,M2," ;
AllRulesHead->strThenRul = "H1" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //8
AllRulesHead->strIfRule = "F8,M1," ;
AllRulesHead->strThenRul = "M3" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //7
AllRulesHead->strIfRule = "F7,M1," ;
AllRulesHead->strThenRul = "M3" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //6
AllRulesHead->strIfRule = "F4,F5,F6," ;
AllRulesHead->strThenRul = "M2" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //5
AllRulesHead->strIfRule = "F3," ;
AllRulesHead->strThenRul = "M2" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //4
AllRulesHead->strIfRule = "F10,F11," ;
AllRulesHead->strThenRul = "M4" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //3
AllRulesHead->strIfRule = "F9," ;
AllRulesHead->strThenRul = "M4" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //2
AllRulesHead->strIfRule = "F2," ;
AllRulesHead->strThenRul = "M1" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
AllRulesHead = new LRules ; //1
AllRulesHead->strIfRule = "F1," ;
AllRulesHead->strThenRul = "M1" ;
AllRulesHead->next = AllRulestmp ;
AllRulestmp = AllRulesHead ;
while( AllRulestmp->next != NULL )
{
AllRulestmp->intDTrust=0 ;
AllRulestmp->intFlagLive=0 ;
AllRulestmp = AllRulestmp->next ;
}
AllRulestmp->intDTrust=0 ;
AllRulestmp->intFlagLive=0 ;
AllRulestmp = AllRulesHead ;
}
///////////////////////////////////////////////////////////////setq
void CIntelligence::Setq()
{
char *facts = new char[10] ;
char jing = '#' ;
cout << "input IfRule:" << "\n" ;
cin >>facts ;
//Facthead= ( struct SFact * )malloc( sizeof(struct SFact) ) ;
Facthead = new ( SFact ) ;
Facthead->next = NULL ;
strcpy( Facthead->strIfRule, facts) ;
Facttmp = Facthead ;
/**/
while ( facts[0]!=jing )
{
delete facts ;
facts = new char[10] ;
cin >>facts ;
if ( facts[0] == jing )
{
break ;
}
//if ( facts=="#" ) break ;
Factnew = new ( SFact ) ;
Factnew->next = NULL ;
strcpy( Factnew->strIfRule, facts ) ;
Facttmp->next = Factnew ;
Facttmp = Factnew ;
}
Facttmp = Facthead;
delete facts ;
facts = NULL ;
}
////////////////////////////////////////////////remember
int CIntelligence::Remember(const char *pPartOfRule )
{
Facttmp = Facthead ;
while (Facttmp->next){
if ( !strcmp( Facttmp->strIfRule, pPartOfRule) )
{//cout <<" have this " << partOfRule << "\n" ;
return 1 ;
}
else
{
/////////////////////
}
Facttmp = Facttmp->next;
}
if ( !strcmp( Facttmp->strIfRule, pPartOfRule) )
{//cout <<" have this " << partOfRule << "\n" ;
return 1 ;
}
else{
/////////////////////
}
return 0 ;
}
//////////////////////////////////////////////////////use then
void CIntelligence::Use_then()
{ /////cout <<Factnew->strIfRule << "\n";
Facttmp = Facthead;
while ( Facttmp->next != NULL ){
Facttmp = Facttmp->next ;
}
Factnew = new ( SFact ) ;
Factnew->next = NULL ;
strcpy( Factnew->strIfRule, AllRulestmp->strThenRul ) ;
cout << AllRulestmp->strThenRul << " Adding in Fact ! \n\n" ;
Facttmp->next = Factnew ;
//strcpy( Factnew->strIfRule, "#" ) ;
Facttmp = Facthead;
}
////////////////////////////////////////////// try_rule
int CIntelligence::Try_rule( )
{
char strpart[10] ;
int i, j, k ;
int begini = 0 ;
int endi = 0 ;
i = 0 ;
while ( i < int(strlen(AllRulestmp->strIfRule)) )
{
if (AllRulestmp->strIfRule[i] == ',')
{
endi = i-1 ;
k = 0 ;
for ( j = begini; j <= endi; j++ )
{
strpart[k] = AllRulestmp->strIfRule[j] ;
k++ ;
}
strpart[k] = '\0' ;
begini = i+1 ;
//cout << strpart << " --- strpart \n" ;
if ( !Remember( strpart ) ) return 0 ; // 前件在Fact里没找到
}
i++ ;
}
// 前件在Fact里找到 处理后件
if ( Remember( AllRulestmp->strThenRul ) ) { //找到
cout << "========= find out =========\n" ;
cout << "Ifrule (" << AllRulestmp->strIfRule <<") Find All in Fact, Thenrule ( " << AllRulestmp->strThenRul << " ) Addin Already \n\n" ;
return 1;
}
else{ //加入
cout << "========= find out ========\n" ;
cout << "Ifrule (" << AllRulestmp->strIfRule <<") Find All in Fact, Thenrule ( " << AllRulestmp->strThenRul << " ) To be Addin \n" ;
Use_then() ;
return 2 ;
}
}
////////////////////////////////////////// step forward
int CIntelligence::Step_forward()
{int tmptryrule ;
while (AllRulestmp->next){
tmptryrule = Try_rule() ;
if ( tmptryrule==2 ){
return 1 ;
}
if ( tmptryrule == 0 || tmptryrule == 1 ){
AllRulestmp = AllRulestmp->next;
totalno++ ;
cout << "Checking rule " << totalno << endl ;
}
}
tmptryrule = Try_rule() ;
if ( tmptryrule == 2 ){
return 1 ;
}
if ( tmptryrule == 0 || tmptryrule == 1 )
{
//AllRulestmp=AllRulestmp->next;
//totalno++ ;
//if ( AllRulestmp->next ) cout << "Checking rule " << totalno << endl ;
}
return 0 ;
}
///////////////////////////////////////////// deduce
void CIntelligence::Deduce()
{
totalno = 1 ;
AllRulestmp = AllRulesHead ;
cout << "Begin from Rule1 \n" ;
while ( Step_forward() ) {
if ( AllRulestmp->strThenRul[0]=='H' ) {
break ;
}
AllRulestmp = AllRulesHead ;
totalno = 1 ;
cout << "Return to Rule1 \n" ;
}
}
void CIntelligence::Translate( const char * src, char * rst )
{
if ( !strcmp(src,"H1") ) strcpy (rst,"It 's panther (Bao)") ;
if ( !strcmp(src,"H2") ) strcpy (rst,"It 's tiger (Hu)") ;
if ( !strcmp(src,"H3") ) strcpy (rst,"It 's giraffe (changjinglu)") ;
if ( !strcmp(src,"H4") ) strcpy (rst,"It 's zebra (Banma)") ;
if ( !strcmp(src,"H5") ) strcpy (rst,"It 's ostrich (Tuoniao)") ;
if ( !strcmp(src,"H6") ) strcpy (rst,"It 's penguin (Qi'e) ") ;
if ( !strcmp(src,"H7") ) strcpy (rst,"It 's quakerbird (Xintianweng)") ;
}
////////////////////////////////////////////////////////////////display
void CIntelligence::Display()
{
char * charTmp = new char[50] ;
while( Facthead->next )
{
//cout << Facthead->strIfRule << "\n" ;
//
if ( Facthead->strIfRule[0] == 'H' ) break ;
//
Facthead =Facthead->next;
}
if (Facthead->strIfRule[0] == 'H' ) {
cout <<"\n\nThe Fanal Resoult is : "<< Facthead->strIfRule << "\n\n" ;
Translate ( Facthead->strIfRule, charTmp ) ;
cout << charTmp <<"\n\n" ;
}
else {
cout <<"It have no RESOULT ! \n" << endl ;
}
delete charTmp ;
charTmp = NULL ;
}
///////////////////////////destroy pointers
void CIntelligence::Destroy()
{
///////////////delete about SFact
if ( ( Facthead == NULL ) || ( AllRulesHead == NULL ) ) return ;
Factnew = Facthead ;
Facttmp = Facthead ;
while ( Facttmp->next != NULL ){
Facthead = Facttmp ;
Facttmp = Facttmp->next ;
delete Facthead ;
Facthead = NULL ;
}
if ( Facttmp != NULL )
{
delete Facttmp ;
}
Facttmp = NULL ;
Factnew = NULL ;
///////////////delete about LRules
AllRulestmp = AllRulesHead ;
while ( AllRulestmp->next != NULL ){
AllRulesHead = AllRulestmp ;
AllRulestmp = AllRulestmp->next ;
delete AllRulesHead ;
AllRulesHead = NULL ;
}
if ( AllRulestmp != NULL )
{
delete AllRulestmp ;
}
AllRulestmp = NULL ;
AllRulesHead = NULL ;
}
////////////////////////////////////////////// main test
void main()
{
CIntelligence Asample ;
Asample.Initrules() ;
Asample.Setq() ;
Asample.Deduce() ;
Asample.Display() ;
Asample.Destroy() ;
Asample.Destroy() ;
/* END */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -