赞
踩
建立一个动物识别系统的规则库,编写程序用以识别虎、豹、斑马、长颈鹿、企鹅、鸵鸟、信天翁等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 动物是信天翁
#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;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。