/////////***************************向量***********************************//
/**************向量管理类的声明*************/
//[程序2-1]
#ifndef VECTOR_MGR
#define VECTOR_MGR 1
class VectorMgr
{
private:
int total_elements,current_size;
int *theVector;
public:
VectorMgr(int);//pass in initial size
~VectorMgr();
bool addElement(int);//pass in asingle element
//pass in ordinal position of element
bool getElement(int,int &);
//pass in ordinal position of element to delete
bool deleteElement(int);
//pass in element balue;return ordinal value
bool findElement(int ,int &);
int getNumbElements();//return total elements in vector
int getSize();//return current size
bool isEmpty();//true if vector is empty
void resize(int);//pass in new size
};
#endif
//[程序2-2]
#include "vectormgr.h"
#include<iostream.h>
VectorMgr::VectorMgr(int size)
{
theVector=new int[size];
if(!theVector)
{
cout<<"\n cannot create vector.";
return;
}
//holds next array index as well as total elements
total_elements=0;
current_size=size;
}
VectorMgr::~VectorMgr()
{
delete []theVector;
}
bool VectorMgr::addElement(int value)
{
if(total_elements==current_size)
return false;//vector is full
theVector [total_elements++]=value;
return true;
}
bool VectorMgr::getElement(int element,int &value)
{
if(element>=total_elements)
return false;
value=theVector[element];
return true;
}
bool VectorMgr::deleteElement(int element)
{
bool result=true;
if(element>=total_elements)
result=false;
else
{
for(int i=element;i<total_elements-1;i++)
theVector[i]=theVector[i+1];
total_elements--;
}
return result;
}
bool VectorMgr::findElement(int value;int &element)
{
int result=true;
int i=0;
while(i<total_elements)
{
if(theVector[i]==value)
element=i;
i++;
}
if(i>=total_elements)
result=false;
return result;
}
int VectorMgr::getNumbElements()
{
return total_elements;
}
int VectorMgr:getSize()
{
return current_size;
}
bool VectorMgr::isEmpty()
{
if(total_elements==0)
return true;
return false;
}
void VectorMgr::resize(int newSize)
{
char yes_no;
if(newSize<total_elements)
{
cout<<"\nthe new size is too small to contain all existing elementsl<<endl;
cout<<"do you want to truncate the vector?(y/n)";
cin>>(yes_no=='y')
total_elements=newSize;
else
return;
}
int *newVector=new int [newSize];
if(!newVector)
{
cout<<"\n cannot create vector.";
return;
}
for(int i=0;i<total_elements;i++)
newVector[i]=thrVector[i];
delete []theVector;
theVector=newVector;
current_size=newSize;
}
//[程序2-3] 管理简单数据类型的向量的迭代类声明
#ifndef VECTOR_ITR
#define VECTOR_ITR 1
#include "VectorMgr.h"
class VectorItr
{
private:
VectorMgr *theVector;
int numb_elements;
int current_index;
public:
//pass in the vector manager and total elements
VectorItr(VectorMgr *,int);
vool getNext(int &);
};
#endif
//[程序2-4] 管理简单数据类型的向量迭代类的具体实现
#include "vectorItr.h"
VectorItr::VectorItr(VectorMgr *inVector,int iElements)
{
theVector=inVector;
numb_elements=iElements;
current_index=0;
}
bool VectorItr::getNext(int &value)
{
bool result=false;
if(current_index<numb_elements)
{
theVector->getElement(current_index,value);
result=true;
current_index++;
}
return result;
}
//[程序2-5] 一个能够重新设定向量大小的应用程序类函数
void AppClass::resize()
{
cout<<"enter a new size ofr the vector:";
int newsize;
cin>>newsize;
testVector->resize(newsize);
}
///**********************************************管理对象的向量*************************//
//[程序2-6] 支持向量的thing类的声明
#ifndef THING
#define THING
#include "Vectorable.h"
class Thing:public Vectorable
{
private:
int thing_id;
char thing_name[20];
public:
Thing();
Thing(int,char []);
int getID();
char *getName();
int getKey();
};
//[程序2-7] 具体实现
#include "Thing.h"
#include <string.h>
Thing::Thing()
{
}
Thing::Thing(int iID,char iName[])
{
thing_id=iID;
strcpy(thing_name,iName);
}
int Thing::getID()
{
return thing_id;
}
char *Thing::getName()
{
return thing_name;
}
int Thing::getKey()
{
return thing_id;
}
//[程序2-8] 用来处理对象的向量管理器的声明
#ifndef VECTOR_MGR
#define VECTOR_MGR
#include "thing.h"
class VectorMgr
{
private:
int total_elements,current_size;
Thing *theVector;
public:
VectorMgr(int);//pass in initial size
~VectorMgr();
bool addElement(Thing);//pass in a single element
//pass in ordinal position of element
bool getElement(int,Thing &);
//pass in ordinal position of element to delete
bool deleteElement(int);
//pass in element value;return ordinal value
bool findElement(int,int &pos);
int getNumbElements();//return total elements in vector
int getSize();//return current size
bool isEmpty();//true if vector is empty
void resize(int);//pass in new size
};
#endif
//[程序2-9] 用来处理对象的 向量管理器的具体实现
#include "vectormgr.h"
VectorMgr::VectorMgr(int size)
{
thrVector=new Thing[size];
if(!theVector)
{
cout<<"\n can not create vector.";
return;
}
//holds next array index as well as total elements
total_elements=0;
current_size=size;
}
VectorMgr::~VectorMgr()
{
delete []theVector;
}
bool VectorMgr::addElement(Thing thThing)
{
if(total_elements==current_size)
return false;//vector is full
theVector[total_elements]=theThing;
total_elements++;
return true;
}
bool VectorMgr::getElement(int element),Thing &theThing)
{
if(element>=total_elements)
return false;
theThing=theVector[element];
return true;
}
bool VectorMgr::deleteElement(int element)
{
bool result=true;
if(element>=total_elements)
result=false;
else
{
for(int i=element;i<total_elements-1;i++)
theVector[i]=theVector[i+1];
total_elements--;
}
return result;
}
bool VectorMgr::findElement(int searchValue,int &element)
{
int result=true;
int i=0;
while(i<total_elements)
{
if(theVector[i].getKey()==searchValue)
element=i;
i++;
}
if(i>=total_elements)
result=false;
return result;
}
int VectorMgr::getNumbElements()
{
return total_elements;
}
int VectorMgr::getSize()
{
return current_size;
}
bool VectorMgr::isEmpty()
{
if(total_elements==0)
return true;
return false;
}
void VectorMgr::resize(int newSize)
{
char yes_no;
if(newSize<total_elements)
{
cout<<"\n the new size is too small to contain all existing elements."<<endl;
cout<<"do you want to truncate the vector?(y/n)";
if(yes_no)=='y')
total_elements=newSize;
else
return;
}
Thing *newVector=new Thing[newSize];
if(!newVector)
{
cout<<"\ncannot create vector.";
return;
}
for(int i=0;i<total_elements;i++)
newVector[i]=theVector[i];
delete []theVector;
theVector=newVector;
current_size=newSize;
}
评论0