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

📄 test.cpp

📁 识别动物的一专家系统,可以识别各种动物
💻 CPP
字号:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
#include<string.h>

#define NULL 0
char *str[]={0,"chew_cud","hooves","mammal","forward_eyes","claws",
    "pointed_teeth","eat_meal","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","not_fly","#"};
int rulep[][6]={{3,12,23,22,0,0},{3,12,23,21,0,0},{11,20,19,22,0,0},
         {11,21,0,0,0,0},{31,13,20,19,17,0},{31,13,18,17,0,0},{13,16,0,0,0,0},
         {15,0,0,0,0,0},{14,0,0,0,0,0},{10,0,0,0,0,0},{8,9,0,0,0,0},
         {7,0,0,0,0,0},{6,5,4,0,0,0},{3,2,0,0,0,0},{3,1,0,0,0,0}};
int rulec[]={30,29,28,27,26,25,24,3,3,13,13,12,12,11,11,0};

typedef struct fact
{
  int number;
  char name[20];
  int active;
  int succ;
  struct fact *next;
};
struct fact *fact_head=NULL;

typedef struct rule
{
char name[20];
int pre[6];
int conc;
struct rule *next;
};
struct rule *rule_head=NULL;

initfact()
{
	struct fact *f,*ftemp;
	int i=0;
	while(strcmp(*(str+i),"#")!=0)
	{
		f=(struct fact *)malloc(sizeof(struct fact));
		strcpy(f->name,*(str+i));
		f->number=i;
		f->active=0;
		f->succ=-1;
		f->next=fact_head;
		fact_head=f;
		i++;
	}
	f=fact_head;
	fact_head=NULL;
	while(f)
	{
		ftemp=f;
		f=f->next;
		ftemp->next=fact_head;
		fact_head=ftemp;
	}
}
print_fact()
{
	struct fact *f;
	f=fact_head;
	while(f!=NULL)
	{
		printf(" %s ",f->name);
		f=f->next;
	}
}

initrule()
{
	char ch[8];
	int i,j;
	struct rule *r;
	ch[0]='R';ch[1]='U',ch[2]='L',ch[3]='E',ch[4]='_',ch[5]='a',ch[6]='\0';
	for(i=0;i<15;i++)
	{
		r=(struct rule *)malloc(sizeof(struct rule));
		strcpy(r->name,ch);
		for(j=0;j<6;j++)
		{
			r->pre[j]=rulep[i][j];
			r->conc=rulec[i];
			r->next=rule_head;
			rule_head=r;
		}
		ch[5]++;
	}
}
print_rule()
{
	struct rule *r;
	int i;
	r=rule_head;
	while(r!=0)
	{
		for(i=0;i<6;i++)
		printf(" %d",r->pre[i]);
		r=r->next;
	}
}

query(struct rule *r)
{
	int i=0;
	char c;
	struct fact *f;
	f=fact_head;
	while(r->pre[i]!=NULL)
	{
		f=fact_head;
	while(f!=0)
	{ 
		if(r->pre[i]==f->number)
			break;
		f=f->next;
	}
	if(f==0)
    return 0;
	if(f->succ==1)
    {
		i++;
		continue;
    }
	if(f->succ==0)
		return 0;
		printf("%s(Y/N)\n",f->name);
		c=getch();
	if((c=='Y')||(c=='y'))
	{
		f->active=1;
		f->succ=1;
		i++;
		continue;
	}
	else
	{
		f->active=-1;
		f->succ=0;
		return 0;
	}
	}
	f=fact_head;
	for(;;)
	{
		if(r->conc==f->number)
			break;
			f=f->next;
	}
	if((r->conc)<23)
	{
		f->active=1;
		f->succ=1;
	return 0;
	}
	printf("\nthis animal is %s",f->name);
	return 1;
}

main()
{
	int i;
	struct rule *r;
	//clrscr();
	initfact();
	initrule();
	r=rule_head;
	while(r!=0)
	{
		i=query(r);
		if((i==1)||(i==-1))
			break;
		r=r->next;
	}
	if(!r)
	printf("\ni don't know!");
}

⌨️ 快捷键说明

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