#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 ;
}
编写一个使用数组类模板Array对数组进行排序、求最大值和求元素和的程序,并采用相关数据进行测试。
最新推荐文章于 2023-01-09 23:25:08 发布