当前位置:   article > 正文

人工智能实验:动物识别系统(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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180

运行效果

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/980731
推荐阅读
相关标签
  

闽ICP备14008679号