数据结构+C++_№4

来源:互联网 发布:svn http nginx 编辑:程序博客网 时间:2024/05/18 02:21

抽象数组的实现

.mycode { font:bold 15 Times; color:#330033; background-color:#f7eef7; border-width:2px; border-style:dashed; border-color:pink; padding:0.5em; FILTER: Alpha(Opacity=50); }

呵呵,数组总的来说还是有些亲切感的,实现起来也很简单:),而且书上的代码也好一些,错误少了不少:)

先来抽象数组定义文件myArray.h:

/*第2章 数组 第2.1.2节抽象数据类型的数组*第38页 抽象数组定义头文件myArray.h** 2005年6月9号,星期四下午* -----------by Speed1*/#ifndef MYARRAY_H#define MYARRAY_H#include <iostream.h>#include <stdlib.h>const int DefaultSize=100;template <class Type>class Array {//数组是相同类型的n(size)个元素的一个收集public:Array(int Size=DefaultSize); //构造函数Array(const Array<Type>& x); //复制构造函数~Array() {delete []elements;} //析构函数Array<Type>& operator =(const Array<Type>& A); //数组复制Type& operator [](int i); //下标Type* operator *() const {return elements;} //指针转换int Length() const {return ArraySize;} //数组长度void ReSize(int sz); //修改数组长度void printArray(); //打印出数组int InputArray(); //输入数组数据private:Type *elements; //底层数组int ArraySize; //数组长度void getArray(); //动态分布数组空间};#endif

然后是实现代码:

/*第2章 数组 第2.1.2节抽象数据类型的数组*第38页 抽象数组实现文件myArray.cpp** 2005年6月9号,星期四下午* -----------by Speed1*/#include <iostream.h>#include "myArray.h"template <class Type> void Array<Type>::getArray(){  //动态分配一个空间,私有函数  elements=new Type[ArraySize]; //创建数组  if(0==elements)  {  cerr<<"Memory Allocation Error!"<<endl;  ArraySize=0;  return;  } }template <class Type> Array<Type>::Array(int sz){  //构造函数,建立一个最大长度为sz的数组  if(0 >=sz)  {  cerr<<"Invalid Array Size!"<<endl;  return;  }  ArraySize=sz;  getArray();}template <class Type> Array<Type>::Array(const Array<Type>& x){  //拷贝构造函数,复制x数组为当前数组  int n=x.ArraySize;  ArraySize=n;  elements=new Type[n];  if(0==elements)  {  cerr<<"Memory Allocation Error"<<endl;  ArraySize=0;  return;  }  Type* srcptr=x.elements;  Type* destprt=elements;  while(n--)  *destprt++=*srcptr++;}template <class Type>Type& Array<Type>::operator[](int i){  //重载操作符[],取下标为不的数组元素。  if(0>i||i>ArraySize-1)  {  cerr<<"Index out of range"<<endl;  ;  }  return elements[i];}template <class Type> void Array<Type>::ReSize(int sz){  if(0>=sz)  cerr<<"Invalid Array Size"<<endl;  if(ArraySize!=sz)  {  Type* newarray=new Type[sz];  if(0==newarray)  cerr<<"Memory Allocation Error!"<<endl;  return;  }  int n=(sz<=ArraySize)?sz:ArraySize;  Type* srcptr=elements;  Type* destprt=newarray;  while(n--)  *destprt++=*srcptr++;  delect []elements;  elements=newarray;  ArraySize=n;}template <class Type> void Array<Type>::printArray()//呵呵,自己写了一个输出数组的方法{  for(int i=0;i<ArraySize;i++)  cout<<"Elements["<<i<<"]:"<<elements[i]<<endl;  cout<<endl;}template <class Type> int Array<Type>::InputArray()//呵呵,自己写的一个输入数组数据的方法{  for(int i=0;i<ArraySize;i++)  {  cout<<"Please enter elements["<<i<<"]:";  cin>>elements[i];  cout<<"/t"<<endl;  }  return 0;}  

最后,书上没有测试代码,自己写了一个:),还是有不少不完善的地方:)

/*第2章 数组 第2.1.2节抽象数据类型的数组*第38页 主程序DS_Cpp_P38.cpp*测试抽象数组的实现情况,自己随便写的的实例,可能有些不恰当** 2005年6月9号,星期四下午* -----------by Speed1*/#include <iostream.h>#include "myArray.h"#include "myArray.cpp"void main(){//抽象数组测试主程序,TestArray1用于构造函数Array(int Size)//TestArray2............Array(const Array<Type>& x)  int ArrayLength; //输入数组长度  int i;  cout<<"Enter the length of Array1:";  cin>>ArrayLength;  cout<<endl;  Array<int> TestArray1(ArrayLength);  Array<int> TestArray2(TestArray1);  cout<<"ArraySize of TestArray1 :"<<TestArray1.Length()<<endl;  cout<<"ArraySize of TestArray2 :"<<TestArray2.Length()<<endl;  cout<<"Main:Print TestArray1:"<<endl;  TestArray1.printArray();  cout<<"Main:Input TestArray2:"<<endl;  TestArray2.InputArray();  cout<<"Main:Print TestArray2:"<<endl;  TestArray2.printArray();  cout<<"Main:Testing the [] operator,Please enter a integer between  0~"<<TestArray1.Length()-1<<":";  cin>>i;  cout<<"TestArray1:"<<TestArray1[i]<<endl;  cout<<"TestArray2:"<<TestArray2[i]<<endl;  }