数组程序实例:第三次迭代

#pragma once
#ifndef IWR_ARRAY_MANAGER_H_INCLUDE__
#define IWR_ARRAY_MANAGER_H_INCLUDE__

#include <assert.h>
#include "Thing.h"

template<typename A, typename B>
class CArrayManager
{
public:
	CArrayManager(int nMaxElements);
	~CArrayManager(void);
public:
	bool AddElememt(A* newValue);
	bool GetElement(int position, A*& value);
	bool DeleteElement(int position);
	bool FindElement(B key, int& position);
	int GetSize();
private:
	int m_nMaxElements;
	int m_nTotalElements;
	A** m_pArray;
};

template<typename A, typename B>
CArrayManager<A, B>::CArrayManager(int nMaxElements)
{
	assert(nMaxElements > 0);
	m_nMaxElements = nMaxElements;
	m_nTotalElements = 0;
	m_pArray = new A*[m_nMaxElements];
}

template<typename A, typename B>
CArrayManager<A, B>::~CArrayManager(void)
{
	for (int i = 0; i < m_nTotalElements; ++i)
	{
		delete m_pArray[i];
	}
	delete [] m_pArray;
}

template<typename A, typename B>
bool CArrayManager<A, B>::AddElememt(A* newValue)
{
	if (newValue == NULL)	return false;
	if (m_nTotalElements < m_nMaxElements)
	{
		m_pArray[m_nTotalElements++] = newValue;
		return true;
	}
	return false;
}

template<typename A, typename B>
bool CArrayManager<A, B>::GetElement(int position, A*& value)
{
	if (position >= 0 && position < m_nTotalElements)
	{
		value = m_pArray[position];
		return true;
	}
	return false;
}

template<typename A, typename B>
bool CArrayManager<A, B>::DeleteElement(int position)
{
	if (position >= 0 && position < m_nTotalElements)
	{
		CThing* pThing = m_pArray[position];
		for (int i = position; i < m_nTotalElements - 1; ++i)
			m_pArray[i] = m_pArray[i + 1];

		delete pThing;
		--m_nTotalElements;
		return true;
	}
	return false;
}

template<typename A, typename B>
bool CArrayManager<A, B>::FindElement(B key, int& position)
{
	for (int ii = 0; ii < m_nTotalElements; ++ii)
	{
		if (key == m_pArray[ii]->GetKey())
		{
			position = ii;
			return true;
		}
	}
	return false;
}

template<typename A, typename B>
int CArrayManager<A, B>::GetSize()
{
	return m_nTotalElements;
}

#endif
#pragma once

#include "ArrayManager.h"
template<typename A, typename B>
class CArrayIterator
{
public:
	CArrayIterator(CArrayManager<A, B>* pManager, int nSize);
	~CArrayIterator(void);
	bool GetNext(A*& value);
private:
	CArrayManager<A, B>* m_pManager;
	int m_nNumElememts;
	int m_nCurrentIndex;
};

template<typename A, typename B>
CArrayIterator<A, B>::CArrayIterator(CArrayManager<A, B>* pManager, int nSize)
{
	m_pManager = pManager;
	m_nNumElememts = nSize;
	m_nCurrentIndex = 0;
}

template<typename A, typename B>
CArrayIterator<A, B>::~CArrayIterator(void)
{
}

template<typename A, typename B>
bool CArrayIterator<A, B>::GetNext(A*& value)
{
	bool bResult = false;
	if (m_nCurrentIndex < m_nNumElememts)
	{
		m_pManager->GetElement(m_nCurrentIndex, value);
		bResult = true;
		m_nCurrentIndex++;
	}
	return bResult;
}

#pragma once

#include <iostream>
#include "Thing.h"
#include "ArrayManager.h"
#include "ArrayIterator.h"
using namespace std;

#define MAX_ELEMENTS			10
#define ADD			1
#define REMOVE		2
#define RETRIEVE		3
#define FIND			4
#define LIST			5
#define SIZE			6
#define EXIT			9

class CAppClass
{
public:
	CAppClass(void);
	~CAppClass(void);
public:
	void Run();
private:
	int Menu();
	void Add();
	void Remove();
	void Retrieve();
	void Find();
	void List();
	void Size();
private:
	CArrayManager<CThing, int>* m_pManager;
};

#include "StdAfx.h"
#include "AppClass.h"

CAppClass::CAppClass(void)
{
	m_pManager = new CArrayManager<CThing, int>(MAX_ELEMENTS);
}

CAppClass::~CAppClass(void)
{
	delete m_pManager;
}

void CAppClass::Run()
{
	int nChoice = Menu();
	while (nChoice != EXIT)
	{
		switch (nChoice)
		{
		case ADD:
			Add();
			break;
		case REMOVE:
			Remove();
			break;
		case RETRIEVE:
			Retrieve();
			break;
		case FIND:
			Find();
			break;
		case LIST:
			List();
			break;
		case SIZE:
			Size();
			break;
		}
		nChoice = Menu();
	}
}

int CAppClass::Menu()
{
	cout<<"\n  You can: ";
	cout<<"\n      1. Add an element";
	cout<<"\n      2. Delete an element";
	cout<<"\n      3. Retrieve an element by its position in the structure";
	cout<<"\n      4. Find ordinal position of element";
	cout<<"\n      5. List all elements";
	cout<<"\n      6. Get size of structure";
	cout<<"\n      9. Exit";
	cout<<"\n\n";

	int nChoice;
	cin>>nChoice;
	return nChoice;
}

void CAppClass::Add()
{
	int id = 0;
	char name[32] = {0};
	cout<<"\nEnter a Thing id: ";
	cin>>id;
	cout<<"Enter a Thing name: ";
	cin>>name;

	CThing* pThing = new CThing(id, name);
	bool bResult = m_pManager->AddElememt(pThing);
	if (bResult)
		cout<<"\n Element add successfully."<<endl;
	else
		cout<<"\n Can't add element, because the array is full."<<endl;
}

void CAppClass::Remove()
{
	int position;
	cout<<"\nEnter the position to delete: ";
	cin>>position;

	bool bResult = m_pManager->DeleteElement(position);
	if (bResult)
		cout<<"\nThe deletion was successfully."<<endl;
	else
	{
		cout<<"\nThe position was out of range.";
		cout<<"No deletion was performed."<<endl;
	}
}

void CAppClass::Retrieve()
{
	int position;
	cout<<"\nEnter an array position: ";
	cin>>position;

	CThing* pThing = NULL;
	bool bResult = m_pManager->GetElement(position, pThing);
	if (bResult)
		cout<<"\nThe value at "<<position<<" has an ID of "<<pThing->GetID()
		<<"\nand an name of "<<pThing->GetName()<<endl;
	else
		cout<<"\nThe position was out of range.";
}

void CAppClass::Find()
{
	int id;
	cout<<"\nEnter an ID to find: ";
	cin>>id;

	int position;
	bool bResult = m_pManager->FindElement(id, position);
	if (bResult)
		cout<<"\nThe Thing with ID of "<<id<<" is in the position "<<position<<endl;
	else
		cout<<"\nThere is no Thing with ID of "<<id<<" is not in the array."<<endl;
}

void CAppClass::Size()
{
	cout<<"There are "<<m_pManager->GetSize()<<" elements in the array."<<endl;
}

void CAppClass::List()
{
	CThing* pThing = NULL;
	CArrayIterator<CThing, int>* iter = new CArrayIterator<CThing, int>(m_pManager, m_pManager->GetSize());

	int i = 0;
	bool bResult = iter->GetNext(pThing);
	while (bResult)
	{
		cout<<i + 1<<" : ID = "<<pThing->GetID()
			<<"         name = "<<pThing->GetName()<<endl;
		bResult = iter->GetNext(pThing);
		i++;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	//CAppClass app;
	//app.Run();

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值