C++学习(12)

  1 //设计一个double类型的数组Array
  2 //1.可定义下界下标和上届上标
  3 //2.可对下标越界进行检查
  4 //3.可重置数组大小
  5 //4.可知道数组的长度
  6 //5.数组类的对象可以使用赋值运算符=和下标运算符[]
  7 
  8 #include<iostream.h>
  9 #include<stdlib.h>
 10 class Array{
 11     private:
 12         int size;
 13         int low;
 14         int up;
 15         double *arr;
 16     public:
 17         Array(int sz=100);
 18         Array(int low,int up);
 19         Array(const Array &arr);
 20         ~Array();
 21 
 22         void operator=(const Array &arr);
 23         double &operator[](int ndx)const;
 24         void Resize(int sz);
 25         void Resize(int ll,int hh);
 26         int ArraySize()const;
 27 };
 28 
 29 Array::Array(int sz){
 30     if(sz<=0){
 31         exit(0);
 32     }
 33     
 34     low=0;
 35     up=sz-1;
 36     size=sz;
 37     arr=new double[size];
 38 }
 39 
 40 Array::Array(int low,int up){
 41     this->low=low;
 42     this->up=up;
 43     size=up-low+1;
 44     arr=new double[size];
 45 }
 46 
 47 Array::Array(const Array &arr){
 48     int n=arr.size;
 49     size=n;
 50     this->arr=new double[size];
 51 
 52     double *sourcePtr=arr.arr;
 53     double *destPtr=this->arr;
 54     while(n--){
 55         *destPtr++=*sourcePtr++;
 56     }
 57 }
 58 
 59 Array::~Array(){
 60     delete []this->arr;
 61 }
 62 
 63 void Array::operator=(const Array &arr){
 64     if(&arr==(this)){//如果是自己等于自己,那么返回空
 65         return;
 66     }
 67     
 68     int n=arr.size;
 69     size=n;
 70     this->arr=new double[size];
 71     
 72     double *sourcePtr=arr.arr;
 73     double *destPtr=this->arr;
 74     while(n--){
 75         *destPtr++=*sourcePtr++;
 76     }
 77 }
 78 
 79 double &Array::operator[](int ndx)const{
 80     if((ndx<low) || (ndx>size-1) ){
 81         cout<<"数组下标越界"<<endl;
 82         exit(0);
 83     }
 84     return this->arr[ndx-low];    
 85 }
 86 
 87 
 88 void Array::Resize(int sz){
 89     if(sz<0){
 90         exit(0);
 91     }
 92     if(sz==this->size){
 93         return;
 94     }
 95 
 96     double *newArray=new double[size];
 97     int n=(sz<=size)?sz:size;
 98     double *sourcePtr=arr;
 99     double *destPtr=newArray;
100     while(n--){
101         *destPtr++ = *sourcePtr++;
102     }
103     delete []this->arr;
104 
105     this->arr=newArray;
106     this->size=sz;
107     this->low=0;
108     this->up=size-1;
109 }
110 
111 
112 void Array::Resize(int ll,int hh){
113     int sz=hh-ll+1;
114     double *newArray=new double[sz];
115 
116     int n=(sz<=this->size)?sz:size;
117     double *sourcePtr=this->arr;
118     double *destPtr=newArray;
119 
120     while(n--){
121         *destPtr++=*sourcePtr++;
122     }
123 
124     delete []this->arr;
125     this->arr=newArray;
126     this->size=sz;
127     this->low=ll;
128     this->up=hh;
129 }
130 
131 int Array::ArraySize()const{
132     return this->size;
133 }
134 
135 int main(){
136     Array a(-5,5);
137     cout<<"ArraySize="<<a.ArraySize()<<endl;
138     for(int i=-5;i<5;i++){
139         a[i]=i;
140     }
141 
142     a.Resize(30);
143     a[21]=21;
144     cout<<"ArraySize="<<a.ArraySize()<<endl;
145 
146     return 0;
147 }

猜你喜欢

转载自www.cnblogs.com/Tobi/p/9245006.html
今日推荐