数据结构+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; }
- 数据结构+C++_№4
- 数据结构+C++_№1
- 数据结构+C++_№2
- 数据结构+C++_№3
- 数据结构+C++_№5
- c语言_数据结构_顺序表
- c语言_数据结构_单链表
- 【C语言】树_数据结构_练习
- 【C语言】图_数据结构_练习
- 【C语言】查找_数据结构_练习
- C数据结构算法_搜索
- 数据结构+C++_№4 抽象数组的实现 代码补充及更正
- 数据结构+C++_№5_顺序表错误更正及新的代码
- 数据结构(C#)_排序算法(冒泡排序)
- 数据结构(C#)_排序算法(插入排序)
- 数据结构(C#)_排序算法(插入排序)
- 数据结构(C#)_排序算法(归并排序)
- 数据结构(C#)_查找(静态查找)
- 故事最近不会更新
- 数据结构+C++_№3
- 总结
- XML2等杂事
- 2005.6.10杂记
- 数据结构+C++_№4
- many-to-many性能
- 我也成博客了!
- 从陌生到陌生
- 处世哲学:看人
- 第一次用PROCEDURE 和CLASS 做ASP.NET (C#)
- 大学生活就这么结束了
- aa
- hibernate 初级应用篇