#pragma once
#ifndef IWR_ARRAY_MANAGER_H_INCLUDE__
#define IWR_ARRAY_MANAGER_H_INCLUDE__
#include <assert.h>
class CArrayManager
{
public:
CArrayManager(int nMaxElements);
~CArrayManager(void);
public:
bool AddElememt(int newValue);
bool GetElement(int position, int& value);
bool DeleteElement(int position);
bool FindElement(int value, int& position);
int GetSize();
private:
int m_nTotalElements;
int m_nMaxElements;
int* m_pArray;
};
#endif
#include "StdAfx.h"
#include "ArrayManager.h"
CArrayManager::CArrayManager(int nMaxElements)
{
assert(nMaxElements > 0);
m_nMaxElements = nMaxElements;
m_nTotalElements = 0;
m_pArray = new int[m_nMaxElements];
}
CArrayManager::~CArrayManager(void)
{
delete [] m_pArray;
}
bool CArrayManager::AddElememt(int newValue)
{
if (m_nTotalElements < m_nMaxElements)
{
m_pArray[m_nTotalElements++] = newValue;
return true;
}
return false;
}
bool CArrayManager::GetElement(int position, int& value)
{
if (position >= 0 && position < m_nTotalElements)
{
value = m_pArray[position];
return true;
}
return false;
}
bool CArrayManager::DeleteElement(int position)
{
if (position >= 0 && position < m_nTotalElements)
{
for (int i = position; i < m_nTotalElements - 1; ++i)
m_pArray[i] = m_pArray[i + 1];
--m_nTotalElements;
return true;
}
return false;
}
bool CArrayManager::FindElement(int value, int& position)
{
for (int ii = 0; ii < m_nTotalElements; ++ii)
{
if (value == m_pArray[ii])
{
position = ii;
return true;
}
}
return false;
}
int CArrayManager::GetSize()
{
return m_nTotalElements;
}
#pragma once
#include "ArrayManager.h"
class CArrayIterator
{
public:
CArrayIterator(CArrayManager* pManager, int nSize);
~CArrayIterator(void);
bool GetNext(int& value);
private:
CArrayManager* m_pManager;
int m_nNumElememts;
int m_nCurrentIndex;
};
#include "StdAfx.h"
#include "ArrayIterator.h"
CArrayIterator::CArrayIterator(CArrayManager* pManager, int nSize)
{
m_pManager = pManager;
m_nNumElememts = nSize;
m_nCurrentIndex = 0;
}
CArrayIterator::~CArrayIterator(void)
{
}
bool CArrayIterator::GetNext(int& 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 "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* m_pManager;
};
#include "StdAfx.h"
#include "AppClass.h"
CAppClass::CAppClass(void)
{
m_pManager = new CArrayManager(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 value;
cout<<"\nEnter a new value: ";
cin>>value;
bool bResult = m_pManager->AddElememt(value);
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;
int value;
bool bResult = m_pManager->GetElement(position, value);
if (bResult)
cout<<"\nThe value at "<<position<<" is "<<value<<endl;
else
cout<<"\nThe position was out of range.";
}
void CAppClass::Find()
{
int value;
cout<<"\nEnter a value to find: ";
cin>>value;
int position;
bool bResult = m_pManager->FindElement(value, position);
if (bResult)
cout<<"\n"<<value<<" is in the position "<<position<<endl;
else
cout<<"\n"<<value<<" is not in the array."<<endl;
}
void CAppClass::Size()
{
cout<<"There are "<<m_pManager->GetSize()<<" elements in the array."<<endl;
}
void CAppClass::List()
{
int value;
CArrayIterator* iter = new CArrayIterator(m_pManager, m_pManager->GetSize());
int i = 0;
bool bResult = iter->GetNext(value);
while (bResult)
{
cout<<i + 1<<" : "<<value<<endl;
bResult = iter->GetNext(value);
i++;
}
}
#include "stdafx.h"
#include "AppClass.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CAppClass app;
app.Run();
return 0;
}