// TempleTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
#include <random>
/*
编写一个程序,使用类模板对数组元素进行排序,倒置、查找和求和
具有对数组元素进行排序,倒置、查找和求和功能,
然后产生类型实参分别为int型和double型的两个模板类,
分别对整型数组与双精度数组完成所要求的操作
*/
using namespace std;
const int SIZE = 100;
template <class Type>
class Array {
private:
int l, z;
Type a[SIZE];
public:
Array(Type *b, int n)
{
int i;
l = n;
cout << "Array 构造函数:" << endl;
for (i = 0; i < l; i++) {
a[i] = b[i];
cout << a[i] << " ";
}
cout << endl;
}
void sort();
void reverse();
void find(Type t);
void sum();
};
template <class Type>
void Array<Type>::sort() {
Type c[SIZE];
int i, j;
Type m;
for (i = 0; i < l; i++) {
c[i] = a[i];
}
// 这下面的{}最好一个都不要省
// 一个都不省的话 首先方便后期添加代码 其次看起来舒服
// 排序 升序排序
for (j = 0; j < l; j++)
{
for (i = 0; i < l; i++)
{
if (c[i] > c[j])
{
m = c[i];
c[i] = c[j];
c[j] = m;
}
}
}
// 输出排序后的数据 -> a b c
for (i = 0; i < l; i++)
{
cout << c[i] << " ";
}
cout << endl;
}
template<class Type>
void Array<Type>::reverse()
{
Type c[SIZE];
Type m;
int i, j;
for (i = 0; i < l; i++)
{
c[i] = a[i];
}
for (j = 0; j < l; j++)
{
if (j > (l / 2)) break;
m = c[j];
c[j] = c[l-j-1];
c[l-j-1] = m;
}
for (i = 0; i < l; i++)
{
cout << c[i] << " ";
}
cout << endl;
}
int main()
{
std::cout << "Hello World!\n";
int i,x;
int *b;
b = new int[10];
x = 11;
//整数
for (i = 0; i < x; i++) {
b[i] = rand() % 100 +1;
}
Array<int> c(b, x);
c.sort();
//实数
//实数随机数
random_device rd;
mt19937 gen(rd());
uniform_real_distribution<> dis(1.0, 2.0);
double *d;
d = new double[10];
for (i = 0; i < x;i++)
{
d[i] = dis(gen);
}
Array<double> f(d, x);
f.sort();
int k;
cin >> k;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单