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

📄 zoo.cpp

📁 通过类型姓名年龄类别来显示动物
💻 CPP
字号:


//*********************************************
//* Zoo.cpp: implementation of the Zoo class. *
//*********************************************

#include "Zoo.h"
#include "Menu.h"

Zoo::Zoo()                              // Default constructor

{
	Begin = NULL;                       // Asign Begin to NULL
}

Zoo::~Zoo()                             // Destructor

{
	Animal * ptr;
	for ( ptr = Begin; Begin != NULL; ) // If Begin is not NULL

	{
		Begin = ptr->GetNext();         // Pointer pointing to GetNext func
		delete ptr;                     // Delete pointer
		ptr = Begin;
	}
}

bool_t Zoo::AddAnimal ( Animal * pAnim )// Add Animal func 
  
{
	pAnim->SetNext ( NULL );            // pointer pointing to SetNext(NULL)
	if ( Begin == NULL ) 

	{
		Begin = pAnim;
		return TRUE;
	}

	if ( Begin->GetCage() >= pAnim->GetCage() )

	{
		pAnim->SetNext ( Begin );      // Pointer pointing to GetCage
		Begin = pAnim;
		return TRUE;
	}

	Animal * prev = Begin, * ptr = Begin->GetNext();
    // Get the pointer via the cage number
	while ( ptr != NULL )

	{
		if ( ptr->GetCage() >= pAnim->GetCage() )

		{
			prev->SetNext( pAnim );
			pAnim->SetNext ( ptr );
			return TRUE;
		}

		prev = ptr;
		ptr = ptr->GetNext();
	}
	
	prev->SetNext( pAnim );
	return TRUE;
}

Animal * Zoo::RemoveAnimalViaNumber(int UniqueNumber, int ToCallDelete)

{
	// pointer pointing to current previeuse Animal equal to NULL
	Animal * cur, *prev = NULL;
	cur = Begin;
	while ( cur != NULL )

	{
		// Get the ponter via the uniquenumber
		if ( cur->GetUniqueNumber () ==	UniqueNumber )

		{
			if ( prev == NULL )
				Begin = cur->GetNext();
			else
				prev->SetNext(cur->GetNext());
			if ( ToCallDelete ) delete cur;
			return cur;
		}

		prev = cur;
		cur = cur->GetNext();
	}

	return NULL;
}

Animal * Zoo::FindAnimalViaNumber(int UniqueNumber)

{
	Animal * cur, *prev = NULL;
	cur = Begin;
	while ( cur != NULL )

	{
		// get the pointer to current Animal via uniqe number
		if ( cur->GetUniqueNumber () ==	UniqueNumber )
			return cur;
		prev = cur;
		cur = cur->GetNext();         // To point current to GetNext func
	}

	return NULL;
}


void Zoo::Display(int AnimalTypeID)

{
	Animal * cur;
	for ( cur = Begin; cur != NULL; cur = cur->GetNext() )

	{
		if ( AnimalTypeID == GENERIC_ANIMAL_ID ||
		    cur->GetAnimalTypeID() == AnimalTypeID )

		{
			cur->Display();        // Pointer to Display info function
		}

	}

    EatKeys();                    // Call EatKey to clean all the junk!!
	cout<<"Press ENTER To Continue...\n";
	while ( cin.get() != '\n' );
}

void Zoo::DisplayViaCage( int cage )

{
	Animal * cur;
	for ( cur = Begin; cur != NULL; cur = cur->GetNext() )

	{
		if ( cur->GetCage() == cage ) break;
	}

	// Give warning if the cage number have not the Animal requested
	if ( cur == NULL ) cout<<"No Animals Were Found For Cage :"<<cage<<"!\n";
	else

	{
	 for (; cur != NULL; cur = cur->GetNext() )

	 {
		 if ( cur->GetCage() != cage ) break;
		 cur->Display();
	 }

	}

    EatKeys();                 // Call EatKeys function to clean up the debris
	cout<<"Press ENTER To Continue...\n";
	while ( cin.get() != '\n' );
}

int Zoo::CountAnimals(int TypeID)

{
	int counter = 0;     // Set counter to zero
	Animal * cur;
	for ( cur = Begin; cur != NULL; cur = cur->GetNext() )
	                	// To count the current Animal in the Zoo
		if ( TypeID == GENERIC_ANIMAL_ID ||
		    cur->GetAnimalTypeID() == TypeID ) counter++;
	return counter;
}

//***********************************************************************

⌨️ 快捷键说明

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