#include
#include
#include
#include
using namespace std;
enum HAIR_COLOR
{
HAIR_BLACK,
HAIR_GRAY,
HAIR_BROWN,
};
enum EYE_COLOR
{
EYE_GREEN,
EYE_BLUE,
EYE_BROWN,
};
enum HAIR_TYPE
{
HAIR_SHORT,
HAIR_LONG,
};
class Cat
{
public:
Cat();
virtual ~Cat();
inline HAIR_COLOR GetHairColor()const { return m_hairColor; }
inline EYE_COLOR GetEyeColor()const { return m_eyeColor; }
inline HAIR_TYPE GetHairType()const { return m_hairType; }
inline void SetLabel( string strLabel ){ m_strLabel = strLabel; }
inline string GetLabel()const { return m_strLabel; }
inline string GetHairColorName(){ return m_strHairColor; }
inline string GetHairTypeName(){ return m_strHairType; }
inline string GetEyeColorName(){ return m_strEyeColor; }
private:
void _SetHairColor( const HAIR_COLOR hairColor );
void _SetEyeColor( const EYE_COLOR hairColor );
void _SetHairType( const HAIR_TYPE hairType );
private:
HAIR_COLOR m_hairColor;
EYE_COLOR m_eyeColor;
HAIR_TYPE m_hairType;
string m_strLabel;
string m_strHairColor;
string m_strHairType;
string m_strEyeColor;
};
Cat::Cat()
{
m_strLabel = "";
_SetHairColor( ( HAIR_COLOR )( rand() % 3 ) );
_SetEyeColor( ( EYE_COLOR )( rand() % 3 ) );
_SetHairType( ( HAIR_TYPE )( rand() % 2 ) );
}
Cat::~Cat()
{
}
void Cat::_SetHairColor( const HAIR_COLOR hairColor )
{
m_hairColor = hairColor;
switch( hairColor )
{
case HAIR_BLACK:
m_strHairColor = "黑毛";
break;
case HAIR_BROWN:
m_strHairColor = "棕毛";
break;
case HAIR_GRAY:
m_strHairColor = "灰毛";
break;
}
}
void Cat::_SetEyeColor( const EYE_COLOR eyeColor )
{
m_eyeColor = eyeColor;
switch( eyeColor )
{
case EYE_BLUE:
m_strEyeColor = "蓝色眼睛";
break;
case EYE_BROWN:
m_strEyeColor = "棕色眼睛";
break;
case EYE_GREEN:
m_strEyeColor = "绿色眼睛";
break;
}
}
void Cat::_SetHairType( const HAIR_TYPE hairType )
{
m_hairType = hairType;
switch( hairType )
{
case HAIR_SHORT:
m_strHairType = "短毛";
break;
case HAIR_LONG:
m_strHairType = "长毛";
break;
}
}
bool CheckCatEyeColor( vector< Cat >catContainer )
{
int iEyeBlueCount = 0, iEyeBrownCount = 0, iEyeGreenCount = 0;
while( !catContainer.empty() )
{
switch( catContainer.back().GetEyeColor() )
{
case EYE_BLUE:
iEyeBlueCount++;
break;
case EYE_BROWN:
iEyeBrownCount++;
break;
case EYE_GREEN:
iEyeGreenCount++;
break;
}
catContainer.pop_back();
}
if( iEyeBlueCount != 0 && iEyeBrownCount != 0 && iEyeGreenCount != 0 )
{
return true;
}
else
{
return false;
}
}
int CheckCatContainerEmpty( vector< Cat >cat, ... )
{
int iNonEmptyCount = 0;
va_list parameters;
va_start( parameters, cat );
vector< Cat > prevItem = cat;
while( prevItem.back().GetLabel() != "Non" )
{
if( !prevItem.empty() )
{
iNonEmptyCount++;
}
prevItem = va_arg( parameters, vector< Cat > );
}
va_end( parameters );
return iNonEmptyCount;
}
bool CheckCatMutualAttack( vector< Cat >cat1, vector< Cat >cat2, vector< Cat >cat3 )
{
bool bRet = false;
Cat non;
non.SetLabel( "Non" );
vector< Cat >nonContainer;
nonContainer.push_back( non ) ;
int iNonEmptyCount = CheckCatContainerEmpty( cat1, cat2, cat3, nonContainer );
if( iNonEmptyCount == 3 )
{
if( cat1.size() != cat2.size() || cat1.size() != cat3.size() || cat2.size() != cat3.size() )
{
bRet = true;
}
else
{
if( CheckCatEyeColor( cat1 ) )
{
bRet = true;
}
else if( CheckCatEyeColor( cat2 ) )
{
bRet = true;
}
else if( CheckCatEyeColor( cat3 ) )
{
bRet = true;
}
}
}
else if( iNonEmptyCount == 2 )
{
if( cat1.empty() )
{
if( cat2.size() != cat3.size() )
{
bRet= true;
}
else
{
if( CheckCatEyeColor( cat2 ) )
{
bRet= true;
}
else if( CheckCatEyeColor( cat3 ) )
{
bRet= true;
}
}
}
else if( cat2.empty() )
{
if( cat1.size() != cat3.size() )
{
bRet= true;
}
else
{
if( CheckCatEyeColor( cat1 ) )
{
bRet= true;
}
else if( CheckCatEyeColor( cat3 ) )
{
bRet= true;
}
}
}
else if( cat3.empty() )
{
if( cat2.size() != cat1.size() )
{
bRet= true;
}
else
{
if( CheckCatEyeColor( cat2 ) )
{
bRet= true;
}
else if( CheckCatEyeColor( cat1 ) )
{
bRet= true;
}
}
}
}
else
{
if( cat1.empty() && cat2.empty() )
{
if( CheckCatEyeColor( cat3 ) )
{
bRet = true;
}
}
else if( cat2.empty() && cat3.empty() )
{
if( CheckCatEyeColor( cat1 ) )
{
bRet = true;
}
}
else if( cat1.empty() && cat3.empty() )
{
if( CheckCatEyeColor( cat2 ) )
{
bRet= true;
}
}
}
return bRet;
}
void main()
{
vector< Cat > catContainer, blackCatContainer, grayCatContainer, brownContainer;
for( int i = 0; i < 7; i++ )
{
Cat cat;
switch( cat.GetHairColor() )
{
case HAIR_BLACK:
blackCatContainer.push_back( cat );
break;
case HAIR_GRAY:
grayCatContainer.push_back( cat );
break;
case HAIR_BROWN:
brownContainer.push_back( cat );
break;
}
catContainer.push_back( cat );
}
string strMutualAttack;
if( CheckCatMutualAttack( blackCatContainer, grayCatContainer, brownContainer ) )
{
strMutualAttack = "是";
}
else
{
strMutualAttack = "不是";
}
for( int i = 0; i < 7; i++ )
{
cout << "猫ID:" << i << "\t"<< "毛色:" << catContainer[ i ].GetHairColorName().c_str() << "\t" << "毛类型:" << catContainer[ i ].GetHairTypeName().c_str() << "\t" << "眼睛颜色:" << catContainer[ i ].GetEyeColorName().c_str() << endl;
}
cout << "是否相互攻击:" << strMutualAttack.c_str() <<endl;
system( "pause" );
}
不知道这个程序运行后有没有达到你的作业要求,你拿去运行试试结果,还有疑问请私密我。