#include<iostream>
#include<time.h>
#include<stdlib.h>
using std::cin;
using std::cout;
class IntArray {
private:
int index;
int size;
int * a_head;
int Getval(int condition);
public:
IntArray() {
index = 0;
size = 0;
a_head = NULL;
};
IntArray(int n) :size(n),index(0) {
a_head = new int[size];
};
void putback(int x) {//向数组尾部插入元素
if (index >size||index==size )
cout << "数组已满,不能再次插入!插入失败!\n";
a_head[index++] = x;
};
int Find_Max();
int Find_Min();
void Array_Show();
~IntArray() {
delete[] a_head;
};
void Sort();
int operator[](int ind) {
return a_head[ind];
};
};
int IntArray::Getval(int condition) {
int temp = a_head[0];
for (int i = 1; i < size; i++) {
if (condition == 0) {//求最大值的条件
if (temp < a_head[i])
temp = a_head[i];
}
else if (condition == 1) {//求最小值的条件
if (temp > a_head[i])
temp = a_head[i];
}
}
return temp;
}
int IntArray::Find_Max() {
return Getval(0);
}
int IntArray::Find_Min() {
return Getval(1);
}
void IntArray::Sort() {//用插入排序实现数组排序
int temp,j;
for (int i = 1; i < size; i++) {
temp = a_head[i];
if (temp < a_head[i-1]) {
for ( j = i - 1; temp< a_head[j]; j--) {
a_head[j + 1] = a_head[j];
}
a_head[j + 1] = temp;
}
}
}
void IntArray::Array_Show() {
cout << "输入数组的大小:\n";
cout << "输入" << index << "个元素:" << std::endl;
cout << "数组元素:";
Sort();//对数组元数排序
for (int i = 0; i < size; i++)
cout << a_head[i] << " ";
cout << std::endl;
cout << "随机取一个值为:" << a_head[rand() % size]<<std::endl;
cout << "最大值:" << Find_Max() << std::endl;
cout << "最小值:" << Find_Min() << std::endl;
system("pause");
}
int main() {
srand((unsigned int)time(0));//种下随机种子
int m;
cin >> m;
IntArray Int_array(m);
int* a = new int [m];
for (int i = 0; i < m; i++) {
cin >> a[i];
Int_array.putback(a[i]);
}
Int_array.Array_Show();
Int_array.~IntArray();
return 0;
}
下面是运行结果: