编写一个使用数组类模板Array对数组进行排序、求最大值和求元素和的程序,并采用相关数据进行测试。

来源:互联网 发布:最好网络投资理财 编辑:程序博客网 时间:2024/04/28 09:55
#include"iostream"#include"algorithm"#include"string.h"using namespace std;template<class ElemType>class Array{private:int length;ElemType *a;public:Array(){cout << "元素个数为:" << endl ;cin >> length ;a=new ElemType[length];cout << "请输入元素" << endl ;for(int i=0;i<length;i++)cin >> a[i];}void Asort(){sort(a,a+length);cout << "sort finish" << endl ;}ElemType max(){return a[length-1];}ElemType sum(){int i=0;ElemType sum=0;while(i<length){sum=sum+a[i];i++;}return sum;}};void main(){Array<int>array;array.Asort();cout << "the max=" << array.max() << endl ;cout << "the sum=" << array.sum() << endl ;}