⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 动物识别系统.cpp

📁 动物识别系统 动物识别专家系统按下列规则
💻 CPP
字号:
#include"动物识别系统.h"
#include"fact.h"
#include"rule_database.h"
#include<iostream>
#include <fstream>
#include <string>
using namespace std;

int t=0;
int h;
string s[100];
int rule_num=0;
int fact_num=0;
extern rule_database rd;
extern fact f;

fact f_f=fact();
void pr_out(int i)//输出
{
    string s1;
	string s2;
    s2=s[i];
	int g=0;
    char * p;
	const char*q   =   s2.c_str();   
    p=strtok( (char *)q, ",") ;
    while (p ) 
   {
       s1=string(p);
       p = strtok(NULL, ",");
	   if(!p)
	       break;
	   if(g==0)
	   {
		   g=1;
		   cout<<"("<<s1<<")";
	   }
	   else
		   cout<<"  且  ("<<s1<<")";
   }
	cout<<"  -->  ("<<s1<<")"<<endl;
}
void matching()//匹配(正向推理)
{
	Node *p;
	Node *q;
	p=(Node *)malloc(sizeof(Node));
	q=(Node *)malloc(sizeof(Node));
	t=0;
	int k=0;
	for(int i=0;i<rd.sp;i++)
	{
		k=0;
		if(rd.tag[i]==1)
			continue;
		p=rd.rule[i]->next;
		while(p->next!=NULL)
		{
			q=f.fa->next;
			while(q->next!=NULL)
			{
				if(p->data==q->data)
				{
					k=1;
					break;
				}
				q=q->next;
			}
		    if(q->next==NULL)
		   {
				if(p->data==q->data)
				   k=1;
			    else
				   k=0;
		   }
           if(k==0)
			   break;
			p=p->next;
		}
	
		if(k==1)
		{
			f.Add(p);
			rd.tag[i]=1;
			cout<<"规则"<<i+1<<":"<<endl;
			pr_out(i);
			t=1;
		}
	}
    if(t==0)
		t=2;
}

void Add_fact(int a)//加入事实
{
    Node * p;
	p=(Node *)malloc(sizeof(Node));
	p->data=a;
	p->next=NULL;
	f.Add(p);
}

void Add_rule(int a[NUM])//加入规则
{
    Node * p;
	p=(Node *)malloc(sizeof(Node));
	p=rd.Add(a);
	rd.push(p);
}

int read(int a)//读文件,获取规则和事实
{
    ifstream file1; 
    int i=0;
	if(a==1)
	    file1.open("a.txt"); 
	if(a==2)
	    file1.open("b.txt"); 
    if (!file1.is_open ())
	{
        cout << "打开文件失败" << endl;
    }
    char c[100]; 
   
    while(!file1.eof())  
	{  
	  file1.getline(c,100,'\n');
	  s[i]=string(c);
	  i++;
	  
   }

   file1.close();
   return i;
}
int chang(string s_t)//编码算法
{
	ifstream file2;
	int i=1;
	char c[100];
	file2.open("c.txt");
    if (!file2.is_open ())
    {
       cout << "打开文件失败" << endl;
    }
	string s1;
	while(!file2.eof())  
	{  
		  file2.getline(c,100,'\n');
	      s1=string(c);
          if(!s_t.compare(s1))
			  break;
		  i++;
	}
	if(!file2.eof()) 
	{
		 file2.close();
		 return i;
	}
	else
	{
		 file2.close();
	     return -1;
	}
}

void rule_add()//将规则编码
{
	string s1;
	string s2;
    for(int j=0;j<rule_num;j++)
	{
		int i=0;
        int a[NUM]={0,0,0,0,0,0,0,0,0,0};
	    s2=s[j];
	    char * p;
	    const char*q   =   s2.c_str();   
        p=strtok( (char *)q, ",") ;
        while (p ) 
	   {
           s1=string(p);
           a[i]=chang(s1);
		   i++;
           p = strtok(NULL, ",");
       }
	   Add_rule(a);
	}
}

void fact_add()//将事实编码
{  
	int i=0;
    for(int j=0;j<fact_num;j++)
   {
      i=chang(s[j]);
	   Add_fact(i);
	}
}


int f_ma(Node *p)//与事实匹配(逆向推理)
{
	Node *q;
	q=(Node *)malloc(sizeof(Node));
	
	int k=0;
	q=f.fa->next;
	while(q->next!=NULL)
	{
		if(p->data==q->data)
		{
			k=1;
			break;
		}
		q=q->next;
	}
    if(k==1)
		return 1;
	return 0;
}

int r_ma(Node *p)//与规则匹配(逆向推理)
{
	Node * q;
	Node * k;
	Node * l;
	q=(Node *)malloc(sizeof(Node));
	
	l=(Node *)malloc(sizeof(Node));
    for(int i=0;i<rd.sp;i++)
	{
		if(rd.tag[i]==1)
			continue;
		q=rd.rule[i]->next;
		
		while(q->next!=NULL)
			q=q->next;
		if(q->data==p->data)
		{
			h=i;
			rd.tag[i]=1;
			q=rd.rule[i]->next;
			l->data=0;
			l->next=NULL;
			p=l;
			while(q->next!=NULL)
			{
			   k=(Node *)malloc(sizeof(Node));
			   k->data=q->data;
			   k->next=NULL;
			   p->next=k;
			   p=k;
			   q=q->next;
			}
               
			f_f.fa->next=l->next;
			cout<<"规则"<<i+1<<":"<<endl;
			pr_out(i);
			return 1;
		}
	}
	return 0;

}

void h_f()//将父节点放回目标链
{
	int k=0;
	Node * p;
	Node * q;
	p=(Node *)malloc(sizeof(Node));
	q=(Node *)malloc(sizeof(Node));
	p=f_f.fa->next;
	for(int i=0;i<rd.sp;i++)
	{
		q=rd.rule[i]->next;
		while(q->next->next!=NULL&&p->next!=NULL)
		{
			if(q->data!=p->data)
			{
				k=1;
				break;
			}
			p=p->next;
			q=q->next;
		}
		if(p->next==NULL)
			k=1;
		if(k==1)
           continue;
		q=rd.rule[i]->next;
		p=f_f.fa->next;
		while(q->next->next!=NULL)
		{
			q=q->next;
			p=p->next;
		}
		q->next->next=p->next;
		f_f.fa->next=q->next;

	}

}

void main()
{
	Node * p;
    int j;
	
	fact_num=read(2);
	fact_add();

	rule_num=read(1);
	rule_add();

    
	while(1)
	{
		int k=0;
        
	    p=(Node *)malloc(sizeof(Node));
		p=f.fa->next;
		while(p->next!=NULL)
		{
			if(p->data==3)
				k=1;
			p=p->next;
  		}
        if(p->next==NULL)
		{
			if(p->data==chang("偶蹄动物"))
				k=1;
		}
		if(t==2)
		{
			cout<<"正向推理失败";
			break;
		}
		if(k==1)
		{
			cout<<"正向推理成功"<<endl;
			break;
		}
		else
		   matching();
	}
    
	rd.sp=0;
	f.fa->data=0;
	f.fa->next=NULL;
	fact_num=read(2);
	fact_add();


	rule_num=read(1);
	rule_add();
    p=(Node *)malloc(sizeof(Node));
	
	p->data=chang("偶蹄动物");
	p->next=NULL;
	
	f_f.Add(p);
	int k=0;
    int b=0;
	while(f_f.fa->next!=NULL)
	{
		p=f_f.fa->next;
        k=f_ma(p);
		if(k==1)
		{
			f_f.fa->next=p->next;
			continue;
		}
		
		k=r_ma(p);
        if(k==1)
           continue;
		if(p->data==chang("偶蹄动物"))
		{
			cout<<"逆向推理失败"<<endl;
			b=1;
			break;
		}
	    h_f();          
	}
    if(b==0)
		cout<<"逆向推理成功"<<endl;
	cin>>j;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -