/*
【任务1扩展一(选做)】规定MyArray只能处理元素为整型的数据未免太弱了,
请设计成模板类,使之适应各种类型(事实上,C++增加的标准类对些类情况均设计成了模板类)。
*/
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: MyArray.cpp
* 作 者: 计114-3 王兴锋
* 完成日期: 2012 年 5 月 21 日
* 版 本 号: V 2.0
* 对任务及求解方法的描述部分
* 程序头部的注释结束
*/
#include <iostream>
using namespace std;
template <class numtype>
class MyArray
{
private:
numtype *arr; //用于存放动态分配的数组内存首地址
numtype size; //数组大小
public:
MyArray(int sz=50);
MyArray(numtype a[], int sz); //由一个内置类型的数组初始化
MyArray(const MyArray &A); //拷贝构造函数
~MyArray(void); //析构函数,注意释放空间
MyArray & operator = (const MyArray &A); //重载“=”使得数组对象可以整体赋值
numtype & operator [] (int i); //重载[],使得Array对象也可以如C++普通数组一样,用a[i]形式取出值【选做】
bool operator == (MyArray& A); //重载==,使得Array对象能整体判断两个数组是否相等(size相等且对应元素相等)
MyArray operator + (MyArray& A); //重载+,使两个Array对象可以整体相加(前提大小相等)【选做】
template <class numtype>
friend ostream& operator << (ostream& out, MyArray<numtype>& A); //重载<<,输出数组
int GetSize(void)const; //取数组大小;
void Resize(int sz); //修改数组的大小,如果sz大于数组的原大小,增加的元素初始为;sz小于数组的原大小,舍弃后面的元素【选做】
};
template <class numtype>
MyArray<numtype>::MyArray(int sz)
{
size = sz;
arr = new numtype [size];
for (int i = 0; i < size; ++i)
arr[i] = 0;
}
template <class numtype>
MyArray<numtype>::MyArray(numtype a[], int sz)
{
size = sz;
arr = new numtype [size];
for (int i = 0; i < size; ++i)
arr[i] = a[i];
}
template <class numtype>
MyArray<numtype>::MyArray(const MyArray<numtype> &A)
{
this->size = A.size;
this->arr = new numtype [this->size];
for (int i = 0; i < size; ++i)
this->arr[i] = A.arr[i];
}
template <class numtype>
MyArray<numtype>::~MyArray(void)
{
delete []arr;
}
template <class numtype>
MyArray<numtype> & MyArray<numtype>::operator = (const MyArray<numtype> &A)
{
this->size = A.size;
this->arr = new numtype [this->size];
for (int i = 0; i < size; ++i)
this->arr[i] = A.arr[i];
return *this;
}
template <class numtype>
numtype & MyArray<numtype>::operator[] (int i)
{
return arr[i];
}
template <class numtype>
bool MyArray<numtype>::operator == (MyArray<numtype>& A)
{
if (this->size != A.size)
return false;
for (int i = 0; i < this->size; ++i)
{
if (this->arr[i] != A.arr[i])
return false;
}
return true;
}
template <class numtype>
MyArray<numtype> MyArray<numtype>::operator + (MyArray<numtype>& A)
{
if (this->size == A.size)
{
MyArray ma(A);
for (int i = 0; i < this->size; ++i)
{
ma.arr[i] = this->arr[i] + A.arr[i];
}
return ma;
}
else
cout << "两数组大小不相等不可相加!" << endl;
return *this;
}
template <class numtype>
ostream& operator << (ostream& out, MyArray<numtype> &A)
{
cout << "数组元素为:" << endl;
for (int i = 0; i < A.size; ++i)
out << "arr[" << i << "]=" << A.arr[i] << endl;
return out;
}
template <class numtype>
int MyArray<numtype>::GetSize(void)const
{
return size;
}
template <class numtype>
void MyArray<numtype>::Resize(int sz)
{
if (size > sz)
{
for (int i = sz; i < size; ++i)
arr[i] = 0;
size = sz;
}
else
{
numtype *sarr = new numtype [sz];
for (int i = 0; i < size; ++i)
sarr[i] = arr[i];
for (int i = size; i < sz; ++i)
{
sarr[i] = 0;
}
arr = sarr;
size = sz;
}
}
int main()
{
double a[10] = {1.1,2.1,3.0,4.1,5.0,6.1,7.0,8.0,9.0,10.0};
double b[10] = {4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0};
MyArray <double> arr1(a, 10);
MyArray <double> arr2(b, 10);
MyArray <double> arr3(10);
cout << arr3;
arr3 = arr1 + arr2;
cout << arr3;
arr3.Resize(20);
cout << arr3;
arr3.Resize(5);
cout << arr3;
system("pause");
return 0;
}
/*
数组元素为:
arr[0]=0
arr[1]=0
arr[2]=0
arr[3]=0
arr[4]=0
arr[5]=0
arr[6]=0
arr[7]=0
arr[8]=0
arr[9]=0
数组元素为:
arr[0]=5.1
arr[1]=7.1
arr[2]=9
arr[3]=11.1
arr[4]=13
arr[5]=15.1
arr[6]=17
arr[7]=19
arr[8]=21
arr[9]=23
数组元素为:
arr[0]=5.1
arr[1]=7.1
arr[2]=9
arr[3]=11.1
arr[4]=13
arr[5]=15.1
arr[6]=17
arr[7]=19
arr[8]=21
arr[9]=23
arr[10]=0
arr[11]=0
arr[12]=0
arr[13]=0
arr[14]=0
arr[15]=0
arr[16]=0
arr[17]=0
arr[18]=0
arr[19]=0
数组元素为:
arr[0]=5.1
arr[1]=7.1
arr[2]=9
arr[3]=11.1
arr[4]=13
请按任意键继续. . .
*/
/*
定义类模板提高了代码的功能,但是也给程序员带来了负担。
不解释、、、
*/
《C++第十四周实验报告1-2》---规定MyArray只能处理元素为整型的数据未免太弱了, 请设计成模板类,使之适应各种类型...
最新推荐文章于 2022-05-21 15:42:38 发布