人工智能实验:动物识别系统(C++代码实现)

问题描述

建立一个动物识别系统的规则库,编写程序用以识别虎、豹、斑马、长颈鹿、企鹅、鸵鸟、信天翁等7种动物。

为了识别这些动物,可以根据动物识别的特征,建立包含下述规则库:

R1:if 动物有毛发 then 动物是哺乳动物

R2:if 动物有奶 then 动物是哺乳动物

R3:if 动物有羽毛 then 动物是鸟

R4:if 动物会飞 and 会生蛋 then 动物是鸟

R5:if 动物吃肉 then 动物是食肉动物

R6:if 动物有犀利牙齿 and 有爪 and 眼向前方 then 动物是食肉动物

R7:if 动物是哺乳动物and有蹄then动物是有蹄类动物

R8:if 动物是哺乳动物and反刍then动物是有蹄类动物

R9:if 动物是哺乳动物and是食肉动物and有黄褐色 and 有暗斑点 then 动物是豹

R10:if 动物是哺乳动物 and是食肉动物and有黄褐色 and有黑色条纹 then 动物是虎

R11:if动物是有蹄类动物 and 有长脖子and有长腿and有暗斑点 then 动物是长颈鹿

R12:if 动物是有蹄类动物 and有黑色条纹 then 动物是斑马

R13:if 动物是鸟and不会飞 and有长脖子and有长腿 and有黑白二色 then 动物是鸵鸟

R14:if 动物是鸟 and不会飞 and会游泳 and有黑白二色 then 动物是企鹅

R15:if 动物是鸟 and善飞 then 动物是信天翁

C++代码

#include<iostream>
#include<iomanip>
using namespace std;

//使用常引用进行传参以提高程序效率
void Animal_Judge(short* Features, const short& FeatureCount)
{
	//使用初始化列表对变量初始化以提高效率
	bool isMammal(false);
	bool isBird(false);
	bool isCarnivore(false); //是食肉动物
	bool isUngulates(false); //是有蹄类动物

	for (int i(0); i < FeatureCount; ++i)
	{
		switch (Features[i])
		{
		case 1: {isMammal = true; break; }
		case 2: {isMammal = true; break; }
		case 3: {isBird = true; break; }
		case 4:
		{
			if (i < FeatureCount - 1)
			{
				if (Features[i + 1] == 5)
				{
					isBird == true;
				}
			}
			break;
		}
		case 5:break;
		case 6: {isCarnivore = true; break; }
		case 7:
		{
			if (i < FeatureCount - 2)
			{
				if (Features[i + 1] == 8 && Features[i + 2] == 9)
				{
					isCarnivore = true;
				}
			}
			break;
		}
		case 8:break;
		case 9:break;
		case 10:
		{
			if (isMammal == true)
			{
				isUngulates = true;
			}
			break;
		}
		case 11:
		{
			if (isMammal == true)
			{
				isUngulates = true;
			}
			break;
		}
		case 12:
		{
			if (isMammal == true && isCarnivore == true)
			{
				if (i < FeatureCount - 1)
				{
					if (Features[i + 1] == 13)
					{
						cout << "经过检验,该动物是豹。" << endl;
						return;
					}
					else if (Features[i + 1] == 14)
					{
						cout << "经过检验,该动物是虎。" << endl;
						return;
					}
				}
			}
			break;
		}
		case 13:
		{
			if (isUngulates == true)
			{
				if (i < FeatureCount - 2)
				{
					if (Features[i + 1] == 15 && Features[i + 2] == 16)
					{
						cout << "经过检验,该动物是长颈鹿。" << endl;
						return;
					}
				}
			}
			break;
		}
		case 14:
		{
			if (isUngulates == true)
			{
				cout << "经过检验,该动物为斑马。" << endl;
				return;
			}
			break;
		}
		case 15:break;
		case 16:break;
		case 17:
		{
			if (isBird == true)
			{
				if (i > 1 && i < FeatureCount - 1)
				{
					if (Features[i - 2] == 15 && Features[i - 1] == 16 && Features[i + 1] == 18)
					{
						cout << "经过检验,该动物为鸵鸟。" << endl;
						return;
					}
				}
				else if (i < FeatureCount - 2)
				{
					if (Features[i + 1] == 18 && Features[i + 2] == 19)
					{
						cout << "经过检验,该动物为企鹅。" << endl;
						return;
					}
				}
			}
			break;
		}
		case 18:break;
		case 19:break;
		case 20:
		{
			if (isBird == true)
			{
				cout << "经过检验,该动物为信天翁。" << endl;
				return;
			}
		}
		
		}
	}
	cout << "根据您输入的特征没有找到匹配的动物哦!" << endl;
	return;
}

int main(void)
{
	const string Features[20] = { "1.有毛发","2.有奶","3.有羽毛","4.会飞","5.会生蛋","6.吃肉","7.有犀利牙齿","8.有爪","9.眼向前方","10.有蹄","11.反刍","12.有黄褐色","13.有暗斑点","14.有黑色条纹","15.有长脖子","16.有长腿","17.不会飞","18.有黑白两色","19.会游泳","20.善飞" };
	cout << "以下是动物的基础特征:" << endl;
	for (int i(0); i < 20; ++i)
	{
		if (i % 5 == 0 && i != 0)
		{
			cout << "\n";
		}
		cout << setiosflags(ios::left);
		cout << setw(15) << Features[i];
	}
	cout << endl << endl;
	loop:
	cout << "请输入待标识动物所满足的编号个数(输入-1退出程序):";
	short FeatureCount;
	cin >> FeatureCount;
	if (FeatureCount != -1)
	{
		short* FeatureIncluded = new short[FeatureCount];
		cout << "请按升序逐一输入待标识动物所满足的编号:";
		for (int i(0); i < FeatureCount; ++i)
		{
			cin >> FeatureIncluded[i];
		}
		Animal_Judge(FeatureIncluded, FeatureCount);
		cout << endl;
		goto loop;
	}
	return 0;
}

运行效果

在这里插入图片描述
在这里插入图片描述

  • 37
    点赞
  • 219
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值