c++题目分享

C++期末考试题目3分享

任务003:(函数模板)
函数功能说明:
将输入数组的各个元素的值按从大到小顺序返回
残缺的代码:
int main()
{
int IntArray[5] = {440, 200, 400,30, 1000};
float FloatArray[3] = { 1.55f, 5.44f, 12.36f};
cout << Getsort (IntArray, 5);
cout << Getsort (FloatArray, 3);
}

//head.h
#include<iostream>
using namespace std;
int SIZE;
template<typename T>
T*  Getsort(T *element, int size) {
	T temp;
	SIZE = size;
	for (int i = 0; i<size; i++) {
		for (int j = i; j<size; j++) {
			if (element[i]<element[j]) {
				temp = element[i];
				element[i] = element[j];
				element[j] = temp;
			}
		}
	}
	return element;
}

template<typename T>
ostream & operator <<(ostream &output, const T *element) {
	for(int i=0;i<SIZE;i++) {
		output << *element;
		element++;
	}
	return output;
}
//main.cpp
#include"head.h"
int main()
{
	int IntArray[5] = { 440, 200, 400,30, 1000 };
	float FloatArray[3] = { 1.55f, 5.44f, 12.36f };
	cout << Getsort(IntArray, 5)<<endl;
	cout << Getsort(FloatArray, 3)<<endl;
}

你可能感兴趣的:(c++)